I have two simple programs written in C running at the same time. The first program has a GUI window, while the second program has no window (hidden).
The first program (GUI) sends a TCP command to the second program (hidden), and the second program opens a file dialogue using Win32 API GetOpenFileNameA()
. The issue is this file dialogue box shows behind the GUI window.
Question:
How do I run GetOpenFileNameA()
and force focus on it?
OPENFILENAMEA ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = FILE_PATH;
ofn.nMaxFile = sizeof(FILE_PATH);
ofn.lpstrFilter = "All Files\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn)) {
return FILE_PATH;
}
>Solution :
You could probably force it on top by providing a callback function for messages intended for the dialog box:
UINT_PTR OFNHookProcOldStyle(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
BringWindowToTop(hdlg); // or SetWindowPos
return 0;
}
Then set
ofn.lpfnHook = OFNHookProcOldStyle;
before calling GetOpenFileNameA
.