[Programming Visual C++]Ex05c-CScrollView Revisited

博客围绕OnMouseMove坐标转换代码展开,介绍其包含的平移语句逻辑,如构建椭圆矩形并转换坐标、使矩形无效等。还提及SetCapture函数捕获鼠标及副作用,以及Win32 ReleaseCapture函数关闭鼠标捕获,同时指出移动椭圆时需保持鼠标相对位置。
1. Mouse Move
void CEx05cView::OnMouseMove(UINT nFlags, CPoint point)
{
    if (m_bCaptured) {
        CClientDC dc(this);
        OnPrepareDC(&dc);
        CRect rectOld(m_pointTopLeft, m_sizeEllipse);
        dc.LPtoDP(rectOld);
        InvalidateRect(rectOld, TRUE);
        m_pointTopLeft = point - m_sizeOffset;
        dc.DPtoLP(&m_pointTopLeft);
        CRect rectNew(m_pointTopLeft, m_sizeEllipse);
        dc.LPtoDP(rectNew);
        InvalidateRect(rectNew, TRUE);
    }
}

The OnMouseMove Coordinate Transformation Code

As you can see, this function contains several translation statements. The logic can be summarized by the following steps:

  1. Construct the previous ellipse rectangle and convert it from logical to device coordinates.
  2. Invalidate the previous rectangle.
  3. Update the top left coordinate of the ellipse rectangle.
  4. Construct the new rectangle and convert it to device coordinates.
  5. Invalidate the new rectangle.

The function calls InvalidateRect twice. Windows "saves up" the two invalid rectangles and computes a new invalid rectangle that is the union of the two, intersected with the client rectangle.

2. Set Cursor Type
void CEx05cView::OnLButtonDown(UINT nFlags, CPoint point) {
   ...
    if (circle.PtInRegion(point)) {
        // Capturing the mouse ensures subsequent LButtonUp message
        SetCapture();
        ...
        // New mouse cursor is active while mouse is captured
        ::SetCursor(::LoadCursor(NULL, IDC_CROSS));
    }
}

3.The SetCapture and ReleaseCapture Functions

SetCapture is the CWnd member function that "captures" the mouse, such that mouse movement messages are sent to this window even if the mouse cursor is outside the window. An unfortunate side effect of this function is that the ellipse can be moved outside the window and "lost." A desirable and necessary effect is that all subsequent mouse messages are sent to the window, including the WM_LBUTTONUP message, which would otherwise be lost. The Win32 ReleaseCapture function turns off mouse capture.


void CEx05cView::OnLButtonUp(UINT nFlags, CPoint point)
{
    if (m_bCaptured) {
        ::ReleaseCapture();
        m_bCaptured = FALSE;
    }
}

4.The m_sizeOffset Data Member

When OnMouseMove moves the ellipse, the relative position of the mouse within the ellipse must be the same as it was when the user first pressed the left mouse button. The m_sizeOffset object stores this original offset of the mouse from the top left corner of the ellipse rectangle.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值