I'm having an issue with WinAPI Edit controls in a child window - they don't respond to mouse clicks, but work fine when placed in the main window. Other controls like buttons work normally in both windows.
I have a main window with a ListView and buttons, and a child window that contains an Edit control and a Button.
This is how the application looks
The Edit control in the child window:
- Doesn't get focus when clicked
- Doesn't show text cursor when clicked
- Doesn't allow text selection with mouse
- However: The Edit control works perfectly if I place it in the main window instead of the child window
- The Button in the child window works fine and responds to clicks
- Calling SetFocus() on the Edit control works
Here's the code: Pastebin
Main Window Creation
Window::Window(int WIDTH, int HEIGHT) {
// ... window class registration
hwnd = CreateWindowEx(/* ... main window params */);
// Edit control in main window (THIS WORKS FINE)
// But I need it in the child window
}
Child Window Creation
ChildWindow::ChildWindow(HWND parentHwnd) {
// ... child window class registration
hwnd = CreateWindowExW(
0,
CHILD_CLASS_NAME,
L"Child Window",
WS_CAPTION | WS_SYSMENU | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
LV_WIDTH, 0, windowWidth, windowHeight,
parentHwnd, // parent is main window
NULL, hInstance, NULL
);
// Problematic Edit control in child window
passEditHwnd = CreateWindowW(
L"EDIT", L"",
WS_BORDER | WS_VISIBLE | WS_CHILD | WS_TABSTOP | ES_AUTOHSCROLL,
(windowWidth / 2) - (buttonWidth / 2),
(windowHeight / 4) * 3,
buttonWidth, buttonHeight,
hwnd, // parent is child window
(HMENU)CHILD_EDIT_ID, hInstance, NULL
);
// This button in the same child window works fine
testHwnd = CreateWindowW(
L"BUTTON", L"test",
WS_BORDER | WS_TABSTOP | WS_VISIBLE | WS_CHILD,
// ... same parameters
hwnd, (HMENU)CHILD_TEST_BTN_ID, hInstance, NULL
);
}
Message Processing
bool Window::ProcessMessages() {
MSG msg = {};
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE) > 0) {
if (msg.message == WM_QUIT || msg.message == WM_DESTROY) {
return false;
}
TranslateMessage(&msg); // This is present
DispatchMessage(&msg);
}
return true;
}
What I've Tried
- Different Edit control styles: WS_TABSTOP, ES_AUTOHSCROLL, ES_CLIENTEDGE
- Explicit focus setting: SetFocus(passEditHwnd) works when called from button click
- EnableWindow(): Tried enabling/disabling - no effect
- TranslateMessage(): Already present in message loop
- Window z-order: Tried BringWindowToTop(), SetWindowPos(), doesn't work
- Different parent relationships: Same issue
Thank you for help!
New contributor
kanashiii is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
lang-cpp
WS_CAPTION,WS_SYSMENU) that block focus when combined withWS_CHILD. You should try to: removeWS_CAPTIONandWS_SYSMENUfrom the child window style. AddWS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN(andWS_BORDERif you want a frame). HandleWM_MOUSEACTIVATEin the child window procedure. (Optional) AddWS_EX_CONTROLPARENTinCreateWindowExto enable Tab navigation.