码迷,mamicode.com
首页 > 其他好文 > 详细

SDK源码分析 7

时间:2016-10-25 14:38:14      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:起点   自定义   style   switch   false   param   lap   hwnd   tchar   

  1 #include <windows.h>
  2 
  3 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  4 
  5 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPervInstance, PSTR szCmdLine, int iCmdShow)
  6 {
  7     WNDCLASS wndclass;
  8     MSG msg;
  9     HWND hWnd;
 10     TCHAR Name_1[] = TEXT("MyClass"), Name_2[] = TEXT("MyWindows");
 11 
 12     wndclass.cbClsExtra = 0;
 13     wndclass.cbWndExtra = 0;
 14     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 15     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 16     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
 17     wndclass.hInstance = hInstance;
 18     wndclass.lpfnWndProc = WndProc;
 19     wndclass.lpszClassName = Name_1;
 20     wndclass.lpszMenuName = NULL;
 21     wndclass.style = CS_VREDRAW | CS_HREDRAW;
 22 
 23     RegisterClass(&wndclass);
 24     hWnd = CreateWindow(Name_1, Name_2, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
 25         NULL, NULL, hInstance, NULL);
 26 
 27     ShowWindow(hWnd, iCmdShow);
 28     UpdateWindow(hWnd);
 29 
 30     while (GetMessage(&msg, NULL, NULL, NULL))
 31     {
 32         TranslateMessage(&msg);
 33         DispatchMessage(&msg);
 34     }
 35     return msg.wParam;
 36 }
 37 void DrawBoxOutline(HWND hWnd, POINT ptBeg, POINT ptEnd)//自定义函数参数3个
 38 {
 39     HDC hdc;
 40     hdc = GetDC(hWnd);
 41 
 42     SetROP2(hdc, R2_NOT);//设置翻转颜色
 43     SelectObject(hdc, GetStockObject(NULL_BRUSH));//构造空刷子
 44     Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);//构造矩形
 45 
 46     ReleaseDC(hWnd, hdc);
 47 }
 48 
 49 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 50 {
 51     static BOOL fBlocking, fValidBox;
 52     static POINT ptBeg, ptEnd, ptBoxBeg, ptBoxEnd;
 53     HDC hdc;
 54     PAINTSTRUCT ps;
 55 
 56     switch (msg)
 57     {
 58     case WM_LBUTTONDOWN:
 59         ptBeg.x = ptEnd.x = LOWORD(lParam);//保存起点终点
 60         ptBeg.y = ptEnd.y = HIWORD(lParam);//保存起点终点
 61 
 62         DrawBoxOutline(hWnd, ptBeg, ptEnd);//画矩形 此时无矩形 为一个点
 63 
 64         SetCapture(hWnd);//捕获鼠标
 65         SetCursor(LoadCursor(NULL, IDC_CROSS));//设置鼠标为十字
 66         fBlocking = TRUE;//画虚线矩形 状态为真
 67         return 0;
 68     case WM_MOUSEMOVE:
 69         if (fBlocking)//如果状态为真
 70         {
 71             SetCursor(LoadCursor(NULL, IDC_CROSS));//设置鼠标为十字
 72             DrawBoxOutline(hWnd, ptBeg, ptEnd);//把上一个矩形画为 白色
 73 
 74             ptEnd.x = LOWORD(lParam);//保存鼠标最后x位置
 75             ptEnd.y = HIWORD(lParam);//保存鼠标最后y位置
 76             DrawBoxOutline(hWnd, ptBeg, ptEnd);//画心矩形
 77             //移动停止 并且放开 只画出最后一个矩形
 78         }
 79         return 0;
 80     case WM_LBUTTONUP:
 81         if (fBlocking)//如果状态为真
 82         {
 83             DrawBoxOutline(hWnd, ptBeg, ptEnd);//画出矩形
 84 
 85             ptBoxBeg = ptBeg;//保存开始坐标
 86             ptBoxEnd.x = LOWORD(lParam);//保存最后鼠标位置x
 87             ptBoxEnd.y = HIWORD(lParam);//保存最后鼠标位置y
 88 
 89             ReleaseCapture();//释放捕获
 90             SetCursor(LoadCursor(NULL, IDC_ARROW));//设置鼠标正常
 91             fBlocking = FALSE;//状态设置假
 92             fValidBox = TRUE;//构造实心矩形状态 为真
 93             InvalidateRect(hWnd, NULL, TRUE);//立即重绘背景 擦除背景
 94         }
 95         return 0;
 96     case WM_CHAR:
 97         if (fBlocking&(wParam == \x1B))//如果状态是虚线 且 按下ESC 
 98         {
 99             DrawBoxOutline(hWnd, ptBeg, ptEnd);//画矩形
100             ReleaseCapture();//释放捕获
101             SetCursor(LoadCursor(NULL, IDC_ARROW));//设置正常鼠标
102             fBlocking = FALSE;//状态为 假
103         }
104         //实际刷不出 因为位置都没用被赋值。
105         return 0;
106     case WM_PAINT:
107         hdc=BeginPaint(hWnd, &ps);
108         
109         if (fValidBox)//如果状态为实心矩形
110         {
111             SelectObject(hdc, GetStockObject(BLACK_BRUSH));//设置画刷为 实心
112             Rectangle(hdc, ptBoxBeg.x, ptBoxBeg.y, ptBoxEnd.x, ptBoxEnd.y);//画矩形
113         }
114 
115         if (fBlocking)//状态是虚线
116         {
117             SetROP2(hdc, R2_NOT);//设置翻转
118             SelectObject(hdc, GetStockObject(NULL_BRUSH));//设置为空画刷
119             Rectangle(hdc, ptBeg.x, ptBeg.y, ptEnd.x, ptEnd.y);//构造矩形
120         }
121         EndPaint(hWnd, &ps);
122         return 0;
123     case WM_DESTROY:
124         PostQuitMessage(0);
125         return 0;
126     }
127 
128     return DefWindowProc(hWnd, msg, wParam, lParam);
129 }

 

SDK源码分析 7

标签:起点   自定义   style   switch   false   param   lap   hwnd   tchar   

原文地址:http://www.cnblogs.com/summergull/p/5996264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!