首页 > 代码库 > SDK源码分析 3

SDK源码分析 3

  1 #include <windows.h>
  2 #define MAXPOINTS 1000
  3 
  4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);//窗口过程
  5 
  6 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  7 {
  8     WNDCLASS wndclass;
  9     MSG msg;
 10     HWND hWnd;
 11     TCHAR Name_1[] = TEXT("MyClass"), Name_2[] = TEXT("MyWindows");
 12 
 13     //设置类风格
 14     wndclass.cbClsExtra = 0;
 15     wndclass.cbWndExtra = 0;
 16     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 17     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 18     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 19     wndclass.hInstance = hInstance;
 20     wndclass.lpfnWndProc = WndProc;
 21     wndclass.lpszClassName = Name_1;
 22     wndclass.lpszMenuName = NULL;
 23     wndclass.style = CS_HREDRAW | CS_VREDRAW;
 24 
 25     RegisterClass(&wndclass);
 26 
 27     hWnd = CreateWindow(Name_1, Name_2, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL,
 28         NULL, hInstance, NULL);
 29 
 30     UpdateWindow(hWnd);
 31     ShowWindow(hWnd, iCmdShow);
 32 
 33     //消息循环
 34     while (GetMessage(&msg, NULL, NULL, NULL))
 35     {
 36         TranslateMessage(&msg);//翻译键盘消息
 37         DispatchMessage(&msg);//转发消息给 WndProc
 38     }
 39     return msg.wParam;
 40 }
 41 
 42 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 43 {
 44     static POINT pt[MAXPOINTS];//定义 POINT 坐标类型 数组大小1000
 45     static int  iCound;//记录变量
 46     int i, j;
 47     PAINTSTRUCT ps;
 48     HDC hdc;
 49 
 50     switch (msg)
 51     {
 52     case WM_LBUTTONDOWN://当鼠标按下之后 记录归零 重画窗口 背景清除
 53         iCound = 0;
 54         InvalidateRect(hWnd, NULL, TRUE);
 55         return 0;
 56     case WM_MOUSEMOVE://当鼠标移动的时候
 57         //消息 & MK_LBUTTON 并且 记录小于 1000
 58         // 如果现在是按下状态 并且 记录 <1000
 59         if (wParam & MK_LBUTTON && iCound < 1000)
 60         {
 61             //每次保存 记录 并且累加记录
 62             pt[iCound].x = LOWORD(lParam);
 63             pt[iCound++].y = HIWORD(lParam);
 64 
 65             hdc = GetDC(hWnd);
 66             SetPixel(hdc, LOWORD(lParam), HIWORD(lParam), 0);//画出一个个像素点
 67             ReleaseDC(hWnd, hdc);
 68         }
 69         return 0;
 70     case WM_LBUTTONUP:
 71         InvalidateRect(hWnd, NULL, FALSE);//当鼠标松开 重绘窗口 不擦除 背景
 72         return 0;
 73     case WM_PAINT:
 74         hdc=BeginPaint(hWnd, &ps);
 75 
 76         //显示忙碌鼠标
 77         SetCursor(LoadCursor(NULL, IDC_WAIT));
 78         ShowCursor(TRUE);
 79         
 80         //i=0;i<999;i++
 81         //j=i+1;j<1000;j++
 82         //画出 点.
 83         /*
 84         比如 当前 循环一次 i=0;i<999;
 85         j=1;j<1000;
 86         画: pt[0].x pt[0].y
 87         到: pt[1].x pt[1].y
 88         
 89         当前 循环 7次 i=6;i<999;
 90         j=7;j<1000;
 91         画: pt[6].x pt[6].y
 92         到: pt[7].x pt[7].y
 93         */
 94         for (i = 0; i < iCound - 1; i++)
 95         {
 96             for (j = i + 1; j < iCound; j++)
 97             {
 98                 MoveToEx(hdc, pt[i].x, pt[i].y, NULL);
 99                 LineTo(hdc, pt[j].x, pt[j].y);
100             }
101         }
102         //更换鼠标.
103         ShowCursor(FALSE);
104         SetCursor(LoadCursor(NULL, IDC_ARROW));
105         EndPaint(hWnd, &ps);
106         return 0;
107     case WM_DESTROY:
108         PostQuitMessage(0);//程序结束
109         return 0;
110 
111     }
112     return DefWindowProc(hWnd, msg, wParam, lParam);//默认给Windows 处理 多余的消息
113 }

技术分享

SDK源码分析 3