Re: Catching button pressed messages
- From: JoeB <joe@xxxxxxxxxx>
- Date: Tue, 26 Sep 2006 16:35:57 +0100
I needed to send a message when the button changes state, so just detecting button down was not enough. The BM_SETSTATE message was sent when ever the change occurs, so was the simplist solution.
I figured that the extra message when the button is clicked normally could be stopped if i handled the OnLButtonUp message, and just released the mouse capture, and did NOT call the base implimentation.
It seems to work, but not calling the base implimentation, i cant figure out why the button redraws correctly?
the redraw must have been called from a different handler somewhere in the CButton class.
J
Tom Serface wrote:
Oops. Sorry, I didn't see that you'd figured out a solution. However, you may still want to take a look at this post. It seems to me that detecting the WM_LBUTTONDOWN would be an easier solution. You could just replace CButton in your dialog with your derived class name CButtonPressed (for example)..
http://www.codeguru.com/forum/archive/index.php/t-51872.html
Tom
"JoeB" <joe@xxxxxxxxxx> wrote in message news:enUbugX4GHA.512@xxxxxxxxxxxxxxxxxxxxxxxHi
I decided to sub-classed the button, and handled the buttons OnWndMsg method looking for the BM_SETSTATE message.
When found, i compared the state value with the previous value set in OnWndMsg (stored as a member variable in my sub-class), and if the value is different (state has changed) i send BN_CLICKED to the parent and update the variable.
--- CODE ---
BOOL CButtonSubClass::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
// If the state has changed, tell the parent.
if( message == BM_SETSTATE )
{
if( wParam != m_wLastState ) // State has changed.
{
m_wLastState = wParam;
// Manually update the state.
CButton::SetState(wParam ? true : false);
// Send message to parent informing of the change.
GetParent()->SendMessage(WM_COMMAND, MAKELONG(GetDlgCtrlID(), BN_CLICKED), (LPARAM)m_hWnd);
*pResult = 0;
return 0;
}
}
return CButton::OnWndMsg(message, wParam, lParam, pResult);
}
------------
This allows me to receive button click events each time the button state changed from down-->up, or up-->down.
In the parent OnButtonClicked handler, i then GetState() and look for BST_PUSHED to determine if the button is up or down.
--- CODE ---
void CParentClass::OnBnClickedButton1()
{
bool bPressed = (m_Button.GetState() & BST_PUSHED) ? true : false;
}
------------
When the button is actually clicked normally, i receive two BN_CLICKED messages, both reporting the button is 'up' but that doesn't really matter too much, i can live with that.
Cheers all..
J
Jeff Partch wrote:"JoeB" <joe@xxxxxxxxxx> wrote in message news:OrmlLvU4GHA.512@xxxxxxxxxxxxxxxxxxxxxxxHi,Yes, that's so you can start to click a button and the change your mind by moving the mouse off of it before you release it.
I need to detect the pressing of a button. A standard button. OnBnClicked appears to only be fired when the button is released, and
OnLButtonDown does not appear to be fired at all if pressing a button.WM_LBUTTONDOWN will be sent to the button itself. To handle it you'll need to map it in a subclass. Or... if you can arrange for this button to be created without the WS_EX_NOPARENTNOTIFY extended window style, you can handle the WM_PARENTNOTIFY message in the parent. Remember though that a button can be pressed with the keyboard too.
Anyone think of a different message that i need to handle?A rather wild idea: Handle WM_CTLCOLOR and -- when it's sent by the button of interest, send it a BM_GETSTATE and look for BST_PUSHED.
- Follow-Ups:
- Re: Catching button pressed messages
- From: Tom Serface
- Re: Catching button pressed messages
- References:
- Catching button pressed messages
- From: JoeB
- Re: Catching button pressed messages
- From: Jeff Partch
- Re: Catching button pressed messages
- From: JoeB
- Re: Catching button pressed messages
- From: Tom Serface
- Catching button pressed messages
- Prev by Date: Re: Creating hidden modal property sheet
- Next by Date: Re: dialogs cannot be moved
- Previous by thread: Re: Catching button pressed messages
- Next by thread: Re: Catching button pressed messages
- Index(es):
Relevant Pages
|