欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

第七章 鼠标(CONNECT)

程序员文章站 2022-10-11 17:05:51
1 /* 2 CONNECT.C -- Connect-the-Dots Mouse Demo Program 3 (c) Charles Petzold,1998 4 */ 5 6 #include 7 8 #define MAXPOINTS 1000 9 10 LRESU ......
第七章 鼠标(CONNECT)
  1 /*
  2 CONNECT.C -- Connect-the-Dots Mouse Demo Program
  3             (c) Charles Petzold,1998
  4 */
  5 
  6 #include <Windows.h>
  7 
  8 #define MAXPOINTS 1000
  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("Connect");
 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("Connect-the-Points Mouse 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 POINT pt[MAXPOINTS];
 61     static int iCount;
 62     HDC hdc;
 63     int i, j;
 64     PAINTSTRUCT ps;
 65 
 66     switch (message)
 67     {
 68     case WM_LBUTTONDOWN:
 69         iCount = 0;
 70         InvalidateRect(hwnd, NULL, TRUE);
 71         return 0;
 72 
 73     case WM_MOUSEMOVE:
 74         if (wParam & MK_LBUTTON && iCount < 1000)
 75         {
 76             pt[iCount  ].x = LOWORD(lParam);
 77             pt[iCount++].y = HIWORD(lParam);
 78             
 79             hdc = GetDC(hwnd);
 80             SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0);
 81             ReleaseDC(hwnd, hdc);
 82         }
 83         return 0;
 84 
 85     case WM_LBUTTONUP:
 86         InvalidateRect(hwnd, NULL, FALSE);
 87         return 0;
 88 
 89     case WM_PAINT:
 90         hdc = BeginPaint(hwnd, &ps);
 91 
 92         SetCursor(LoadCursor(NULL, IDC_WAIT));
 93         ShowCursor(TRUE);
 94 
 95         for (i = 0; i < iCount - 1; ++i)
 96             for (j = i + 1; j < iCount; ++j)
 97             {
 98                 MoveToEx(hdc, pt[i].x, pt[i].y, NULL);
 99                 LineTo(hdc, pt[j].x, pt[j].y);
100             }
101 
102         ShowCursor(FALSE);
103         SetCursor(LoadCursor(NULL, IDC_ARROW));
104 
105         EndPaint(hwnd, &ps);
106         return 0;
107 
108     case WM_DESTROY:
109         PostQuitMessage(0);
110         return 0;
111     }
112 
113     return DefWindowProc(hwnd, message, wParam, lParam);
114 }
CONNECT.C

 第七章 鼠标(CONNECT)