Chapter I:在WM_MOUSEMOVE中绘制.
- void DrawDragRect(HDC hDC, RECT& rc)
- {
- for (int j=rc.top; j<rc.bottom; j++)
- {
- if (j%2==0)
- SetPixel(hDC, rc.left, j, RGB(255, 0, 0));
- }
- for (int i=rc.left; i<rc.right; i++)
- {
- if (i%2==0)
- SetPixel(hDC, i, rc.top, RGB(255, 0, 0));
- }
- for (int j=rc.top; j<rc.bottom; j++)
- {
- if (j%2==0)
- SetPixel(hDC, rc.right, j, RGB(255, 0, 0));
- }
- for (int i=rc.left; i<rc.right; i++)
- {
- if (i%2==0)
- SetPixel(hDC, i, rc.bottom, RGB(255, 0, 0));
- }
- }
- void DrawDragRect(HDC hDC, POINT ptSource, POINT ptDest)
- {
- int left = ptSource.x <= ptDest.x ? ptSource.x : ptDest.x;
- int right = left + abs(ptDest.x - ptSource.x);
- int top = ptSource.y <= ptDest.y ? ptSource.y : ptDest.y;
- int bottom = top + abs(ptDest.y - ptSource.y);
- ASSERT(left<=right && bottom<=top);
- for (int j=top; j<bottom; j++)
- {
- if (j%2==0)
- SetPixel(hDC, left, j, RGB(255, 0, 0));
- }
- for (int i=left; i<right; i++)
- {
- if (i%2==0)
- SetPixel(hDC, i, top, RGB(255, 0, 0));
- }
- for (int j=top; j<bottom; j++)
- {
- if (j%2==0)
- SetPixel(hDC, right, j, RGB(255, 0, 0));
- }
- for (int i=left; i<right; i++)
- {
- if (i%2==0)
- SetPixel(hDC, i, bottom, RGB(255, 0, 0));
- }
- }
- RECT g_rcDrag = {0};//待绘制的矩形框.
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- ...
- case WM_LBUTTONDOWN:
- {
- // 在客户区按下左键,作为绘制的起点.
- g_rcDrag.right = g_rcDrag.left = LOWORD(lParam);
- g_rcDrag.bottom = g_rcDrag.top = HIWORD(lParam);
- ::SetCapture(hWnd);//必须调用,否则收不到WM_LBUTTONUP.
- }
- break;
- case WM_MOUSEMOVE:
- {
- if (wParam == MK_LBUTTON)
- {
- POINT pt;
- pt.x = LOWORD(lParam);
- pt.y = HIWORD(lParam);
- // 待绘制的拖曳矩形的4个点.
- if (pt.x <= g_rcDrag.left) g_rcDrag.left = pt.x;
- if (g_rcDrag.left != pt.x) g_rcDrag.right = pt.x;
- if (pt.y <= g_rcDrag.top) g_rcDrag.top = pt.y;
- if (g_rcDrag.top != pt.y) g_rcDrag.bottom = pt.y;
- // 发出WM_PAINT消息,以绘制该矩形.
- InvalidateRect(hWnd, NULL, TRUE);// 注意,最后一个参数必须为TRUE,如果为FALSE,上次绘制的矩形不消失.
- UpdateWindow(hWnd);
- }
- }
- break;
- case WM_LBUTTONUP:
- {
- ::ReleaseCapture();
- g_rcDrag.left = g_rcDrag.right = 0;
- InvalidateRect(hWnd, NULL, TRUE);// 注意,最后一个参数必须为TRUE,如果为FALSE,上次绘制的矩形不消失.
- UpdateWindow(hWnd);
- }
- break;
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd, &ps);
- if (0 < g_rcDrag.right-g_rcDrag.left)
- DrawDragRect(hdc, g_rcDrag); //::DrawFocusRect(hdc, &g_rcDrag);也可以
- ::DrawDragRect(hdc, )
- break;
- ...
- }
- RECT g_rcDrag = {-1};
- #define ID_TIMER 1
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- ...
- switch (message)
- {
- case WM_CREATE:
- ::SetTimer (hWnd, ID_TIMER, 100, NULL) ; //创建定时器,每10微妙产生一个WM_TIMER消息
- break;
- case WM_TIMER:
- if (-1 != g_rcDrag.left)
- {
- POINT pt = {0};
- GetCursorPos(&pt);
- ScreenToClient(hWnd, &pt);
- // 待绘制的拖曳矩形的4个点.
- if (pt.x <= g_rcDrag.left) g_rcDrag.left = pt.x;
- if (g_rcDrag.left != pt.x) g_rcDrag.right = pt.x;
- if (pt.y <= g_rcDrag.top) g_rcDrag.top = pt.y;
- if (g_rcDrag.top != pt.y) g_rcDrag.bottom = pt.y;
- InvalidateRect(hWnd, NULL, TRUE);
- UpdateWindow(hWnd);
- }
- break;
- case WM_LBUTTONDOWN:
- {
- g_rcDrag.right = g_rcDrag.left = LOWORD(lParam);
- g_rcDrag.bottom = g_rcDrag.top = HIWORD(lParam);
- ::SetCapture(hWnd);
- }
- break;
- case WM_PAINT:
- {
- hdc = BeginPaint(hWnd, &ps);
- if (0 < g_rcDrag.right-g_rcDrag.left)
- DrawDragRect(hdc, g_rcDrag); //::DrawFocusRect(hdc, &g_rcDrag);也可以
- }
- break;
- case WM_DESTROY:
- KillTimer (hWnd, ID_TIMER) ; //销毁定时器
- break;
- ...
- }
- ...
- }