#include <windows.h>
/*
问题:
按下键盘a,为什么先弹了第二处的MessageBox,再弹出第一处的MessageBox
自己调试过,流程好像没问题,是先调了第一处,再调第二处。只是弹框的顺序反了
*/
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wndClass = {0};
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName = TEXT("keyTest");
RegisterClass(&wndClass);
HWND hwnd = CreateWindow(TEXT("keyTest"), TEXT("keyBoard"),
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WinProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_KEYDOWN: //输入法得切换英文
{
TCHAR szBuff[MAXBYTE] = { 0 };
if (wParam >= 'A' && wParam <= 'Z')
{
wsprintf(szBuff, TEXT("OnKeyDown: %c\r\n"), wParam);
MessageBox(hwnd, szBuff, NULL, MB_OK); //第一处
}
OutputDebugString(szBuff);
return 0;
}
case WM_CHAR:
{
TCHAR szBuff[MAXBYTE] = { 0 };
if (wParam >= 'a' && wParam <= 'z')
{
wsprintf(szBuff, TEXT("OnChar: %c\r\n"), wParam);
MessageBox(hwnd, szBuff, NULL, MB_OK);//第二处
}
OutputDebugString(szBuff);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
所以现在的运行结果是先出来第二处的对话框,然后是第一处的对话框。这个问题我觉得应该是和模态对话框有关,不知道这个模态对话框是如何运行的?