mfc 移动绘制的图形

 

在Dialog的消息中调用对应的函数

 

// 拖拽矩形操作类
class CDragRect {
public:
    void OnInitDialog(HWND hwnd);

    void OnSize(int x, int y);
    void OnPaint(HDC hdc);

    void OnMouseDown(int x, int y);
    void OnMouseUp();
    void OnMouseMove(int x, int y);

    // 矩形的最小大小
    void SetMinSize(int width, int height) { mMinWidth = width; mMinHeight = height; }

protected:
    // 在区域内
    static bool mIsAtRect(RECT& rect, int x, int y){
        if (x > rect.left && x < rect.right && y > rect.top && y < rect.bottom) {
            return true;
        }
        return false;
    }

    // 在右下角(设置大小)区域内
    static bool mIsAtSERect(RECT& rect, int x, int y) {
        RECT rct;
        int width = 5;
        rct.left = rect.right - width;
        rct.top = rect.bottom - width;
        rct.right = rect.right + width;
        rct.bottom = rect.bottom + width;

        return mIsAtRect(rct, x, y);
    }

    // 获取光标
    static HCURSOR yGetWndCursor(HWND hWnd) {
        DWORD currCur = ::GetClassLong(hWnd, GCL_HCURSOR);
        return (HCURSOR)currCur;
    }

    // 设置光标
    static void ySetWndCursor(HWND hWnd, HCURSOR hCursor) {
        DWORD current = ::GetClassLong(hWnd, GCL_HCURSOR);
        if (current != (LONG)hCursor) {
            ::SetClassLong(hWnd, GCL_HCURSOR, (LONG)hCursor);
        }
    }

protected:
    HWND mHwnd; //矩形所在的窗口
    RECT rectDrag; // 拖动的句柄区域
    bool mIsMouseDown;
    POINT mMouseDownLocation; // 鼠标按下时的位置
    RECT mouseDownRect; // 记录鼠标按下时rectDrag的值, 用于计算拖动后的大小
    HCURSOR mCurSizeAll, mCurSizeNWSE, mCurDefault; //鼠标
    int mMinWidth, mMinHeight; // 矩形的最小大小
    int mMargin; // 矩形距离边缘的最小距离
};

 

 

void CDragRect::OnMouseDown(int x, int y)
{
    mIsMouseDown = true;
    mMouseDownLocation.x = x;
    mMouseDownLocation.y = y;

    mouseDownRect.left = rectDrag.left;
    mouseDownRect.top = rectDrag.top;
    mouseDownRect.right = rectDrag.right;
    mouseDownRect.bottom = rectDrag.bottom;

    ::SetCapture(mHwnd);
}

void CDragRect::OnMouseUp()
{
    mIsMouseDown = false;
    mMouseDownLocation.x = mMouseDownLocation.y = 0;

    ySetWndCursor(mHwnd, mCurDefault);
    ::ReleaseCapture();
}

void CDragRect::OnMouseMove(int x, int y)
{
    if (false == mIsMouseDown)
    {
        if (mIsAtSERect(rectDrag, x, y)) {
            ySetWndCursor(mHwnd, mCurSizeNWSE);
        }
        else if (mIsAtRect(rectDrag, x, y)) {
            ySetWndCursor(mHwnd, mCurSizeAll);
        }
        else {
            ySetWndCursor(mHwnd, mCurDefault);
        }
    }
    else
    {
        RECT client;
        ::GetClientRect(mHwnd, &client);
        int cliWidth = client.right - client.left;
        int cliHeight = client.bottom - client.top;

        HCURSOR currentCursor = yGetWndCursor(mHwnd);
        if (currentCursor == mCurSizeAll) {
            //移动操作
            int rectWidth = rectDrag.right - rectDrag.left;
            int rectHeight = rectDrag.bottom - rectDrag.top;
            rectDrag.left = (x - mMouseDownLocation.x) + mouseDownRect.left;
            rectDrag.top = (y - mMouseDownLocation.y) + mouseDownRect.top;

            rectDrag.right = rectDrag.left + rectWidth;
            rectDrag.bottom = rectDrag.top + rectHeight;

            if (rectDrag.left < mMargin) { rectDrag.left = mMargin; }
            if (rectDrag.right >(cliWidth - mMargin)) { rectDrag.left = (cliWidth - mMargin) - rectWidth; }

            if (rectDrag.top < mMargin) { rectDrag.top = mMargin; }
            if (rectDrag.bottom >(cliHeight - mMargin)) { rectDrag.top = (cliHeight - mMargin) - rectHeight; }

            rectDrag.right = rectDrag.left + rectWidth;
            rectDrag.bottom = rectDrag.top + rectHeight;
        }
        else if (currentCursor == mCurSizeNWSE) {
            //大小操作
            int newWidth = (x - mMouseDownLocation.x) + (mouseDownRect.right - mouseDownRect.left);
            int newHeight = (y - mMouseDownLocation.y) + (mouseDownRect.bottom - mouseDownRect.top);

            if (newWidth < mMinWidth) { newWidth = mMinWidth; }
            if (newHeight < mMinHeight) { newHeight = mMinHeight; }

            if (rectDrag.left + newWidth > cliWidth - mMargin) { newWidth = (cliWidth - mMargin) - rectDrag.left; }
            if (rectDrag.top + newHeight > cliHeight - mMargin) { newHeight = (cliHeight - mMargin) - rectDrag.top; }

            rectDrag.right = rectDrag.left + newWidth;
            rectDrag.bottom = rectDrag.top + newHeight;
        }

        ::InvalidateRect(mHwnd, &client, TRUE);
    }
}

void CDragRect::OnSize(int x, int y)
{
    if (::IsWindow(mHwnd))
    {
        RECT clientRect;
        ::GetClientRect(mHwnd, &clientRect);

        int w = clientRect.right - clientRect.left;
        int h = clientRect.bottom - clientRect.top;

        rectDrag.left = w / 4;
        rectDrag.top = h / 4;
        rectDrag.right = w * 3 / 4;
        rectDrag.bottom = h * 3 / 4;
    }
}

void CDragRect::OnPaint(HDC hdc)
{
    int x = rectDrag.left;
    int y = rectDrag.top;
    int w = rectDrag.right - rectDrag.left;
    int h = rectDrag.bottom - rectDrag.top;

    HPEN hNewPen = CreatePen(PS_SOLID, 1, RGB(125, 125, 125));
    HPEN hOldPen = (HPEN)SelectObject(hdc, hNewPen);

    MoveToEx(hdc, x, y, NULL);
    LineTo(hdc, x + w, y);
    LineTo(hdc, x + w, y + h);
    LineTo(hdc, x, y + h);
    LineTo(hdc, x, y);

    SelectObject(hdc, hOldPen);
    DeleteObject(hNewPen);
}

void CDragRect::OnInitDialog(HWND hwnd) {
    mHwnd = hwnd;

    mIsMouseDown = false;
    mMouseDownLocation.x = mMouseDownLocation.y = 0;
    mMinWidth = mMinHeight = 10;
    mMargin = 4;

    mCurDefault = LoadCursor(NULL, IDC_ARROW); //箭头
    mCurSizeAll = LoadCursor(NULL, IDC_SIZEALL); //拖动
    mCurSizeNWSE = LoadCursor(NULL, IDC_SIZENWSE);
}

 

posted @ 2017-05-26 17:20  菩提树~今生  阅读(2266)  评论(0编辑  收藏  举报