I have the dialog shown with ShowWindow(hWnd, SW_SHOWNOACTIVATE); But it doesn't work, the new dialog still steals the focus, why is it?
here is the some code snippets from my program, QueryWindow is the MFC dialog class linked with the dialog:
QueryWindow window;
//window.DoModal();
window.Create(QueryWindow::IDD);
window.ShowWindow(SW_SHOWNOACTIVATE);
To create a modal dialog box, call either of the two public constructors declared in CDialog. Next, call the dialog object's DoModal member function to display the dialog box and manage interaction with it until the user chooses OK or Cancel. This management by DoModal is what makes the dialog box modal.
To create a modeless dialog box, call your public constructor and then call the dialog object's Create member function to load the dialog resource. You can call Create either during or after the constructor call. If the dialog resource has the property WS_VISIBLE, the dialog box appears immediately.
A modal dialog box closes when the user chooses one of its buttons, typically the OK button or the Cancel button. Choosing the OK or Cancel button causes Windows to send the dialog object a BN_CLICKED control-notification message with the button's ID, either IDOK or IDCANCEL.
MFC supports both kinds of dialog box with class CDialog . The controls are arranged and managed using a dialog-template resource, created with the dialog editor. Property sheets, also known as tab dialog boxes, are dialog boxes that contain "pages" of distinct dialog-box controls.
There are few ways to skip dialog from getting focused:
Make you OnInitDialog() to return zero value. Example:
BOOL QueryWindow::OnInitDialog()
{
CDialog::OnInitDialog();
return FALSE; // return 0 to tell MFC not to activate dialog window
}
This is the best and most correct solution.
Add WS_EX_NOACTIVATE style to your dialog window. You can edit dialog resource properties or change it in runtime:
BOOL QueryWindow::PreCreateWindow(CREATESTRUCT& cs)
{
cs.dwExStyle |= WS_EX_NOACTIVATE;
return CDialog::PreCreateWindow(cs);
}
Side-effect: you can use controls on your window, but it will look like it was not activated.
Last way is to save foreground window before creating your dialog and set foreground window at the end:
BOOL QueryWindow::Create(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
{
CWnd* pForeground = GetForegroundWindow();
const BOOL bRes = CAlertDialog::Create(lpszTemplateName, pParentWnd);
if(pForeground)
pForeground->SetForegroundWindow();
return bRes;
}
This is the worth solution because potentially you can get flicker.
Important!
Don't forget to control following API calls:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With