如何将程序窗口最小化为托盘

时间:2012-03-21 15:23:44

标签: c# restore system-tray

  

可能重复:
  Restore a minimized window of another application

我想恢复(显示并给予焦点)外部程序。

问题是,如果它在托盘上MainWindowHandle为0,那么我无法恢复窗口。

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

我已经搜索了google和stackoverflow。我发现一些线程有同样的问题,但没有答案。

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

如何从托盘中弹出外部应用程序并将其带到任务栏?


我可以使用FindWindow,

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

但是如果有一个进程具有相同的类和标题,FindWindow将只返回其中一个,我该如何解决?

1 个答案:

答案 0 :(得分:4)

问题是,Windows没有支持将窗口最小化到系统托盘中。没有这样的状态。为了模拟这种行为,应用程序只需完全隐藏窗口。 您可以使用Spy ++工具查找目标应用程序可见的窗口。 然后将其“最小化”到托盘,并查看属性中的更改内容。然后在您的应用程序中,您应该还原属性。这是必需的,因为有几种隐藏窗口的方法,不同的应用程序使用其中一种。

例如,Windows任务管理器将样式从VS_VISIBLE更改为VS_MINIMIZED(并删除VS_VISIBLE)。