Need Some Help With Windows Cross-Program Focus Issue

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Need Some Help With Windows Cross-Program Focus Issue

Post by Xangis » Fri Jan 20, 2012 8:47 pm

I have two applications, one is a wxWidgets-based add-on to another that communicates over the network. The two apps have to remain separate codebases/processes (one's mine and one isn't).

At a certain point, when the user clicks a button in the main application, I need to bring up a file browse dialog in the addon.

The trouble is that in Windows, one app stealing focus from another has been locked down and this browse dialog shows up behind the main app. I thought I had a workaround by calling AllowSetForegroundProcess() in the main app, passing in the process ID of the addon, but what I'm trying isn't working. I suspect it's because a wxFileDialog is modal, so anything I try has to be done before the dialog is shown and because there's no dialog showing when I call focus-changing function calls, they get ignored.

Here's what I've tried running from the addon program:
SetForegroundWindow()
SwitchToThisWindow()
BringWindowToTop()

Dropping down to Windows-specific code is an option (I experimented with using GetOpenFileName but since that's what wx uses, it didn't behave differently).

Maybe it would work if I created a non-modal dialog first, caused that to get focus, then show the file dialog? Assuming that the AllowSetForegroundProcess / SetForegroundWindow call combination would work for a non-modal window, which I'm not sure about. Any thoughts or suggestions on this one?

I have a suspicion that I need to create my own focus-stealing file browse dialog from scratch, but I'm hesitant to build it if it won't be able to get focus since it's likely to be a lot of work.
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]

User avatar
doublemax
Moderator
Moderator
Posts: 14624
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Need Some Help With Windows Cross-Program Focus Issue

Post by doublemax » Fri Jan 20, 2012 9:14 pm

i've read your post several times, but i'm still not sure i understood what's happening.

The add-on is the application you have control over and that's written in wxWidgets?

I'd try this:
- remember the HWND of the current foreground window (Win32 api)
- if the add-on has a toplevel-window, display that first
- if it doesn't have a toplevel window, create and display a dummy toplevel window (can be displayed outside the visible screen)
- display the filedialog
- close dummy-window
- when your add-on is done, restore focus to the main application (Win32 api)
Use the source, Luke!

Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Re: Need Some Help With Windows Cross-Program Focus Issue

Post by Xangis » Fri Jan 20, 2012 9:48 pm

Yes, the addon is wx and the one I control.

I'll try the dummy window trick. Thanks. :)
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]

Post Reply