views:

681

answers:

4

I have a VB 6 MDI application. It responds to the depricated SwitchToThisWindow function, but not the ShowWindow and SetActiveWindow functions. I know ShowWindow and SetActiveWindow are declared correctly because I can use them with any other application.

EDIT: My goal isn't to use these functions, it is simple to switch the focus from my appliation to the VB 6 application. So if you have any alternative methods I'm all ears.

+1  A: 

SetActiveWindow will only work with windows that are attached to the current thread, so you can't use it to activate another application.

You can bring a window from another application to the foreground with SetForegroundWindow. This will only work if your application is currently in the foreground, but from your question it seems like this is the case.

interjay
That gets me back to the same place as SwitchToThisWindow. But it doesn't work when the window is minimized.
Jonathan Allen
+1  A: 

After activating the parent, you would have to send the WM_MDIACTIVATE message to activate a particular MDI child window. Getting your hands on the MDI child window handles ought to be challenging.

Hans Passant
Thanks, but just getting the parent window up is enough for me.
Jonathan Allen
Use SetForegroundWindow() instead.
Hans Passant
+1  A: 

You can detect whether a window is minimized by using IsIconic(hWnd), and then send ShowWindow(hWnd, SW_RESTORE) to restore the minimized window. Finally use SetForegroundWindow(hWnd) to bring the window to the front.

Here's some excellent VB6 by Karl Peterson that does it all for you.

MarkJ
A: 

You can use some of the code from my answer here: http://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application/2315589#2315589, just change the set active window declaration to the set foreground window function, you can also try using different enums for the ShowWindow function.

Visual Basic 6 definition

 Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long 

Visual Basic .NET definition

 Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer 

C# definition

[DllImport("user32.dll")] public static extern int SetForegroundWindow(int hwnd)  

If your goal isnt to use the user32.dll imports, then your pretty much screwed, because as your application has no access to the target application in order to bring it into focus you will need to use windows.

ALTERNATIVELY you can use some sort of inter process communication system (key words: .net remoting) and code the focus snippet into the target applciation, and then from your application just send the focus message to your second app

Tommy