Click here to Skip to main content
15,881,757 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 401K   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

 
GeneralMy vote is 0 (zero) Pin
Voevudko A. E., Ph.D.19-Dec-17 9:43
professionalVoevudko A. E., Ph.D.19-Dec-17 9:43 
GeneralMy vote of 5 Pin
Suvendu Shekhar Giri21-Oct-16 18:35
professionalSuvendu Shekhar Giri21-Oct-16 18:35 
GeneralCode to Click the UnClickable Pin
SoftwareDeveloperGoa7-Dec-10 12:50
SoftwareDeveloperGoa7-Dec-10 12:50 
GeneralRe: Code to Click the UnClickable Pin
Super Lloyd7-Dec-10 13:19
Super Lloyd7-Dec-10 13:19 
GeneralMy vote of 3 Pin
drummerboy05118-Jul-10 13:13
professionaldrummerboy05118-Jul-10 13:13 
GeneralRe: My vote of 3 Pin
Chris Maunder13-Oct-10 1:45
cofounderChris Maunder13-Oct-10 1:45 
GeneralRe: My vote of 3 Pin
Super Lloyd7-Dec-10 13:20
Super Lloyd7-Dec-10 13:20 
GeneralClassic Pin
blackjack215027-Jan-09 2:02
blackjack215027-Jan-09 2:02 
QuestionIs there a editable button? Pin
vivadot26-May-04 13:45
vivadot26-May-04 13:45 
GeneralPlacing a button on the MainFrame Pin
Alex Evans4-Sep-03 19:43
Alex Evans4-Sep-03 19:43 
GeneralRe: Placing a button on the MainFrame Pin
MasterOfBytes23-Apr-04 4:18
MasterOfBytes23-Apr-04 4:18 
GeneralRe: Placing a button on the MainFrame Pin
Alex Evans25-Apr-04 22:55
Alex Evans25-Apr-04 22:55 
GeneralDebuging Tool Pin
CSharpDavid31-May-02 6:13
CSharpDavid31-May-02 6:13 
GeneralMFC70.dll Pin
Philippe Lhoste28-May-02 1:42
Philippe Lhoste28-May-02 1:42 
GeneralRe: MFC70.dll Pin
Chris Maunder30-May-02 20:35
cofounderChris Maunder30-May-02 20:35 
GeneralRe: MFC70.dll Pin
Philippe Lhoste2-Jun-02 22:28
Philippe Lhoste2-Jun-02 22:28 
GeneralRe: MFC70.dll Pin
Chris Maunder2-Jun-02 22:52
cofounderChris Maunder2-Jun-02 22:52 
GeneralDifferent Joke Control Pin
Paul M Watt24-May-02 16:20
mentorPaul M Watt24-May-02 16:20 
GeneralI clicked the unclickable! Pin
Tom Archer24-May-02 12:15
Tom Archer24-May-02 12:15 
GeneralRe: I clicked the unclickable! Pin
Nish Nishant24-May-02 14:45
sitebuilderNish Nishant24-May-02 14:45 
GeneralRe: I clicked the unclickable! Pin
Tom Archer24-May-02 15:18
Tom Archer24-May-02 15:18 
GeneralRe: I clicked the unclickable! Pin
Tom Archer24-May-02 15:20
Tom Archer24-May-02 15:20 
GeneralRe: I clicked the unclickable! Pin
Nish Nishant25-May-02 3:56
sitebuilderNish Nishant25-May-02 3:56 
GeneralRe: I clicked the unclickable! Pin
Bhaskar Priya24-Oct-06 2:06
Bhaskar Priya24-Oct-06 2:06 
GeneralRe: I clicked the unclickable! Pin
14-Jun-02 4:57
suss14-Jun-02 4:57 

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.