Getting BN_DOUBLECLICK to Work for Buttons






4.18/5 (5 votes)
BN_DOUBLECLICKED doesn't get received
Introduction
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 ;
}
else
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) ;
else
{
// 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.
Enjoy!
Revision History
- 26th June, 2002 - Initial revision