moveablebutton.cpp

来自「这个是MFC用于拖动CSATIC图标的程序源代码」· C++ 代码 · 共 69 行

CPP
69
字号
// MoveableButton.cpp : implementation file
//

#include "stdafx.h"
#include "MoveableButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMoveableButton

CMoveableButton::CMoveableButton()
{
    m_bStartMove = FALSE;
}

CMoveableButton::~CMoveableButton()
{
}


BEGIN_MESSAGE_MAP(CMoveableButton, CButton)
//{{AFX_MSG_MAP(CMoveableButton)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMoveableButton message handlers

void CMoveableButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    m_bStartMove = TRUE;
    CButton::OnLButtonDown(nFlags, point);
}

void CMoveableButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    m_bStartMove = FALSE;
    CButton::OnLButtonUp(nFlags, point);
}

void CMoveableButton::OnMouseMove(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    if (m_bStartMove)
    {
        CRect rect;
		
        ClientToScreen(&point);
        this->GetParent()->ScreenToClient(&point);
		
        GetClientRect(&rect);
        point.Offset(-rect.Width()/2, -rect.Height()/2);
		
        SetWindowPos(NULL, point.x , point.y , 0, 0, SWP_NOZORDER|SWP_NOSIZE);
		//         afxDump<<point.x<<","<<point.y<<"\n";
        Invalidate();
    }
    CButton::OnMouseMove(nFlags, point);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?