Click here to Skip to main content
13,145,603 members (46,250 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 25 Jun 2002

Getting BN_DOUBLECLICK to work for buttons

, 25 Jun 2002
Rate this:
Please Sign up or sign in to vote.
BN_DOUBLECLICKED doesn't get received
<!-- Add the rest of your HTML here -->


In MFC it is quite possible to map a BN_DOUBLECLICKED message for your button, but it will never be called by the framework as the two clicks get interpreted as two single clicks on the button. To get around this in the past I developed the following algorithm in the BN_CLICKED handler to work out which type of clicking was really being done by the user.

void CTestView::OnButton1()
    // must be static due to re-entrant function
    static bool double_wait = false;
    // must be static due to re-entrant function
    static bool handled = false ;
    MSG    msg ;
    if (double_wait)
        // this is a double click
        // do double click code here
        TRACE("Double click\n") ;
        handled = true ;
        return ;
        handled = false ;
        double_wait = true ;

    UINT    time = GetDoubleClickTime() ;
    UINT    slice = time / 10 ;
    // break up the OS double click time into some small
    // chunks to avoid hogging the CPU this could be done
    // in many different ways, but I chose this one
    for (int i = 0 ; i < 10 && !handled ; i++)
        // break up the sleep into chuncks to give better
        // response
        if (i == 9)
            Sleep(slice + time % 10) ;
            // dispatch any messages that have come in,
            // including any clicks on this button again.
            Sleep(slice) ;
        while (::PeekMessage(&msg, NULL, 0, 0,
                                PM_NOYIELD | PM_REMOVE))
            ::TranslateMessage(&msg) ;
            ::DispatchMessage(&msg) ;
    // time ran out,
    if (!handled)
        // this is a single click
        // do single click code here
        TRACE("Single click\n") ;
    double_wait = false ;

So with this code in your OnClick() handler, you can then do the correct single/double click function code.


Revision History

26 Jun 2002 - Initial Revision


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


About the Author

Roger Allen
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

You may also be interested in...


Comments and Discussions

GeneralHere is my way to do it. Pin
LeeeNN3-Dec-05 15:34
memberLeeeNN3-Dec-05 15:34 
LeeeNN3-Dec-05 6:23
memberLeeeNN3-Dec-05 6:23 
GeneralAnother way to do it Pin
rasorl30-Sep-03 21:50
memberrasorl30-Sep-03 21:50 
QuestionRe: Another way to do it Pin
bruce2g8-Dec-05 7:37
memberbruce2g8-Dec-05 7:37 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 26 Jun 2002
Article Copyright 2002 by Roger Allen
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid