2

Possible Duplicate:
Restore a minimized window of another application

I want to restore (show and give focus) to an external program.

The problem is that if it is on the tray the MainWindowHandle is 0, so I can not restore the window.

Process[] process = Process.GetProcessesByName("MyApp");
//process.MainWindowHandle  == 0 if it is on tray!! :(

I've already searched google and stackoverflow. I found some threads with the same question, but there is no answer to this.

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr windowHandle, ShowWindowFlag flag);

How can I pop the external application out of tray and bring it to taskbar?


I can use FindWindow,

    [DllImport("User32.dll", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string className, string windowName);

but if there is more the one process with the same class and title, FindWindow will return only one of them, how can i solve this?

1
  • no, it is not a duplicate, my problem is when the app is hiden, minimized to tray.
    – Pedro77
    Commented Mar 21, 2012 at 15:34

1 Answer 1

5

The thing is, that Windows does not have a support to minimize the window into system tray. There is no such state. And in order to simulate this behavior applications simply hide the windows totally. You can use Spy++ tool to find the window of your target app while it is visible. Then "minimize" it to tray, and see what was changed in the attributes. Then in your app you should revert the attributes. This is needed, because there several ways to hide a window and different apps use one of them.

For example Windows Task Manager change the style from VS_VISIBLE to VS_MINIMIZED (and remove VS_VISIBLE).

5
  • Thanks but, what if the app is already on tray? No way to solve that?
    – Pedro77
    Commented Mar 21, 2012 at 15:50
  • You should perform described steps only once in order to find exact way how to restore the window and it's class and name. Then you can call FindWindow WinAPI function (or equivalent in C#, I'm not sure how it is called), it will return a handle to you, and you can alter the style of the window, to make it visible again. Commented Mar 21, 2012 at 16:05
  • What if there is more than one app running with the same class and name?
    – Pedro77
    Commented Mar 21, 2012 at 16:47
  • Then you have to find which one you need to use. Also for this reason I noted, that you should check window class. But really there is no 100% correct way to determine what window you just have found. I believe you target one particular app, and can find how it works? Commented Mar 21, 2012 at 16:59
  • but FindWindow will return always the same handle, how can I get the other handle if the process have the same class and name? Maybe using FindWindowEx? I'll take a look on this method.
    – Pedro77
    Commented Mar 21, 2012 at 19:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.