Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've tried googling this, but I can't seem to come up with an answer. I'm developing a program and need to be able to detect when a CButton control receives focus (i.e. gets tabbed onto). Is there a way to do this short of sub-classing CButton?

If it matters, I need to be able to display certain things when a button receives focus, and then set focus the focus to another control when the user actually clicks on the button or hits the return key when that button is the active one.

I thought about trying to override the default behavior of WM_NEXTDLGCTL message handler, but I can't seem to find a way to do that. Does anybody have any other ideas? I'd like to avoid sub classing if possible because I only need this behavior for 3 buttons in a much larger program, and it's becoming confusing with all the extra classes that already exist.

Thanks for any help in advance!
Posted

1 solution

I have used MFC much... but the following link should help...

See OnSetFocus[^]

Message Maps (MFC)[^]

Handlers for WM_ Messages[^]

The message that will interest you is WM_SETFOCUS and possibly WM_KILLFOCUS.

I don't think that WM_NEXTDLGCTL will help as it is intended to control the navigation order.
 
Share this answer
 
Comments
apocalyptic.angell 19-Jul-11 11:27am    
I looked at those in depth before posting here. Unfortunately there is no way for me to handle the WM_SETFOCUS message that is sent to the button when it recieves focus in the dialog class of the dialog containing the button. To be able to trap it and do something about it in the dialog class I'd need to subclass the CButton class, add a handler for the WM_SETFOCUS message which would then send a message to notify the dialog class, which is what I'm hoping to avoid. I was thinking that if I could trap the WM_NEXTDLGCTL message I'd know that focus was changing and I would be able to check which control is receiving focus and preform the special actions if it's one that I'm interested in. Thanks for taking the time to help though!
Philippe Mori 19-Jul-11 13:15pm    
I guess it would not works if the focus is moved using the mouse for example.
Philippe Mori 19-Jul-11 13:18pm    
I think you should be able to handle notifications like EN_SETFOCUS, BN_SETFOCUS...
apocalyptic.angell 19-Jul-11 13:18pm    
Yeah, now that you mention that, you're probably right. I guess I'll just have to subclass the button. Oh well it was worth a shot.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900