WM_KILLFOCUS逻辑
WM_KILLFOCUS logic
我有以下代码,见下文。
为什么 case WM_KILLFOCUS WinMain() 中的代码从未到达?
如果我从 WndProc() 中删除 case WM_KILLFOCUS,WinMain() 中的那个仍然没有达到。
案例 WM_KEYDOWN 工作得很好。
如何修改代码,以便达到 WinMain() 中的 case WM_KILLFOCUS?
代码非常基础。
谢谢。
#include <windows.h>
HINSTANCE m_hinstance_Module = NULL;
HWND m_hwnd_Window = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch (umessage)
{
case WM_KILLFOCUS:
// THE CODE REACHES THIS POINT.
MessageBoxA(hwnd, "WM_KILLFOCUS", "WndProc()", 0);
return DefWindowProc(hwnd, umessage, wparam, lparam);
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, umessage, wparam, lparam);
}
// Done.
return 0;
}
void InitializeWindow()
{
WNDCLASSEX struct_WNDCLASSEX;
m_hinstance_Module = GetModuleHandle(NULL); // If this parameter is NULL, GetModuleHandle returns a
handle to the file used to create the calling process (.exe file).
struct_WNDCLASSEX.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
struct_WNDCLASSEX.lpfnWndProc = WndProc;
struct_WNDCLASSEX.cbClsExtra = 0;
struct_WNDCLASSEX.cbWndExtra = 0;
struct_WNDCLASSEX.hInstance = m_hinstance_Module;
struct_WNDCLASSEX.hIcon = LoadIcon(NULL, IDI_WINLOGO);
struct_WNDCLASSEX.hIconSm = struct_WNDCLASSEX.hIcon;
struct_WNDCLASSEX.hCursor = LoadCursor(NULL, IDC_ARROW);
struct_WNDCLASSEX.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
struct_WNDCLASSEX.lpszMenuName = NULL;
struct_WNDCLASSEX.lpszClassName = L"TEST";
struct_WNDCLASSEX.cbSize = sizeof(WNDCLASSEX);
RegisterClassEx(&struct_WNDCLASSEX);
m_hwnd_Window = CreateWindowEx(WS_EX_APPWINDOW, L"TEST", L"TEST", WS_OVERLAPPEDWINDOW, 0, 0, 800,
600, NULL, NULL, m_hinstance_Module, NULL);
ShowWindow(m_hwnd_Window, SW_SHOW);
SetForegroundWindow(m_hwnd_Window);
SetFocus(m_hwnd_Window);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine,
_In_ int nShowCmd)
{
MSG structMsg;
bool blnDone = false;
InitializeWindow();
ZeroMemory(&structMsg, sizeof(MSG));
while (!blnDone)
{
if (PeekMessage(&structMsg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&structMsg);
DispatchMessage(&structMsg);
}
if (structMsg.message == WM_QUIT)
{
blnDone = true;
}
else
{
switch (structMsg.message)
{
case WM_KILLFOCUS:
// THE CODE NEVER REACHES THIS POINT.
MessageBoxA(m_hwnd_Window, "WM_KILLFOCUS", "WinMain()", 0);
break;
case WM_KEYDOWN:
MessageBoxA(m_hwnd_Window, "WM_KEYDOWN", "WinMain()", 0);
break;
}
}
}
return 0;
}
Message routing 使用以下两种方法之一完成:
The system uses two methods to route messages to a window procedure: posting messages to a first-in, first-out queue called a message queue, a system-defined memory object that temporarily stores messages, and sending messages directly to a window procedure.
发布到消息队列的消息称为 queued messages, whereas messages that are immediately passed to a window procedure are called nonqueued messages。
WM_KILLFOCUS
是非队列消息1。这就是为什么 window 过程会观察到此消息,而消息循环不会。
这是架构限制。您不能使非排队消息显示在消息队列中。
1 提示在documentation中编码:“发送到window”,而不是 "posted".
我有以下代码,见下文。
为什么 case WM_KILLFOCUS WinMain() 中的代码从未到达? 如果我从 WndProc() 中删除 case WM_KILLFOCUS,WinMain() 中的那个仍然没有达到。 案例 WM_KEYDOWN 工作得很好。
如何修改代码,以便达到 WinMain() 中的 case WM_KILLFOCUS?
代码非常基础。
谢谢。
#include <windows.h>
HINSTANCE m_hinstance_Module = NULL;
HWND m_hwnd_Window = NULL;
LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch (umessage)
{
case WM_KILLFOCUS:
// THE CODE REACHES THIS POINT.
MessageBoxA(hwnd, "WM_KILLFOCUS", "WndProc()", 0);
return DefWindowProc(hwnd, umessage, wparam, lparam);
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, umessage, wparam, lparam);
}
// Done.
return 0;
}
void InitializeWindow()
{
WNDCLASSEX struct_WNDCLASSEX;
m_hinstance_Module = GetModuleHandle(NULL); // If this parameter is NULL, GetModuleHandle returns a
handle to the file used to create the calling process (.exe file).
struct_WNDCLASSEX.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
struct_WNDCLASSEX.lpfnWndProc = WndProc;
struct_WNDCLASSEX.cbClsExtra = 0;
struct_WNDCLASSEX.cbWndExtra = 0;
struct_WNDCLASSEX.hInstance = m_hinstance_Module;
struct_WNDCLASSEX.hIcon = LoadIcon(NULL, IDI_WINLOGO);
struct_WNDCLASSEX.hIconSm = struct_WNDCLASSEX.hIcon;
struct_WNDCLASSEX.hCursor = LoadCursor(NULL, IDC_ARROW);
struct_WNDCLASSEX.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
struct_WNDCLASSEX.lpszMenuName = NULL;
struct_WNDCLASSEX.lpszClassName = L"TEST";
struct_WNDCLASSEX.cbSize = sizeof(WNDCLASSEX);
RegisterClassEx(&struct_WNDCLASSEX);
m_hwnd_Window = CreateWindowEx(WS_EX_APPWINDOW, L"TEST", L"TEST", WS_OVERLAPPEDWINDOW, 0, 0, 800,
600, NULL, NULL, m_hinstance_Module, NULL);
ShowWindow(m_hwnd_Window, SW_SHOW);
SetForegroundWindow(m_hwnd_Window);
SetFocus(m_hwnd_Window);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine,
_In_ int nShowCmd)
{
MSG structMsg;
bool blnDone = false;
InitializeWindow();
ZeroMemory(&structMsg, sizeof(MSG));
while (!blnDone)
{
if (PeekMessage(&structMsg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&structMsg);
DispatchMessage(&structMsg);
}
if (structMsg.message == WM_QUIT)
{
blnDone = true;
}
else
{
switch (structMsg.message)
{
case WM_KILLFOCUS:
// THE CODE NEVER REACHES THIS POINT.
MessageBoxA(m_hwnd_Window, "WM_KILLFOCUS", "WinMain()", 0);
break;
case WM_KEYDOWN:
MessageBoxA(m_hwnd_Window, "WM_KEYDOWN", "WinMain()", 0);
break;
}
}
}
return 0;
}
Message routing 使用以下两种方法之一完成:
The system uses two methods to route messages to a window procedure: posting messages to a first-in, first-out queue called a message queue, a system-defined memory object that temporarily stores messages, and sending messages directly to a window procedure.
发布到消息队列的消息称为 queued messages, whereas messages that are immediately passed to a window procedure are called nonqueued messages。
WM_KILLFOCUS
是非队列消息1。这就是为什么 window 过程会观察到此消息,而消息循环不会。
这是架构限制。您不能使非排队消息显示在消息队列中。
1 提示在documentation中编码:“发送到window”,而不是 "posted".