Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

GetOpenFileName Focus

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading