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

标签 c# process hide show show-hide

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

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

最后我想使用 C# 显示/最大化!

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

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

  1. 显示窗口
  2. 动画窗口
  3. 动画窗口
  4. 设置前景窗口
  5. 设置窗口位置

我遇到了同样的问题。

我可以隐藏窗口(虽然我必须使用 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 是否有最小化此最小化和最大化此窗口的命令?

最佳答案

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

您可以通过了解语音识别 winwow 名称然后使用如下内容来做到这一点:

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

关于c# - 如何使用 C# 隐藏/显示进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3032170/

相关文章:

c# - 将 Shapes.Path 项目绑定(bind)到 ItemsControl

c# - 当您没有类的源代码时,是否可以对对象进行 .NET 二进制序列化?

c# - (C# 'Random' 是一个命名空间,但像变量一样使用)我不明白为什么

javascript - 使用jquery连续显示单词

ios - 按下按钮时隐藏键盘

jquery通过id显示/隐藏多个元素

c# - 正则表达式匹配 C# 代码中的 SQL 关键字

python - 在Windows python 2.5中杀死一个进程

linux - linux中的top -c命令过滤基于进程名列出的进程

PHP 使用 proc_open 并捕获 'echo' 命令的标准输出