#include<windows.h>LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrecInstance,LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR szName[]=TEXT("Keybord test");
WNDCLASS wndclass;
HWND hwnd;
MSG msg; wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc= WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon= LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor= LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szName ; if(!RegisterClass(&wndclass))
{
MessageBox(hwnd,szName,TEXT("sdfsf"),MB_OK);
return 0;
} hwnd = CreateWindow (szName, TEXT ("What Size is the Window?"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ; ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
while(!GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;

}LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
HPEN hp; bool upf=false;
bool shif=false;
bool ctrf=false;
bool ac=false;
bool bf=false; static TCHAR up[]=TEXT("you have hit the up key");
static TCHAR shift[]=TEXT("you have hit the shift key");
static TCHAR ctr[]=TEXT("you have hit the ctrl key");
static TCHAR ctra[]=TEXT("you have hit ctrl and a key");
static TCHAR shifb[]=TEXT("you have hit shift and b key"); switch(iMsg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP:upf=true;break;
case VK_CONTROL:ctrf=true;break;
case VK_SHIFT:shif=true;break;
default:break;
}
break;
}
case WM_KEYUP:
InvalidateRect(hwnd,NULL,FALSE);
break;
case WM_CHAR:
if(ctrf)
if(wParam==65)
{
ctrf=false;
ac=true;
}
if(shif)
if(wParam==66)
{
shif=false;
bf=true;
}
break;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
SetTextColor(hdc,RGB(255,0,0));
if(upf)
{
TextOut(hdc,0,0,up,lstrlen(up));
upf=false;
}
if(shif)
{
TextOut(hdc,0,0,shift,lstrlen(shift));
shif=false;
}
if(ctrf)
{
TextOut(hdc,0,0,ctr,lstrlen(ctr));
ctrf=false;
}
if(ac)
{
TextOut(hdc,0,0,ctra,lstrlen(ctra));
ac=false;
}
if(bf)
{
TextOut(hdc,0,0,shifb,lstrlen(shifb));
bf=false;
}
EndPaint(hwnd,&ps);
break;
default: DefWindowProc(hwnd,iMsg,wParam,lParam);break;
}
return 0;}

解决方案 »

  1.   

    if(!RegisterClass(&wndclass))
    {
    MessageBox(hwnd,szName,TEXT("sdfsf"),MB_OK);
    return 0;
    }你的CreateWindows都没调用,hwnd都无效,它弹到哪去啊?
      

  2.   

    while(!GetMessage(&msg,NULL,0,0))
    -->
    while(GetMessage(&msg,NULL,0,0))default: DefWindowProc(hwnd,iMsg,wParam,lParam);break;
    -->
    default: 
       return DefWindowProc(hwnd,iMsg,wParam,lParam);
      

  3.   

    default: DefWindowProc(hwnd,iMsg,wParam,lParam);break;这句 还有最后的renturn 0;在窗口过程中的return 0表示消息你已经处理过了,不需要系统处理了,而你的程序并没有处理WM_CREATE消息,而又不让系统处理,所以当然弹不出来改成
    default:  
      return DefWindowProc(hwnd,iMsg,wParam,lParam);