首页 > 代码库 > Windows

Windows

 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 instance13 HINSTANCE hPrevInstance,     // handle to previous instance14 LPSTR lpCmdLine,      // command line15 int nCmdShow        // show state16 )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 window56 UINT uMsg,      // message identifier57 WPARAM wParam,   // first message parameter58 LPARAM lParam   // second message parameter59 )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 }