Click here to Skip to main content
15,889,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know how to set the focus to an edit control, but is there a way to check if an edit control has focus?
Posted

Use the Getfocus[^] function to get the windows that currently has the input focus and compare the result with the edit control your using.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-12 12:30pm    
We completed out posts almost at the same time; I advised the same thing.
My 5.
--SA
DrBones69 1-Apr-12 13:36pm    
Thanks, it appears that I hadn't had my coffee yet. ;)
I guess you found out the function SetFocus:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646312%28v=vs.85%29.aspx[^].

If you did, I don't know how could you miss GetFocus referenced from that page:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646294%28v=vs.85%29.aspx[^].

The focus means keyboard focus; and only one control at a time in the whole system can have the focus. So, you find out HWND of the window which currently has focus and compare with HWND of the control you are interested in. If they are the same, your control is currently focused.

[EDIT]

DrBones69 asked:
I know how to set the focus to an edit control, but is there a way to check if an edit control has focus?

This would be another way. One can get CWnd::GetFocus, but still compare HWND of the result with HWND of the control in question. Please see:
http://msdn.microsoft.com/en-US/library/f9fd0z3x%28v=vs.100%29.aspx[^],
http://msdn.microsoft.com/en-us/library/bcs64shx.aspx[^].

In more general case, it's good to use CWnd::GetSafeHwnd, but the result CWnd::GetFocus will already return NULL in case of none is focused, and this result should be tested for NULL. Anyway, see also:
http://msdn.microsoft.com/en-us/library/d64ehwhz.aspx[^].

—SA
 
Share this answer
 
v4
Comments
DrBones69 1-Apr-12 13:37pm    
Isn't CWnd used instead of HWND?
Sergey Alexandrovich Kryukov 1-Apr-12 14:55pm    
HWND -- look at the profiles of the functions. With CWnd -- why not? -- please see my updated answer. Anyway, the comparison will actually be done by HWND; there is HWND operator. It's also good to use CWnd::GetSafeHwnd.
Thank you for the suggestion.
--SA

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