# Windows FileChooser parent window

**URL:** <https://forum.juce.com/t/windows-filechooser-parent-window/25436>\
**Category:** Windows\
**Created:** [December 5, 2017, 1:39am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436 "2017-12-05T01:39:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![roeland-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/roeland-2/32/738_2.png) [@roeland-2](https://forum.juce.com/u/roeland-2)\
**Post date:** [December 5, 2017, 1:39am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/1 "2017-12-05T01:39:20Z")

</div>

On Windows, native file choosers have the annoying property that they are not automatically focused when the user clicks on the main window while one is open. Also once the file chooser is behind another window, there’s no way to bring it back to focus and your app just appears to hang.

Normally if you show dialogs in Windows, you have to set up an owner `HWND`, eg. for an open dialog that’s most likely your main window.

So let’s apply the following patch to `juce_win32_FileChooser.cpp`. This grabs the current component with keyboard focus and assumes that’s the parent window for the file chooser. It’s a bit hacky but it gets the job done:

[filechooser-5.diff.txt](https://forum.juce.com/uploads/short-url/9AHlp0tb7wiWcPXjpAbjma2Jvmr.txt) (3.4 KB)

The file choosers now behave as expected — if you click the main window, the file chooser will pop to the top and the title bar will flash. No more “hangs”.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 5, 2017, 8:59am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/2 "2017-12-05T08:59:33Z")

</div>

Thanks, didn’t know that was a problem, I’ll have a look at that…

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [December 5, 2017, 9:04am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/3 "2017-12-05T09:04:38Z")

</div>

I tend to have to use something like:

```
m_pParent->getParentComponent()->toFront (true);

```

where m\_pParent is a class member…

```
MainContentComponent* m_pParent;

```

after closing a chooser… so it’s not Windows only.

Rail

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 6, 2017, 9:10am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/4 "2017-12-06T09:10:38Z")

</div>

hmm - just looked at your suggested changes, but you seem to be ignoring the async flag - I think this might break code that uses the file chooser asynchronously (which will before too long be the way we all have to use them). Have you actually tried using it asynchronously, in case that actually works as expected? (as well as being a far safer way to do it compared to the old modal loop approach)

---

<div class="post-metadata">

**Author:** ![roeland-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/roeland-2/32/738_2.png) [@roeland-2](https://forum.juce.com/u/roeland-2)\
**Post date:** [December 6, 2017, 8:51pm UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/5 "2017-12-06T20:51:53Z")

</div>

The idea is that for both modal and asynchronous calls the `HWND` will come from the currently focused component.

I did notice that when using `launchAsync()` the resulting dialog is the old Windows 95-style dialog, which is a consequence of using the `OFN_ENABLEHOOK` flag.

Anyway, file choosers on Windows by definition run a modal loop one way or another, so there using the modal versions will probably be the safer choice.

---

<div class="post-metadata">

**Author:** ![roeland-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/roeland-2/32/738_2.png) [@roeland-2](https://forum.juce.com/u/roeland-2)\
**Post date:** [March 15, 2018, 10:03pm UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/6 "2018-03-15T22:03:06Z")

</div>

An alternative solution is to override `inputAttemptWhenModal()` so it raises the file chooser.

```auto
    virtual void inputAttemptWhenModal() override
    {
        // find handle of the native popup window
        HWND hwnd = GetWindow((HWND) getWindowHandle(), GW_ENABLEDPOPUP);
        jassert(hwnd);
        if (!hwnd) return;
        
        FLASHWINFO fw = { sizeof(fw) };
        fw.hwnd = hwnd;
        fw.dwFlags = FLASHW_CAPTION;
        fw.uCount = 6;
        fw.dwTimeout = 70;
        BringWindowToTop(hwnd);
        FlashWindowEx(&fw);
    }

```

This works much better if there are multiple top-level windows.

---

<div class="post-metadata">

**Author:** ![fuo](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@fuo](https://forum.juce.com/u/fuo)\
**Post date:** [February 3, 2022, 8:41am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/7 "2022-02-03T08:41:46Z")

</div>

Sorry to bump this old thread, but the issue is still relevant today.  
I manually applied the patch provided in first post to the current version of the file, but this does not seem to solve the issue.  
Is there a way to make the native file chooser either always on top of the main (or in my case plugin) window? Or alternatively make it pop on top when clicking said window?

---

<div class="post-metadata">

**Author:** ![fuo](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@fuo](https://forum.juce.com/u/fuo)\
**Post date:** [February 12, 2022, 2:34pm UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/8 "2022-02-12T14:34:05Z")

</div>

Bump.  
The demo runner has the same issue, and I feel like it should really be addressed within JUCE…

---

<div class="post-metadata">

**Author:** ![roeland-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/roeland-2/32/738_2.png) [@roeland-2](https://forum.juce.com/u/roeland-2)\
**Post date:** [February 21, 2022, 9:02pm UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/9 "2022-02-21T21:02:51Z")

</div>

It is quite unfortunate that this still isn’t fixed. Did you also try to use the `inputAttemptWhenModal()` function as described above?

---

<div class="post-metadata">

**Author:** ![fuo](https://avatars.discourse-cdn.com/v4/letter/f/ba8739/32.png) [@fuo](https://forum.juce.com/u/fuo)\
**Post date:** [February 22, 2022, 4:42am UTC](https://forum.juce.com/t/windows-filechooser-parent-window/25436/10 "2022-02-22T04:42:40Z")

</div>

Hi [roeland](https://forum.juce.com/u/roeland), thank you for your response.

Yes I did, I tried putting it in every component from the editor down to the child components but it never gets called: the editor window seems frozen. My application is a plugin, and I get this behavior both in standalone and plugin modes.
