Stops a specified child window from activating its parent
Syntax
BOOL WINAPI NtUserSetChildWindowNoActivate (
HWND hwndChild
)
Parameters
- hwndChild
- The child window that shouldn't activate its parent
Return Value
Nonzero if the function succeeded, zero otherwise
Remarks
Calling this function allows the child window to be interacted with (ie buttons can be clicked) without making their parent the foreground window or changing the keyboard focus
Note: if this function succeeds when hwndChild identifies an edit control, clicking in it will display a carat, but any keys pressed will affect the foreground window and not the edit control.
This function is also known as _NtUserSetChildWindowNoActivate@4.
Example
// clicking the "No KeyFocus" button will trigger WM_COMMAND messages // but not change keyboard focus // the "KeyFocus" button will do both // the edit control displays the title of the foreground // window during WM_COMMAND messages of both buttons // NtUserSetChildWindowNoActivate isn't exported by name // it is ordinal 2005 in user32 in Windows 7 and 8 // this is queried and called in WM_CREATE #define WIN32_LEAN_AND_MEAN #define _WIN32_WINNT 0x0600 #include <windows.h> #include <commctrl.h> #include <stdio.h> static const USHORT ID_NOFOCUS = 1; static const USHORT ID_FOCUS = 2; static const USHORT ID_EDIT = 3; #pragma comment(lib, "comctl32.lib") LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { HMODULE hMod = GetWindowInstance(hwnd); HWND hNoKeyFocus = CreateWindowEx(0, WC_BUTTON, L"No KeyFocus", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 10, 200, 50, hwnd, (HMENU)ID_NOFOCUS, hMod, NULL); HMODULE hUser = GetModuleHandle(L"user32.dll"); typedef BOOL (WINAPI*pfnSetNoParentFocus)(HWND); pfnSetNoParentFocus setNoParentFocus = (pfnSetNoParentFocus)GetProcAddress(hUser, (LPCSTR)2005); setNoParentFocus(hNoKeyFocus); CreateWindowEx(0, WC_BUTTON, L"KeyFocus", WS_CHILD | WS_VISIBLE | WS_TABSTOP, 10, 70, 200, 50, hwnd, (HMENU)ID_FOCUS, hMod, NULL); CreateWindowEx(0, WC_EDIT, L"", WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_LEFT, 10, 125, 200, 30, hwnd, (HMENU)ID_EDIT, hMod, NULL); return 0; } break; case WM_COMMAND: { if(LOWORD(wParam) != ID_EDIT) { WCHAR buffer[MAX_PATH + 20]; WCHAR foregroundBuffer[MAX_PATH]; GetWindowText(GetForegroundWindow(), foregroundBuffer, ARRAYSIZE(foregroundBuffer)); swprintf(buffer, ARRAYSIZE(buffer), L"Foreground = %s", foregroundBuffer); SetWindowText(GetDlgItem(hwnd, ID_EDIT), buffer); } } break; case WM_DESTROY: { PostQuitMessage(0); return 0; } break; } return DefWindowProc(hwnd, msg, wParam, lParam); } int WINAPI WinMain(HMODULE hInst, HMODULE, LPSTR, int) { InitCommonControls(); WNDCLASSEX wndClass = {sizeof(wndClass), 0}; wndClass.hbrBackground = (HBRUSH)COLOR_WINDOW + 1; wndClass.lpfnWndProc = &WndProc; wndClass.hIcon = wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wndClass.lpszClassName = L"Class"; wndClass.hInstance = hInst; RegisterClassEx(&wndClass); HWND hwnd = CreateWindowEx(WS_EX_TOPMOST, wndClass.lpszClassName, L"NtUserSetChildWindowNoActivate", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 300, 200, NULL, NULL, hInst, NULL); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }