after clicking child window, cant interact with parent window

78 Views Asked by At

I created a button. After i click the button, it works but after clicking i cant interact with main window(such as pressing arrow key). when i click outside main window (killing focus on main window), then i click main window(focus on main window) then i can interact with main window. i dont know what's going on. here is the code Link and the button creates as following.

inline void Button::create_button() {
    const auto _button_width = button_wrapper_.right - button_wrapper_.left;
    const auto _button_height = button_wrapper_.bottom - button_wrapper_.top;

    auto _button_hwnd = CreateWindow(
        L"Button",
        text_,
        WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
        button_wrapper_.left,
        button_wrapper_.top,
        _button_width,
        _button_height,
        parent_hwnd_,
        nullptr,
        reinterpret_cast<HINSTANCE>(GetWindowLongPtr(parent_hwnd_, GWLP_HINSTANCE)),
        nullptr);

}

I tried to refocus main windows after click button but it not works. I tried to create button as Win32 tutorial and it still not work(maybe i misunderstand it?)

1

There are 1 best solutions below

0
Simon Mourier On BEST ANSWER

The problem is the button "steals the focus" when we click on it, so the window doesn't receive WM_KEYDOWN messages any more. This is by design. See also this on SO: C++ Window losing focus when child button is pressed

There are multiple way to fix it, but in this case the easier is to add a SetFocus call, right after the button has been clicked, like this:

case WM_COMMAND: {
        switch (LOWORD(w_param)) {
        case BN_CLICKED: {
            board_controller_.reset_board();
            SetFocus(m_hwnd_); // add this here to give focus back to window
            break;
        }
        }
        return 0;