当我创建一个新的Win32应用程序时,我注意到以下函数:
当某个地方调用函数PostMessage或SendMessage时,您将收到消息,并且我注意到函数WndProc可以接收消息,因为有一个注册它的函数。
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
当某个地方调用函数PostMessage或SendMessage时,您将收到消息,并且我注意到函数WndProc可以接收消息,因为有一个注册它的函数。
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SCREENCAPTURE));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_SCREENCAPTURE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
注意: wcex.lpfnWndProc = WndProc;
我想了解PostMessage()的机制以及如何接收它,因此我创建了一个C++控制台应用程序,以查看是否可以注册函数WndProc,以下是我的尝试代码:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int a = 1;//Break point here to see if it's call
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
return RegisterClassEx(&wcex);
}
void main()
{
MyRegisterClass(GetModuleHandle(NULL));//(HINSTANCE)GetConsoleWindow()
PostMessage(GetConsoleWindow(), 132, 0, 0);
SYSTEMTIME st;
while (true)
{
GetSystemTime(&st);
printf("I'm wanting and waiting and waiting :(, The time is %I64u \n", st.wMilliseconds);
}
}