Re: Catching button pressed messages

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxxxxxxxxxxxxxxxxxxxxxx
Hi

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@xxxxxxxxxxxxxxxxxxxxxxx
Hi,

I need to detect the pressing of a button. A standard button. OnBnClicked appears to only be fired when the button is released, and
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.

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.



.



Relevant Pages

  • Re: Catching button pressed messages
    ... You may want to take a look at a CCheckBox with BS_PUSHLIKE style set. ... the redraw must have been called from a different handler somewhere in the ... BOOL CButtonSubClass::OnWndMsg(UINT message, WPARAM wParam, LPARAM ... // If the state has changed, tell the parent. ...
    (microsoft.public.vc.mfc)
  • Re: Catching button pressed messages
    ... 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, and if the value is different i send BN_CLICKED to the parent and update the variable. ... BOOL CButtonSubClass::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult) ... In the parent OnButtonClicked handler, i then GetStateand look for BST_PUSHED to determine if the button is up or down. ...
    (microsoft.public.vc.mfc)
  • Accessing or Calling a Parents Methods or Functions from the Child (Update)
    ... Send or Post a Message to the Parent. ... The lParam and wParam are long values and can be used to pass any parameters ... The handler must have exactly this prototype: ...
    (microsoft.public.vc.mfc)
  • Accessing or Calling a Parents Methods or Functions from the Child (Update again)
    ... Send or Post a Message to the Parent. ... The lParam and wParam are long values and can be used to pass any parameters ... The handler must have exactly this prototype: ...
    (microsoft.public.vc.mfc)
  • Re: fork() race in SIGCHLD handler
    ... > It sounds like you should delay setting the handler in the parent till ... > the fork has returned, but that leaves a window in which the child can ... before the parent fork() has returned, so I will definitely miss it. ... Compiler issues will intervene to make that cloudy. ...
    (comp.os.linux.development.system)