切换Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden在运行时 [英] Toggle Process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden at runtime
问题描述
我要为在运行时切换过程的可视性,我有通过另一个控制台应用程序默认是隐藏的,但我想,让管理员用户切换此一进程启动Windows窗体应用程序通过一个复选框状态和显示控制台应用程序,如果他们选择。
我有这个,但它不工作:
私人无效checkBox1_CheckedChanged(对象发件人,EventArgs的发送)
{
ProcessWindowStyle状态= cvarDataServiceProcess.StartInfo.WindowStyle;
如果(状态== ProcessWindowStyle.Hidden)
cvarDataServiceProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
否则,如果(状态== ProcessWindowStyle.Normal)
cvarDataServiceProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
}
您必须使用Win32 API的这一点。
函数[DllImport(user32.dll中)]
静态外部布尔的ShowWindow(IntPtr的的HWND,INT的nCmdShow); ProcessWindowStyle状态= ProcessWindowStyle.Normal; 无效切换()
{
如果(cvarDataServiceProcess.HasExited)
{
的MessageBox.show(封端的);
}
其他
{
如果(cvarDataServiceProcess.MainWindowHandle!= IntPtr.Zero)
{
如果(状态== ProcessWindowStyle.Hidden)
{
//正常
状态= ProcessWindowStyle.Normal;
的ShowWindow(cvarDataServiceProcess.MainWindowHandle,1);
}
否则,如果(状态== ProcessWindowStyle.Normal)
{
//隐
状态= ProcessWindowStyle.Hidden;
的ShowWindow(cvarDataServiceProcess.MainWindowHandle,0);
}
}
}
}
不过,这不会在进程启动工作隐藏
,因为不会被创建的窗口句柄到主窗口将为零(无效)。
所以,也许可以正常启动过程并在那之后将其隐藏。 :)
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;
}
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);
}
}
}
}
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屋!