Windows 程序 HelloWorld
1 #include <windows.h> 2 #include <stdio.h> 3 4 LRESULT CALLBACK WinSunProc( 5 HWND hwnd, // handle to window 6 UINT uMsg, // message identifier 7 WPARAM wParam, // first message parameter 8 LPARAM lParam // second message parameter 9 ); 10 11 int WINAPI WinMain( 12 HINSTANCE hInstance, // handle to current instance 13 HINSTANCE hPrevInstance, // handle to previous instance 14 LPSTR lpCmdLine, // command line 15 int nCmdShow // show state 16 ) 17 { 18 19 WNDCLASS wndcls; 20 wndcls.cbClsExtra=0; 21 wndcls.cbWndExtra=0; 22 wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); 23 wndcls.hCursor=LoadCursor(NULL,IDC_ARROW); 24 wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION); 25 wndcls.hInstance=hInstance; //应用程序实例句柄由 WinMain 函数传进来 26 wndcls.lpfnWndProc=WinSunProc; 27 wndcls.lpszClassName="xx"; 28 wndcls.lpszMenuName=NULL; 29 wndcls.style=CS_HREDRAW | CS_VREDRAW; 30 31 //注册窗口类 32 RegisterClass(&wndcls); 33 34 //创建窗口 35 HWND hwnd; 36 hwnd=CreateWindow("xx","第一个窗口", 37 WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL); 38 39 //显示窗口和更新窗口 40 ShowWindow(hwnd,SW_SHOWNORMAL); 41 UpdateWindow(hwnd); 42 43 //定义消息结构体,进行消息循环 44 MSG msg; 45 while(GetMessage(&msg,NULL,0,0)) 46 { 47 TranslateMessage(&msg); 48 DispatchMessage(&msg); 49 } 50 51 return msg.wParam ; 52 } 53 54 LRESULT CALLBACK WinSunProc( 55 HWND hwnd, // handle to window 56 UINT uMsg, // message identifier 57 WPARAM wParam, // first message parameter 58 LPARAM lParam // second message parameter 59 ) 60 { 61 switch(uMsg) 62 { 63 case WM_CHAR: 64 char szChar[20]; 65 sprintf(szChar,"char code is %d",wParam); 66 MessageBox(hwnd,szChar,"char",0); 67 break; 68 case WM_LBUTTONDOWN: 69 MessageBox(hwnd,"m ouse clicked","message",0); 70 HDC hdc; 71 hdc=GetDC(hwnd); //不能在响应 WM_PAINT 消息时调用 72 TextOut(hdc,0,50,"点击鼠标之后产生的字符串",strlen("点击鼠标之后产生的字符串")); 73 ReleaseDC(hwnd,hdc); 74 break; 75 case WM_PAINT: 76 HDC hDC; 77 PAINTSTRUCT ps; 78 hDC=BeginPaint(hwnd,&ps); //BeginPaint 只能在响应 WM_PAINT 消息时调用 79 TextOut(hDC,0,0,"第一个窗口绘制成功",strlen("第一个窗口绘制成功")); 80 EndPaint(hwnd,&ps); 81 break; 82 case WM_CLOSE: 83 if(IDYES==MessageBox(hwnd," 是否真的结束?","message",MB_YESNO)) 84 { 85 DestroyWindow(hwnd); 86 } 87 break; 88 case WM_DESTROY: 89 PostQuitMessage(0); 90 break; 91 default: 92 return DefWindowProc(hwnd,uMsg,wParam,lParam); 93 } 94 return 0; 95 }