📄 mybutton.cpp
字号:
// MyButton.cpp : implementation file
//
#include "stdafx.h"
#include "Button.h"
#include "MyButton.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyButton
CMyButton::CMyButton()
{
m_nJumpDistance=10; //移动的距离
}
CMyButton::~CMyButton()
{
}
BEGIN_MESSAGE_MAP(CMyButton, CButton)
//{{AFX_MSG_MAP(CMyButton)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CWnd *pParent=GetParent();
if(!pParent)pParent=GetDesktopWindow();
CRect ParentRect;
pParent->GetClientRect(ParentRect);
ClientToScreen(&point);
pParent->ScreenToClient(&point);
CRect ButtonRect;
GetWindowRect(ButtonRect);
pParent->ScreenToClient(ButtonRect);
CPoint Center=ButtonRect.CenterPoint();
CSize CriticalSize(ButtonRect.Width()/6,ButtonRect.Height()/6);
CRect NewButtonRect=ButtonRect;
if(point.x-CriticalSize.cx>Center.x)
{
if(ButtonRect.left>ParentRect.left+ButtonRect.Width()+m_nJumpDistance)
NewButtonRect-=CSize(ButtonRect.right-point.x+m_nJumpDistance,0);
else
NewButtonRect+=CSize(point.x-ButtonRect.left+m_nJumpDistance,0);
}
else if(point.x+CriticalSize.cx<Center.x)
{
if(ButtonRect.right<ParentRect.right-ButtonRect.Width()-m_nJumpDistance)
NewButtonRect+=CSize(point.x-ButtonRect.left+m_nJumpDistance,0);
else
NewButtonRect-=CSize(ButtonRect.right-point.x+m_nJumpDistance,0);
}
if(point.y-CriticalSize.cy>Center.y)
{
if(ButtonRect.top>ParentRect.top+ButtonRect.Height()+m_nJumpDistance)
NewButtonRect-=CSize(0,ButtonRect.bottom-point.y+m_nJumpDistance);
else
NewButtonRect+=CSize(0,point.y-ButtonRect.top+m_nJumpDistance);
}
else if(point.y+CriticalSize.cy<Center.y)
{
if(ButtonRect.bottom<ParentRect.bottom-ButtonRect.Height()-m_nJumpDistance)
NewButtonRect+=CSize(0,point.y-ButtonRect.top+m_nJumpDistance);
else
NewButtonRect-=CSize(0,ButtonRect.bottom-point.y+m_nJumpDistance);
}
MoveWindow(NewButtonRect);
RedrawWindow();
CButton::OnMouseMove(nFlags, point);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -