Click here to Skip to main content
15,892,005 members
Articles / Desktop Programming / MFC

An unclickable button

Rate me:
Please Sign up or sign in to vote.
4.87/5 (40 votes)
30 May 2002CPOL 401.7K   3.6K   64   77
What looks like a normal pushbutton - until the user tries to click on it.

Sample Image - trick_button.gif

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

C++
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)

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

    if (point.x > 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 < 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 > 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 < 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);
}

void CTrickButton::OnSetFocus(CWnd* pOldWnd)
{
    CButton::OnSetFocus(pOldWnd);

    // Give the focus right back to the window that justn gave it to us
    if (pOldWnd!=NULL && ::IsWindow(pOldWnd->GetSafeHwnd()))
        pOldWnd->SetFocus();
}

void CTrickButton::PreSubclassWindow()
{
    // Ensure that the TabStop style is removed from the control
    ModifyStyle(WS_TABSTOP, 0);
    CButton::PreSubclassWindow();
}

History

31 May 2002 - updated to include removal of WM_TABSTOP, plus minor cleanup.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder CodeProject
Canada Canada
Chris Maunder is the co-founder of CodeProject and ContentLab.com, and has been a prominent figure in the software development community for nearly 30 years. Hailing from Australia, Chris has a background in Mathematics, Astrophysics, Environmental Engineering and Defence Research. His programming endeavours span everything from FORTRAN on Super Computers, C++/MFC on Windows, through to to high-load .NET web applications and Python AI applications on everything from macOS to a Raspberry Pi. Chris is a full-stack developer who is as comfortable with SQL as he is with CSS.

In the late 1990s, he and his business partner David Cunningham recognized the need for a platform that would facilitate knowledge-sharing among developers, leading to the establishment of CodeProject.com in 1999. Chris's expertise in programming and his passion for fostering a collaborative environment have played a pivotal role in the success of CodeProject.com. Over the years, the website has grown into a vibrant community where programmers worldwide can connect, exchange ideas, and find solutions to coding challenges. Chris is a prolific contributor to the developer community through his articles and tutorials, and his latest passion project, CodeProject.AI.

In addition to his work with CodeProject.com, Chris co-founded ContentLab and DeveloperMedia, two projects focussed on helping companies make their Software Projects a success. Chris's roles included Product Development, Content Creation, Client Satisfaction and Systems Automation.

Comments and Discussions

 
GeneralRe: Perfect Pin
Nish Nishant23-May-02 15:10
sitebuilderNish Nishant23-May-02 15:10 
Generali saw this in a commerical app! Pin
Jared Allen23-May-02 13:06
Jared Allen23-May-02 13:06 
GeneralI know just the place to use this... PinPopular
Matt Gullett23-May-02 12:23
Matt Gullett23-May-02 12:23 
GeneralManaged Version Pin
Rama Krishna Vavilala23-May-02 4:52
Rama Krishna Vavilala23-May-02 4:52 
GeneralRe: Managed Version Pin
Nish Nishant23-May-02 8:48
sitebuilderNish Nishant23-May-02 8:48 
GeneralRe: Managed Version Pin
Rama Krishna Vavilala23-May-02 9:01
Rama Krishna Vavilala23-May-02 9:01 
GeneralRe: Managed Version Pin
Nish Nishant23-May-02 21:49
sitebuilderNish Nishant23-May-02 21:49 
GeneralRe: Managed Version Pin
Keia3-Dec-03 4:48
professionalKeia3-Dec-03 4:48 
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CodeProject.WinForms
{
public class TrickButton: System.Windows.Forms.Button
{
// Member variables
protected int m_nJumpDistance = 5; // Pixels to jump

public TrickButton()
{
TabStop = false;
}

public int JumpDistance
{
get
{
return m_nJumpDistance;
}
set
{
m_nJumpDistance = value;
}
}

// Called when the mouse moves over the button
protected override void OnMouseMove(MouseEventArgs e)
{
// Get the current mouse position from the MouseEventArgs
// object and convert to screen coords
Point point = new Point(e.X, e.Y);
point = PointToScreen(point);

// All pixels are used in parent client coordinates
// So convert screen coords parent relative coords
Control pParent = Parent;
if (pParent != null)
{
point = pParent.PointToClient(point);
}

// Get the client area of the parent (so we know the bounds
// of movement for the button
Rectangle ParentRect;
if (pParent == null)
{
// use the current screen if no parent
Screen currentScreen = Screen.FromPoint(point);
ParentRect = currentScreen.WorkingArea;
}
else
{
ParentRect = pParent.ClientRectangle;
}

// Get the dimensions of this button and convert to parent coords
Rectangle buttonRect = RectangleToScreen(ClientRectangle);
if (pParent != null)
{
buttonRect = pParent.RectangleToClient(buttonRect);
}

Point center = new Point((buttonRect.Right+buttonRect.Left)/2,
(buttonRect.Bottom+buttonRect.Top)/2);

// We now check where the mouse is relative to the center
// of the button. If it's within the critical distance then
// we need to know from which direction the mouse is coming
// from so we can move the button away from the mouse

Rectangle newButtonRect = buttonRect;

// mouse attack from the right side
if (point.X > center.X)
{
if (buttonRect.Left > ParentRect.Left + buttonRect.Width + m_nJumpDistance)
{
newButtonRect.X -= buttonRect.Right - point.X + m_nJumpDistance;
}
else
{
newButtonRect.X += point.X - buttonRect.Left + m_nJumpDistance;
}
}
// mouse attack from the left
else if (point.X < center.X)
{
if (buttonRect.Right < ParentRect.Right - buttonRect.Width - m_nJumpDistance)
{
newButtonRect.X += point.X - buttonRect.Left + m_nJumpDistance;
}
else
{
newButtonRect.X -= buttonRect.Right - point.X + m_nJumpDistance;
}
}
// mouse attack from below
if (point.Y > center.Y)
{
if (buttonRect.Top > ParentRect.Top + buttonRect.Height + m_nJumpDistance)
{
newButtonRect.Y -= buttonRect.Bottom - point.Y + m_nJumpDistance;
}
else
{
newButtonRect.Y += point.Y - buttonRect.Top + m_nJumpDistance;
}
}
// attack from above
else if (point.Y < center.Y)
{
if (buttonRect.Bottom < ParentRect.Bottom - buttonRect.Height - m_nJumpDistance)
{
newButtonRect.Y += point.Y - buttonRect.Top + m_nJumpDistance;
}
else
{
newButtonRect.Y -= buttonRect.Bottom - point.Y + m_nJumpDistance;
}
}

// now we move the button to the new point
Location = new Point(newButtonRect.X, newButtonRect.Y);

base.OnMouseMove(e);
}

protected override void WndProc(ref System.Windows.Forms.Message pMsg)
{
const int WM_SETFOCUS = 0x0007;

// Call the base class function first
base.WndProc(ref pMsg);

// If the button is recieving the focus then pass
// it back to the window that gave it the focus
if (pMsg.Msg == WM_SETFOCUS)
{
Control pOldWnd = FromHandle(pMsg.WParam);

if (pOldWnd != null)
{
pOldWnd.Focus(); //set focus to previous control
}
}
}
}
}

GeneralWTL Version Pin
Tim Smith23-May-02 4:36
Tim Smith23-May-02 4:36 
QuestionReally neat... but how do you use it? Pin
Selevercin23-May-02 4:13
Selevercin23-May-02 4:13 
Generalbut... Pin
jack Mesic22-May-02 22:09
jack Mesic22-May-02 22:09 
GeneralOld Trick Pin
Mark A22-May-02 10:14
Mark A22-May-02 10:14 
GeneralMemories... Pin
Ravi Bhavnani22-May-02 10:14
professionalRavi Bhavnani22-May-02 10:14 
GeneralRe: Memories... Pin
Chris Maunder22-May-02 13:17
cofounderChris Maunder22-May-02 13:17 
GeneralI'd always wondered... Pin
David Wulff22-May-02 7:50
David Wulff22-May-02 7:50 
GeneralRe: I'd always wondered... Pin
Philip Patrick22-May-02 20:50
professionalPhilip Patrick22-May-02 20:50 
GeneralPuzzle UI Pin
Chris Losinger22-May-02 2:34
professionalChris Losinger22-May-02 2:34 
GeneralRe: Puzzle UI Pin
Selevercin23-May-02 3:49
Selevercin23-May-02 3:49 
Generalcheat... Pin
Bug22-May-02 0:44
Bug22-May-02 0:44 
GeneralRe: cheat... Pin
Chris Maunder22-May-02 1:18
cofounderChris Maunder22-May-02 1:18 
GeneralRe: cheat... Pin
Bug22-May-02 2:03
Bug22-May-02 2:03 
GeneralRe: cheat... Pin
Pete Bassett22-May-02 6:04
Pete Bassett22-May-02 6:04 
GeneralRe: cheat... Pin
Chris Maunder22-May-02 13:08
cofounderChris Maunder22-May-02 13:08 
GeneralRe: cheat... Pin
Tom Archer24-May-02 12:28
Tom Archer24-May-02 12:28 
GeneralRe: cheat... Pin
Chris Maunder24-May-02 14:18
cofounderChris Maunder24-May-02 14:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.