windows应用程序【二】创建窗口

版权声明:欢迎转载请注明转自方辰昱的博客https://blog.csdn.net/viafcccy https://blog.csdn.net/viafcccy/article/details/85270805

我们在创建窗口时需要的过程 

关于消息循环

因为处理器同时只能执行一个程序 因此我们需要操作系统去调度程序 因此我们只是将消息传给操作系统 等待回应从而由操作系统来显示我们需要的窗口

wndproc()

窗口过程负责用来响应某一类窗口收到的各种Windows消息

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static char szWndClassName[] = "hellowin";
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

     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 = szWndClassName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, "注册失败", 
                      "错误", MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szWndClassName,             // window class name
                          "窗口标题",			      // window caption
                          WS_OVERLAPPEDWINDOW,        // window style
                          CW_USEDEFAULT,              // initial x position
                          CW_USEDEFAULT,              // initial y position
                          CW_USEDEFAULT,              // initial x size
                          CW_USEDEFAULT,              // initial y size
                          NULL,                       // parent window handle
                          NULL,                       // window menu handle
                          hInstance,                  // program instance handle
                          NULL) ;                     // creation parameters
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))  //消息队列
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;  //WM_QUIT
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	 HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;

     switch (message)
     {
	  case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          
          GetClientRect (hwnd, &rect) ;
          
          DrawText (hdc, TEXT ("在屏幕中心输出文字"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
          
          EndPaint (hwnd, &ps) ;
          return 0 ;
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

最后对于我们为什要对一个窗口定义很多遍

第一遍第一类时类似于定义了一个基类 也就像确定了你是大学生具有所有大学生的特点

但是第二次创建窗口时 会说明你是哪个专业的 性格是什么 

猜你喜欢

转载自blog.csdn.net/viafcccy/article/details/85270805