Click here to Skip to main content
11,720,785 members (83,756 online)
Click here to Skip to main content

An unclickable button

, 30 May 2002 CPOL 338.5K 3.2K 63
Rate this:
Please Sign up or sign in to vote.
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?) Smile | :)

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;                                   // 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)

Share

About the Author

Chris Maunder
Founder CodeProject
Canada Canada
Chris is the Co-founder, Administrator, Architect, Chief Editor and Shameless Hack who wrote and runs The Code Project. He's been programming since 1988 while pretending to be, in various guises, an astrophysicist, mathematician, physicist, hydrologist, geomorphologist, defence intelligence researcher and then, when all that got a bit rough on the nerves, a web developer. He is a Microsoft Visual C++ MVP both globally and for Canada locally.

His programming experience includes C/C++, C#, SQL, MFC, ASP, ASP.NET, and far, far too much FORTRAN. He has worked on PocketPCs, AIX mainframes, Sun workstations, and a CRAY YMP C90 behemoth but finds notebooks take up less desk space.

He dodges, he weaves, and he never gets enough sleep. He is kind to small animals.

Chris was born and bred in Australia but splits his time between Toronto and Melbourne, depending on the weather. For relaxation he is into road cycling, snowboarding, rock climbing, and storm chasing.

You may also be interested in...

