第七章 鼠标(CHECKER1)

时间:2019-11-26 15:58来源:澳洲幸运10
CHECKE瑞鹰1程序将客商细分分为22个矩形,构成叁个5*5的数组。即便在中间一个矩形内单击鼠标,就用X形填充该矩形。再度单击,则X形消失。 1 /*-------------------------------------------- 2 CHE

CHECKE瑞鹰1程序将客商细分分为22个矩形,构成叁个5*5的数组。即便在中间一个矩形内单击鼠标,就用X形填充该矩形。再度单击,则X形消失。

图片 1图片 2

  1 /*--------------------------------------------
  2 CHECKER1.C -- Mouse Hit-Test Demo Program No.1
  3               (c) Charles Petzold, 1998
  4 --------------------------------------------*/
  5 
  6 #include <Windows.h>
  7 
  8 #define DIVISION 5
  9 
 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
 11 
 12 int WINAPI WinMain( __in HINSTANCE hInstance
 13                     , __in_opt HINSTANCE hPrevInstance
 14                     , __in LPSTR lpCmdLine
 15                     , __in int nShowCmd )
 16 {
 17     static TCHAR szAppName[] = TEXT("Checker1");
 18     HWND hwnd;
 19     MSG msg;
 20     WNDCLASS wndclass;
 21 
 22     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 23     wndclass.lpfnWndProc = WndProc;
 24     wndclass.cbClsExtra = 0;
 25     wndclass.cbWndExtra = 0;
 26     wndclass.hInstance = hInstance;
 27     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 28     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 29     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 30     wndclass.lpszMenuName = NULL;
 31     wndclass.lpszClassName = szAppName;
 32 
 33     if (!RegisterClass(&wndclass))
 34     {
 35         MessageBox(NULL, TEXT("Program requires Windows NT!")
 36             , szAppName, MB_ICONERROR);
 37         return 0;
 38     }
 39 
 40     hwnd = CreateWindow(szAppName, TEXT("Checker1 Mouse Hit-Test Demo")
 41         , WS_OVERLAPPEDWINDOW
 42         , CW_USEDEFAULT, CW_USEDEFAULT
 43         , CW_USEDEFAULT, CW_USEDEFAULT
 44         , NULL, NULL, hInstance, NULL);
 45 
 46     ShowWindow(hwnd, nShowCmd);
 47     UpdateWindow(hwnd);
 48 
 49     while (GetMessage(&msg, NULL, 0, 0))
 50     {
 51         TranslateMessage(&msg);
 52         DispatchMessage(&msg);
 53     }
 54 
 55     return msg.wParam;
 56 }
 57 
 58 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 59 {
 60     static BOOL fState[DIVISION][DIVISION];
 61     static int cxBlock, cyBlock;
 62     HDC hdc;
 63     int x, y;
 64     PAINTSTRUCT ps;
 65     RECT rect;
 66 
 67     switch (message)
 68     {
 69     case WM_SIZE:
 70         cxBlock = LOWORD(lParam) / DIVISION;
 71         cyBlock = HIWORD(lParam) / DIVISION;
 72         return 0;
 73 
 74     case WM_LBUTTONDOWN:
 75         x = LOWORD(lParam) / cxBlock;
 76         y = HIWORD(lParam) / cyBlock;
 77 
 78         if (x < DIVISION && y < DIVISION)
 79         {
 80             fState[x][y] ^= 1;
 81 
 82             rect.left = x * cxBlock;
 83             rect.top = y * cyBlock;
 84             rect.right = (x + 1) * cxBlock;
 85             rect.bottom = (y + 1) * cyBlock;
 86 
 87             InvalidateRect(hwnd, &rect, FALSE);
 88         }
 89         else
 90             MessageBeep(0);
 91         return 0;
 92 
 93     case WM_PAINT:
 94         hdc = BeginPaint(hwnd, &ps);
 95 
 96         for (x = 0; x < DIVISION; ++x)
 97             for (y = 0; y < DIVISION; ++y)
 98             {
 99                 Rectangle(hdc, x * cxBlock, y * cyBlock
100                     , (x + 1) * cxBlock, (y + 1) * cyBlock);
101 
102                 if (fState[x][y])
103                 {
104                     MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL);
105                     LineTo(hdc, (x + 1) * cxBlock, (y + 1) * cyBlock);
106                     MoveToEx(hdc, x * cxBlock, (y + 1) * cyBlock, NULL);
107                     LineTo(hdc, (x + 1) * cxBlock, y * cyBlock);
108                 }
109             }
110 
111         EndPaint(hwnd, &ps);
112         return 0;
113 
114     case WM_DESTROY:
115         PostQuitMessage(0);
116         return 0;
117     }
118 
119     return DefWindowProc(hwnd, message, wParam, lParam);
120 }

CHECKER1.C

CHECKE奥迪Q51程序呈现结果如图所示:

图片 3

不无贰十一个矩形具备相像的宽度和中度,那一个宽度和惊人被保留在cxBlock、cyBlock中。无论哪一天转移顾客区的朗朗上口,程序都将再也计算cxBlock、cyBlock。WM_LBUTTONDOWN管理逻辑利用鼠标的坐标来判别哪些矩形被单击,然后在fState数组中行使位运算符(按位异或^卡塔 尔(阿拉伯语:قطر‎总括那多少个矩形的新境况,最后强制使该矩形失效,进而发出WM_PAINT消息。

若是客商区的上升的幅度和中度无法被5整除,客户区的左侧或底层就可以情不自禁二个小长条区域,不被举办覆盖。在这里片区域扩充鼠标单击时,CHECKEKoleos1程序会调用MessageBeep函数实行响应。

当CHECKE冠道1程序收到WM_PAINT信息时,它会再度绘制整个顾客区,利用GDI中的Rectangle函数画出全数的矩形。若fState值为真,则CHECKE中华V1程序调用函数MoveToEx和函数LineTo函数画出两条直线。

编辑:澳洲幸运10 本文来源:第七章 鼠标(CHECKER1)

关键词: