I have a dialog based MFC application. I have a hidden Edit Control on it which has the default focus for reading USB connected scanner input. All other controls on the dialog are read only. But when user clicks on any of the controls, the hidden edit control loses the focus. How can I solve the problem, so that this hidden edit control has the focus even after user clicks on the dialog box anywhere.
Asked
Active
Viewed 1,528 times
2
-
One would use read-only controls when you would want to let the user select/copy text in the controls, otherwise the controls would normally be disabled -- can you disable these other controls? – Edward Clements Sep 15 '15 at 15:32
-
You want to *block input*? Have you considered calling [BlockInput](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646290.aspx)? Apart from that, if controls are read-only, the user will understand that they can read, and **copy** contents. If that breaks your application you may wish to rework the nefarious code you have in place. Apart from that, a window that is disabled doesn't get input. But really, you should definitely rethink your design. Would the current implementation play well with Assistive Technologies? – IInspectable Sep 15 '15 at 15:57
-
@Edward Clements All my controls are read-only, but I cannot disable them. – SGP Sep 15 '15 at 17:35
-
@IInspectable Using BlockInput() not only blocked input for my application, it blocked all the inputs for windows. – SGP Sep 15 '15 at 17:35
-
Why does that hidden edit control have to have a focus in order to read USB? – Vlad Feinstein Sep 16 '15 at 18:46
-
@Vlad Feinstein yes, it needs to have focus to read USB scanner output. – SGP Sep 19 '15 at 05:03
1 Answers
4
Well, most mouse messages in an MFC application are posted and not sent. So, you could override PreTranslateMessage(MSG*) in your CDialog derived class and then eat those message by returning TRUE to prevent message dispatch or FALSE to allow normal processing.
BOOLCMyDlg::PreTranslateMessage(MSG* pMsg)
{
switch (pMsg->message)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
///etc, etc
{
// might want other tests
return TRUE;
} break;
}
return CDialog::PreTranslateMessage(MSG* pMsg);
}

Joseph Willcoxson
- 5,853
- 1
- 15
- 29
-
This solution worked perfectly for me. Among your "etc, etc" users are likely to want WM_LBUTTONDBLCLK as well, and perhaps also WM_KEYDOWN. – Dragonfly Oct 20 '22 at 23:42