【发布时间】:2013-12-03 06:07:20
【问题描述】:
我有一个非常旧的应用程序,我很惊讶。此应用程序在没有消息循环的情况下运行。 (GetMessage 或 PeekMessage)。
这怎么可能?
从 Visual Studio 编辑的示例:
HINSTANCE g_hInstance = NULL;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow);
ATOM _RegisterClass(HINSTANCE hInstance);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
_RegisterClass(hInstance);
InitInstance(hInstance, SW_NORMAL);
return 0;
}
ATOM _RegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXA wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_SAVEBITS;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = "TEST_CLASS";
ATOM a = 0;
a = RegisterClassExA(&wcex);
return a;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
g_hInstance = hInstance; // Store instance handle in our global variable
hWnd = CreateWindowA("TEST_CLASS", "TEST_WINDOW", WS_OVERLAPPEDWINDOW,
0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
SendMessageW(hWnd, WM_USER, 111, 0);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CREATE:
OutputDebugStringA("Create called.\n");
break;
case WM_USER:
{
if (wParam == 111)
{
OutputDebugStringA("User called.\n");
}
}
break;
case WM_DESTROY:
OutputDebugStringA("Destroy called.\n");
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
调试输出:
创建调用。 用户调用。 销毁调用。 程序“[2152] Test.exe: Native”已退出,代码为 0 (0x0)。
【问题讨论】:
-
应用程序是否创建窗口?
-
是的,当然要创建窗口。
-
它是用什么语言编写的?它使用什么框架?它是如何创建它的主窗口的?
-
这是不可能的,只是你还没有发现它是如何做到的。要么是 DOS 应用程序,要么是 DOS 应用程序,您将命令窗口误认为是 Windows 窗口。
-
它在不显示窗口的情况下运行并完成。我看不到任何消息处理。
WM_CREATE作为CreateWindow的结果传递给您的WindowProc,但这并不意味着您有一个消息循环。
标签: c windows winapi message-queue