Comments and Discussions

 
GeneralCode to Click the UnClickable Pin
SoftwareDeveloperGoa7-Dec-10 12:50
memberSoftwareDeveloperGoa7-Dec-10 12:50 
GeneralRe: Code to Click the UnClickable Pin
Super Lloyd7-Dec-10 13:19
memberSuper Lloyd7-Dec-10 13:19 
GeneralMy vote of 3 Pin
drummerboy05118-Jul-10 13:13
memberdrummerboy05118-Jul-10 13:13 
GeneralRe: My vote of 3 Pin
Chris Maunder13-Oct-10 1:45
adminChris Maunder13-Oct-10 1:45 
GeneralRe: My vote of 3 Pin
Super Lloyd7-Dec-10 13:20
memberSuper Lloyd7-Dec-10 13:20 
GeneralClassic Pin
blackjack215027-Jan-09 2:02
memberblackjack215027-Jan-09 2:02 
QuestionIs there a editable button? Pin
vivadotnet26-May-04 13:45
membervivadotnet26-May-04 13:45 
GeneralPlacing a button on the MainFrame Pin
Alex Evans4-Sep-03 19:43
memberAlex Evans4-Sep-03 19:43 
GeneralRe: Placing a button on the MainFrame Pin
MasterOfBytes23-Apr-04 4:18
memberMasterOfBytes23-Apr-04 4:18 
GeneralRe: Placing a button on the MainFrame Pin
Alex Evans25-Apr-04 22:55
memberAlex Evans25-Apr-04 22:55 
GeneralDebuging Tool Pin
David Gallagher31-May-02 6:13
memberDavid Gallagher31-May-02 6:13 
GeneralMFC70.dll Pin
Philippe Lhoste28-May-02 1:42
memberPhilippe Lhoste28-May-02 1:42 
GeneralRe: MFC70.dll Pin
Chris Maunder30-May-02 20:35
adminChris Maunder30-May-02 20:35 
GeneralRe: MFC70.dll Pin
Philippe Lhoste2-Jun-02 22:28
memberPhilippe Lhoste2-Jun-02 22:28 
GeneralRe: MFC70.dll Pin
Chris Maunder2-Jun-02 22:52
adminChris Maunder2-Jun-02 22:52 
GeneralDifferent Joke Control Pin
kilowatt24-May-02 16:20
memberkilowatt24-May-02 16:20 
GeneralI clicked the unclickable! Pin
Tom Archer24-May-02 12:15
memberTom Archer24-May-02 12:15 
GeneralRe: I clicked the unclickable! Pin
Nish - Native CPian24-May-02 14:45
memberNish - Native CPian24-May-02 14:45 
GeneralRe: I clicked the unclickable! Pin
Tom Archer24-May-02 15:18
memberTom Archer24-May-02 15:18 
GeneralRe: I clicked the unclickable! Pin
Tom Archer24-May-02 15:20
memberTom Archer24-May-02 15:20 
GeneralRe: I clicked the unclickable! Pin
Nish - Native CPian25-May-02 3:56
memberNish - Native CPian25-May-02 3:56 
GeneralRe: I clicked the unclickable! Pin
BhaskarPriya24-Oct-06 2:06
memberBhaskarPriya24-Oct-06 2:06 
GeneralRe: I clicked the unclickable! Pin
Anonymous14-Jun-02 4:57
memberAnonymous14-Jun-02 4:57 
GeneralRe: I clicked the unclickable! Pin
mla15421-Jan-13 7:52
membermla15421-Jan-13 7:52 
GeneralPerfect Pin
Mark Nischalke23-May-02 14:51
memberMark Nischalke23-May-02 14:51 
GeneralRe: Perfect Pin
Nish - Native CPian23-May-02 15:10
memberNish - Native CPian23-May-02 15:10 
Generali saw this in a commerical app! Pin
Jared Allen23-May-02 13:06
memberJared Allen23-May-02 13:06 
GeneralI know just the place to use this... Pin
Matt Gullett23-May-02 12:23
memberMatt Gullett23-May-02 12:23 
GeneralManaged Version Pin
Rama Krishna23-May-02 4:52
memberRama Krishna23-May-02 4:52 
GeneralRe: Managed Version Pin
Nish - Native CPian23-May-02 8:48
memberNish - Native CPian23-May-02 8:48 
GeneralRe: Managed Version Pin
Rama Krishna23-May-02 9:01
memberRama Krishna23-May-02 9:01 
GeneralRe: Managed Version Pin
Nish - Native CPian23-May-02 21:49
memberNish - Native CPian23-May-02 21:49 
GeneralRe: Managed Version Pin
Alex Schmidt3-Dec-03 4:48
memberAlex Schmidt3-Dec-03 4:48 
GeneralWTL Version Pin
Tim Smith23-May-02 4:36
memberTim Smith23-May-02 4:36 
QuestionReally neat... but how do you use it? Pin
Selevercin23-May-02 4:13
memberSelevercin23-May-02 4:13 
Generalbut... Pin
jack Mesic22-May-02 22:09
memberjack Mesic22-May-02 22:09 
GeneralOld Trick Pin
Mark A22-May-02 10:14
memberMark A22-May-02 10:14 
GeneralMemories... Pin
Ravi Bhavnani22-May-02 10:14
memberRavi Bhavnani22-May-02 10:14 
GeneralRe: Memories... Pin
Chris Maunder22-May-02 13:17
adminChris Maunder22-May-02 13:17 
GeneralI'd always wondered... Pin
David Wulff22-May-02 7:50
memberDavid Wulff22-May-02 7:50 
GeneralRe: I'd always wondered... Pin
Philip Patrick22-May-02 20:50
memberPhilip Patrick22-May-02 20:50 
GeneralPuzzle UI Pin
Chris Losinger22-May-02 2:34
memberChris Losinger22-May-02 2:34 
GeneralRe: Puzzle UI Pin
Selevercin23-May-02 3:49
memberSelevercin23-May-02 3:49 
Generalcheat... Pin
Bug22-May-02 0:44
memberBug22-May-02 0:44 
GeneralRe: cheat... Pin
Chris Maunder22-May-02 1:18
adminChris Maunder22-May-02 1:18 
GeneralRe: cheat... Pin
Bug22-May-02 2:03
memberBug22-May-02 2:03 
GeneralRe: cheat... Pin
Pete Bassett22-May-02 6:04
memberPete Bassett22-May-02 6:04 
GeneralRe: cheat... Pin
Chris Maunder22-May-02 13:08
adminChris Maunder22-May-02 13:08 
GeneralRe: cheat... Pin
Tom Archer24-May-02 12:28
memberTom Archer24-May-02 12:28 
GeneralRe: cheat... Pin
Chris Maunder24-May-02 14:18
adminChris Maunder24-May-02 14:18 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 31 May 2002
Article Copyright 2002 by Chris Maunder
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid