如何使用c#隐藏/显示进程?

7

在执行我的程序时,我想隐藏/最小化Microsoft语音识别应用程序:

alt text http://img143.imageshack.us/img143/9380/minimize.png

最后,我想使用c#显示/最大化。

这个进程不是由我启动的,所以我无法控制进程startInfo。

我尝试使用user32.dll方法,例如:

  1. ShowWindow
  2. AnimatedWindows
  3. AnimatedWindows
  4. SetForegroundWindow
  5. SetWindowPos

但是所有这些方法都有同样的问题。

我可以隐藏窗口(虽然我必须使用SW_HIDE选项调用其中一种方法两次),但当我使用SW_SHOW标志调用该方法时,它根本不显示。

如何在隐藏进程后进行最大化/显示?

提前感谢您!

这里是一些代码片段,现在已实现使用SetWindowPlacement:

{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetWindowPlacement(IntPtr hWnd,
       [In] ref WINDOWPLACEMENT lpwndpl);
    [DllImport("user32.dll")]
    public static extern Boolean ShowWindowAsync(IntPtr hWnd, Int32 nCmdShow);
    [DllImport("user32.dll")]
    public static extern Boolean SetForegroundWindow(IntPtr hWnd);        
    [DllImport("user32.dll")]
    public static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
    [DllImport("user32.dll")]
    public static extern Boolean AnimateWindow(IntPtr hWnd, uint dwTime, uint dwFlags);
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, uint dwAttribute, IntPtr pvAttribute, IntPtr lol);
//Definitions For Different Window Placement Constants
const UInt32 SW_HIDE = 0;
const UInt32 SW_SHOWNORMAL = 1;
const UInt32 SW_NORMAL = 1;
const UInt32 SW_SHOWMINIMIZED = 2;
const UInt32 SW_SHOWMAXIMIZED = 3;
const UInt32 SW_MAXIMIZE = 3;
const UInt32 SW_SHOWNOACTIVATE = 4;
const UInt32 SW_SHOW = 5;
const UInt32 SW_MINIMIZE = 6;
const UInt32 SW_SHOWMINNOACTIVE = 7;
const UInt32 SW_SHOWNA = 8;
const UInt32 SW_RESTORE = 9;

public sealed class AnimateWindowFlags
{
    public const int AW_HOR_POSITIVE = 0x00000001;
    public const int AW_HOR_NEGATIVE = 0x00000002;
    public const int AW_VER_POSITIVE = 0x00000004;
    public const int AW_VER_NEGATIVE = 0x00000008;
    public const int AW_CENTER = 0x00000010;
    public const int AW_HIDE = 0x00010000;
    public const int AW_ACTIVATE = 0x00020000;
    public const int AW_SLIDE = 0x00040000;
    public const int AW_BLEND = 0x00080000;
}

public struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public System.Drawing.Point ptMinPosition;
    public System.Drawing.Point ptMaxPosition;
    public System.Drawing.Rectangle rcNormalPosition;
}


            //this works

            param = new WINDOWPLACEMENT();
            param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
            param.showCmd = (int)SW_HIDE;
            lol = SetWindowPlacement(theprocess.MainWindowHandle, ref param);


            // this doesn't work

            WINDOWPLACEMENT param = new WINDOWPLACEMENT();
            param.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
            param.showCmd = SW_SHOW;
            lol = GetWindowPlacement(theprocess.MainWindowHandle, ref param);

注意: SAPI API 是否有一个命令来最小化这个窗口并将其最大化?

3个回答

2

正如Tomas所说,你应该尝试使用SW_HIDE和SW_SHOW消息。

你可以通过了解语音识别窗口名称并使用以下代码实现:

HWND hc = FindWindow("processname","Windowtitle"); 
ShowWindow(hc,SW_HIDE);

我该如何发现窗口标题? - aF.
简单的方法是加载Windows的任务管理器并在其中查看。再看一下这两个链接:http://msdn.microsoft.com/en-us/library/ms633499%28VS.85%29.aspx http://www.recursosvisualbasic.com.ar/htm/listado-api/88-hwnd-class-name-parent.htm - Juan Nunez

1

我忘了提到那个。我也用了它! - aF.

1

如果您发送SW_HIDE消息,进程是否仍在运行?该应用程序肯定没有使用标准GUI样式,因此它可能通过关闭自身来响应该消息。

如果是这种情况,您可以尝试其他技巧,例如将窗口移动到某个不可见的位置(例如-1000,-1000),这也应该可以使用您已经导入的SetWindowPlacement方法实现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接