
Introduction
I wrote this many years ago for a friend. It is basically just a normal pushbutton, except that when the user tries to click on it, it moves out of the way making it practically un-clickable.
I have no idea what anyone would ever want to use such a control in their app, apart from wanting to be extremely annoying to users (and hey - don't we all get like that on occasion?) :)
The button is just a normal button with OnMouseMove
overridden: (m_nJumpDistance
is the distance in pixels to jump once the mouse has moved over the control).
The WM_SETFOCUS
message has also been handled to bounce the focus back to the window that previously had focus, and PreSubclassWindow
has been overridden to allow the removal of the WS_TABSTOP
window style bit. This ensures that the user can't tab to the control.
The guts of it all
void CTrickButton::OnMouseMove(UINT nFlags, CPoint point)
{
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();
CRect NewButtonRect = ButtonRect;
if (point.x > 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 < 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 > 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 < 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);
}
void CTrickButton::OnSetFocus(CWnd* pOldWnd)
{
CButton::OnSetFocus(pOldWnd);
if (pOldWnd!=NULL && ::IsWindow(pOldWnd->GetSafeHwnd()))
pOldWnd->SetFocus();
}
void CTrickButton::PreSubclassWindow()
{
ModifyStyle(WS_TABSTOP, 0);
CButton::PreSubclassWindow();
}
History
31 May 2002 - updated to include removal of WM_TABSTOP
, plus minor cleanup.
Current passion project:
CodeProject SenseAI. Please join in!
Chris is the Co-founder of the popular code-sharing site CodeProject.com, the digital advertising agency DeveloperMedia.com and the content marketing agency ContentLab.IO.
He's been programming way too long and has been, in various guides, an astrophysicist, mechanic, mathematician, physicist, breeder of carnivorous plants, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a serial entrepreneur.
Chris has programmed everything from FORTRAN on a CRAY to Python on a Pi, but generally leans on TypeScript, C#, and SQL for the front, middle and back bits of his applications. His current focus is on ensuring developers know enough about Artificial Intelligence to be dangerous.