耐得住寂寞,禁得起诱惑,这就是程序人生
步骤:
1.在WinMain中定义各种变量
2.注册窗口类RegisterClass
3.创建窗口CreateWindow
4.显示窗口和更新窗口
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
5.消息循环
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
1.在WinMain中定义各种变量
2.注册窗口类RegisterClass
3.创建窗口CreateWindow
4.显示窗口和更新窗口
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
5.消息循环
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
1 #include <windows.h> 2 3 4 5 LRESULT CALLBACK MyProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam); 6 7 8 9 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) 10 11 { 12 13 MSG msg; 14 15 HWND hwnd; 16 17 static TCHAR szAppName[] = "hl"; 18 19 20 21 WNDCLASS wndclass; 22 23 wndclass.style = CS_HREDRAW | CS_VREDRAW; 24 25 wndclass.cbClsExtra = 0; 26 27 wndclass.cbWndExtra = 0; 28 29 wndclass.lpfnWndProc = MyProc; 30 31 wndclass.hInstance = hInstance; 32 33 wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); 34 35 wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); 36 37 wndclass.hbrBackground= (HBRUSH)GetStockObject(WHITE_BRUSH); 38 39 wndclass.lpszMenuName = NULL; 40 41 wndclass.lpszClassName= szAppName; 42 43 44 45 if(!RegisterClass(&wndclass)) 46 47 { 48 49 MessageBox(NULL,TEXT("error"),TEXT("title"),MB_ICONERROR); 50 51 return 0; 52 53 } 54 55 hwnd = CreateWindow(szAppName, 56 57 TEXT("Hello"), 58 59 WS_OVERLAPPEDWINDOW, 60 61 CW_USEDEFAULT, 62 63 CW_USEDEFAULT, 64 65 CW_USEDEFAULT, 66 67 CW_USEDEFAULT, 68 69 NULL, 70 71 NULL, 72 73 hInstance, 74 75 NULL 76 77 ); 78 79 ShowWindow(hwnd,nShowCmd); 80 81 UpdateWindow(hwnd); 82 83 84 85 while(GetMessage(&msg,hwnd,0,0)) 86 87 { 88 89 TranslateMessage(&msg); 90 91 DispatchMessage(&msg); 92 93 } 94 95 return msg.wParam; 96 97 } 98 99 100 101 LRESULT CALLBACK MyProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam) 102 103 { 104 105 106 107 switch(message) 108 109 { 110 111 case WM_DESTROY: 112 113 PostQuitMessage(0); 114 115 return 0; 116 117 } 118 119 return DefWindowProc(hwnd,message,wParam,lParam); 120 121 }