windows程序设计 例题解析 CHECKER3.C

/*-------------------------------------------------
   CHECKER3.C -- Mouse Hit-Test Demo Program No. 3
                 (c) Charles Petzold, 1998
  -------------------------------------------------*/

#include

#define DIVISIONS 5

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

TCHAR szChildClass[] = TEXT ("Checker3_Child") ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("Checker3") ;
     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 = szAppName ;
    
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
    
     wndclass.lpfnWndProc   = ChildWndProc ;
     wndclass.cbWndExtra    = sizeof (long) ;
     wndclass.hIcon         = NULL ;
     wndclass.lpszClassName = szChildClass ;
    
     RegisterClass (&wndclass) ;
    
     hwnd = CreateWindow (szAppName, TEXT ("Checker3 Mouse Hit-Test Demo"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
    
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
    
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static HWND hwndChild[DIVISIONS][DIVISIONS] ;
     int         cxBlock, cyBlock, x, y ;
    
     switch (message)
     {
     case WM_CREATE :
          for (x = 0 ; x < DIVISIONS ; x++)                      //创建25个子窗口(5行5列)
               for (y = 0 ; y < DIVISIONS ; y++)
                    hwndChild[x][y] = CreateWindow (szChildClass, NULL,
                              WS_CHILDWINDOW | WS_VISIBLE,
                              0, 0, 0, 0,      //注意对0,0,0,0的理解,应该初始化的时候,在WM_CREASTE中还不知道主窗口的大小,也就更不知道子窗口的大小,
                          //只是临时指定,在wm_size消息中,通过movewindow就真正确定了窗口的大小
                           
                              hwnd, (HMENU) (y << 8 | x),           //子窗口标识符,确定唯一的窗口
                              (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), //获取应用程序实例句柄,可以使用全局的hInstance
                              NULL) ;

//对于这个嵌套循环,每创建一个子窗口,都会想子窗口产生一个WM_CREATE消息,也就是说,在这个嵌套循环中一共要调用25次ChildWndProc子窗口过程
//对每个子窗口产生的WM_CREATE消息进行处理
          return 0 ;
              
     case WM_SIZE :
          cxBlock = LOWORD (lParam) / DIVISIONS ;
          cyBlock = HIWORD (lParam) / DIVISIONS ;
         
          for (x = 0 ; x < DIVISIONS ; x++)
                for (y = 0 ; y < DIVISIONS ; y++)              //指定每一个子窗口的位置
                    MoveWindow (hwndChild[x][y],
                                x * cxBlock, y * cyBlock,
                                cxBlock, cyBlock, true) ;
//特别要注意movewindow这个语句,如果不清楚,你就无法知道程序的执行流程,只能默默糊糊了解大概;
//movewindow重新指定每个子窗口在主窗口客户区的位置和每个子窗口的大小
//当重新移动每一个子窗口时,子窗口无效,若最后一个参数为true,则会产生WM_PAINT消息,(这wm_paint消息是传个子窗口过程的),
//这本例中参数为true,也就是说,对于每一个子窗口无效时,都会调用ChildWndProc子窗口过程,对WM_PAINT消息进行处理,绘出每个子窗口图形
//
//当重新移动每一个子窗口时,子窗口无效,若最后一个参数为false,则不会产生WM_PAINT消息,读者可以把参数改为false,就会发现,窗口了上什么都没有

          return 0 ;
              
     case WM_LBUTTONDOWN :
          MessageBeep (0) ;
          return 0 ;
         
     case WM_DESTROY :
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

LRESULT CALLBACK ChildWndProc (HWND hwnd, UINT message,
                               WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;
    
     switch (message)
     {
     case WM_CREATE :
          SetWindowLong (hwnd, 0, 0) ;       //用零填充额外的空间
          return 0 ;
         
     case WM_LBUTTONDOWN :
          SetWindowLong (hwnd, 0, 1 ^ GetWindowLong (hwnd, 0)) ;//对额外的空间里的值取反,判断是否单击了子窗口(异或操作,相同为0,不同为1)
          InvalidateRect (hwnd, NULL, FALSE) ;
          return 0 ;
         
    case WM_PAINT :
          hdc = BeginPaint (hwnd, &ps) ;
         
          GetClientRect (hwnd, &rect) ; //每一个子窗口的大小
          Rectangle (hdc, 0, 0, rect.right, rect.bottom) ; //画一个子窗口
         
          if (GetWindowLong (hwnd, 0))              //当任何一个子窗口按下,则显示交叉的对角线,或取消对角线
          {
               MoveToEx (hdc, 0,          0, NULL) ;
               LineTo   (hdc, rect.right, rect.bottom) ;
               MoveToEx (hdc, 0,          rect.bottom, NULL) ;
               LineTo   (hdc, rect.right, 0) ;
          }
         
          EndPaint (hwnd, &ps) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

 

总结下程序执行流程:

                       

                 1.主窗口产生WM_CREATE消息,调用主窗口窗口过程对wm_create消息进行处理

                 2.主窗口窗口过程对wm_create消息进行处理,每次循环调用了createwindow函数,该函数产生WM_create消息(调用子窗口的窗口过程进行处理,一共25次)

                 3.主窗口产生WM_SIZE消息,调用主窗口窗口过程进行处理

                 4.主窗口窗口过程对WM_SIZE进行处理时,调用movewindow函数,该函数每次循环产生一个WM_PAINT

                    消息(调用子窗口的窗口过程进行处理,一共25次

                5.接下来就是处理鼠标事件了

你可能感兴趣的:(windows程序设计)