gpt4 book ai didi

c++ - Win32 : C++: How do I re-focus on Parent Window after clicking in a child window?

转载 作者:行者123 更新时间:2023-11-28 02:23:12 25 4
gpt4 key购买 nike

在我的 Win32 CPP 程序中,我定义了一些子窗口来显示各种文本字符串,例如:

hnd_to_this_ch_window = CreateWindow( 
L"EDIT",L"Some initial text", WS_VISIBLE | WS_CHILD | ES_LEFT,
position_of_this_window_X,
position_of_this_window_Y,
TEXTOUT_DEFAULT_WIDTH,
TEXTOUT_DEFAULT_HEIGHT,
handle_to_my_parent_window, NULL,
hinstance_variable_used_by_create_window,
NULL )

我的问题是,如果我用鼠标单击以选择其中一个子窗口中的文本(例如,将其复制到某处),应用程序的焦点将转到该子窗口,因此以前的任何按键通过我的主窗口处理 CALLBACK(with case WM_KEYDOWN:)现在被捕获到子窗口中,在那里它们显示为输入的字符。我调用什么魔术函数让焦点回到父级(以便我的 WM_KEYDOWN)可以再次工作?我希望我可以只点击主窗口的标题栏,这样它就会恢复正常,但这是行不通的(因为,很明显,我的程序缺少一些额外的逻辑)。

最佳答案

在要获得焦点的窗口的窗口过程中处理WM_KILLFOCUS消息,并使用SetFocus函数恢复焦点。

如果你想在窗口被点击时获得焦点,处理 WM_LBUTTONDOWN 消息。

LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
// Restore the focus when it was lost.
if (Msg == WM_KILLFOCUS) {
SetFocus(hWnd);
// Msg was handled, return zero.
return 0;
}
// Or when the window is clicked.
if (Msg == WM_LBUTTONDOWN) {
SetFocus(hWnd);
// Msg was handled, return zero.
return 0;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}

关于c++ - Win32 : C++: How do I re-focus on Parent Window after clicking in a child window?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com