An Unclickable Button | CodeGuru

An Unclickable Button

  Download Source Code and Demo Project Environment: VC++ 5.0 (SP3) NT 4.0 (SP3), Win95 I wrote this a while back 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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 11, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

 
Download Source Code and Demo Project

Environment: VC++ 5.0 (SP3) NT 4.0 (SP3), Win95

I wrote this a while back 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).

void CTrickButton::OnMouseMove(UINT nFlags, CPoint point)
{
    CWnd* pParent = GetParent();
    if (!pParent) pParent = GetDesktopWindow();

CRect ParentRect; // Parent client area (Parent coords)
pParent->GetClientRect(ParentRect);

ClientToScreen(&point); // Convert point to parent coords
pParent->ScreenToClient(&point);

CRect ButtonRect; // Button Dimensions (Parent coords)
GetWindowRect(ButtonRect);
pParent->ScreenToClient(ButtonRect);
CPoint Center = ButtonRect.CenterPoint(); // Center of button (parent coords)

CSize CriticalSize(ButtonRect.Width()/6, ButtonRect.Height()/6);

CRect NewButtonRect = ButtonRect; // New position (parent coords)

if (point.x – CriticalSize.cx > Center.x) // Mouse is right of center
{
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) // Mouse is left of center { 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) // Mouse is below center
{
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) // Mouse is above center { 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); }

Last updated: 16 June 1998

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.