写了个例子,测试了一下,发现出现你这种情况的可能就是在DispatchMessage前面没有先TranslateMessage,而TranslateMessage就是检查WM_KEYDOWN消息是否是一个char消息,如果是一个char消息,就会向消息队例里放一个WM_CHAR.
程序如下,可以分别试试无模式和模式的。
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;
BOOL InitInstance(HINSTANCE, int);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
// {
TranslateMessage(&msg);
DispatchMessage(&msg);
// }
}
return msg.wParam;
}
LRESULT CALLBACK dlgproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hwnd, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd =
// DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX),NULL, (DLGPROC ) dlgproc);
CreateDialog(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX),NULL, (DLGPROC ) dlgproc);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}