在运行时切换 Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden [英] Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime

查看:41
本文介绍了在运行时切换 Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时切换进程的可见性,我有一个 Windows 窗体应用程序,它通过一个进程启动另一个默认隐藏的控制台应用程序,但我想允许管理员用户切换这个通过复选框声明并在他们选择时显示控制台应用程序.

I want to toggle a process's visibility at runtime, I have a Windows Form app that starts via a process another console app hidden by default but I'd like to allow the admin user to toggle this state via a checkbox and show the console app if they choose.

我有这个,但它不起作用:

I have this but it's not working:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        ProcessWindowStyle state = cvarDataServiceProcess.StartInfo.WindowStyle;
        if (state == ProcessWindowStyle.Hidden)
            cvarDataServiceProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        else if (state == ProcessWindowStyle.Normal)
            cvarDataServiceProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;            
    }

推荐答案

您必须为此使用 Win32 API.

You have to use Win32 API for this.

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    ProcessWindowStyle state = ProcessWindowStyle.Normal;

    void toggle()
    {
        if (cvarDataServiceProcess.HasExited)
        {
            MessageBox.Show("terminated");
        }
        else
        {
            if (cvarDataServiceProcess.MainWindowHandle != IntPtr.Zero)
            {
                if (state == ProcessWindowStyle.Hidden)
                {
                    //normal
                    state = ProcessWindowStyle.Normal;
                    ShowWindow(cvarDataServiceProcess.MainWindowHandle, 1);
                }
                else if (state == ProcessWindowStyle.Normal)
                {
                    //hidden
                    state = ProcessWindowStyle.Hidden;
                    ShowWindow(cvarDataServiceProcess.MainWindowHandle, 0);
                }
            }
        }
    }

然而,这在进程启动时不起作用hidden,因为不会创建窗口并且主窗口的句柄为零(无效).
因此,也许您可​​以正常启动该过程,然后将其隐藏.:)

This, however, will not work when the process is started hidden, because the window will not be created and the handle to main window will be zero (invalid).
So, maybe you can start the process normally and then hide it after that. :)

这篇关于在运行时切换 Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