StartInfo.WindowStyle = ProcessWindowStyle.Hidden仍然无效

时间:2013-06-15 14:36:34

标签: c# .net process processstartinfo

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.UseShellExecute = false;
proc.CreateNoWindow = true;
Process.Start(proc);
SendKeys.Send("{TAB}{TAB}{TAB}{TAB}{ENTER}");// Open Lan Setting window

我已经尝试过很多方法来隐藏/关闭“Internet属性窗口”和Lan设置窗口,但是这个代码不起作用。 救救我!

1 个答案:

答案 0 :(得分:1)

我认为你应该找到另一种方式来解决你的代理问题,但是如果你仍然想要使用显示和关闭对话框的技巧,我在这里有一个解决方案,这将有所帮助(我已经测试过)和你不必使用我觉得非常不稳定的SendKeys。这是我的代码:

//You have to add these using first:
//using System.Runtime.InteropServices;
//using System.Threading;
//This is used to find a window
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern IntPtr FindWindow(string className, string windowName);
//This is used to find Button in a window
[DllImport("user32")]    
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);
//This is to help you Click on a Button
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);    
//---------------------------
//Here are our methods 
//---------------------------    
//This is used to Get handle of a Button of a Parent window by its Text
private IntPtr GetButton(IntPtr parent, string text)
{
   return FindWindowEx(parent, IntPtr.Zero, "Button", text);
}
//This is used to Click on a Window (usually a Button) with its Handle passed in
private void ClickWindow(IntPtr hwnd)
{
   SendMessage(hwnd, 0x201, IntPtr.Zero, IntPtr.Zero);
   SendMessage(hwnd, 0x202, IntPtr.Zero, IntPtr.Zero);
}
//This is used to find the Local Area Network (LAN) Settings window
//This is called in a separate thread, because somehow the LAN settings window
//showing causes the main Form not-responding (we can't call anything in our main thread).
private void SearchForLanSettingsWindow()
{
        int i = 0;
        while (true)
        {
            Thread.Sleep(100);
            IntPtr windowHandle = FindWindow(null,"Local Area Network (LAN) Settings");
            if (windowHandle != IntPtr.Zero)
            {
                //Find the button OK, if you like, you can replace it with "Cancel",...
                IntPtr button = GetButton(windowHandle, "OK");
                //Click on that OK button to Close your Lan settings window
                //You may want to research on the DestroyWindow or CloseWindow
                //win32 api without having to click on a Button, but I think this should be better. It's up to you.
                ClickWindow(button);
                break;
            }
            i++;
            if (i > 20)//timeout
            {
                break;
            }
        }
}
//And here is your code with my code appended
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window        
proc.UseShellExecute = false;
System.Diagnostics.Process.Start(proc);

Thread.Sleep(100);//Sleep to be sure the Window is really created
//Get the handle to the window "Internet Properties"
IntPtr mainHandle = FindWindow(null, "Internet Properties");
//Find the tab "Connections", this tab has class "#32770" and is a child window of the window "Internet Properties"
IntPtr child = FindWindowEx(mainHandle, IntPtr.Zero, "#32770", "Connections");
//Get the button "LAN settings"
IntPtr button = GetButton(child, "&LAN settings");
//Create new thread and start it to find the Lan settings window 
//we have to do this now because for some reason, after the LAN settings window shows
//we can't call any code in our class.
new Thread(SearchForLanSettingsWindow).Start();
//Click on the LAN settings button to Show the LAN settings window
ClickWindow(button);
//Get the button OK on the window "Internet Properties"
button = GetButton(mainHandle, "OK");
//Click on that button to close the window "Internet Properties"
ClickWindow(button);

就是这样。

我发现如果计算机安装的是非英语语言,则按钮OK, LAN settings可能会有所不同。因此,更好的解决方案是使用GetDlgItem()从ID中获取按钮。为此,您必须先导入函数GetDlgItem()

[DllImport("user32")]
private static extern IntPtr GetDlgItem(IntPtr dlgHandle, int itemID);

我使用Spy++来了解OK and LAN settings的控制ID是什么。 OK的控件ID为1,控件ID为LAN settings0x62C。因此,要获取这些按钮的句柄,您可以使用以下代码:

IntPtr button = GetDlgItem(parent, 1);//OK button
button = GetDlgItem(parent, 0x62C);//LAN settings, remember that the dialog containing LAN settings button is Connections not the Internet Properties.

以下是使用Process.Kill()的另一种解决方案,我不确定是否可以杀死RunDll32.exe,但是如果可以,这将是另一个更清晰的解决方案:

//You have to add these using first:
//using System.Runtime.InteropServices;
//using System.Diagnostics;
//using System.Threading;
//This is used to find a window
[DllImport("user32", CharSet=CharSet.Auto)]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32")]    
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);
//This is to help you Click on a Button
[DllImport("user32")]
private static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam);
//This is used to get a Button (as an item) on a dialog
[DllImport("user32")]
private static extern IntPtr GetDlgItem(IntPtr dlgHandle, int itemID);
//---------------------------
//Here are our methods 
//---------------------------    
//This is used to Click on a Window (usually a Button) with its Handle passed in
private void ClickWindow(IntPtr hwnd)
{
   SendMessage(hwnd, 0x201, IntPtr.Zero, IntPtr.Zero);
   SendMessage(hwnd, 0x202, IntPtr.Zero, IntPtr.Zero);
}
//This is used to find the Local Area Network (LAN) Settings window
//This is called in a separate thread, because somehow the LAN settings window
//showing causes the main Form not-responding (we can't call anything in our main thread).
private void SearchForLanSettingsWindow()
{
    int i = 0;
    while (i < 20)
    {
        Thread.Sleep(100);
        IntPtr windowHandle = FindWindow(null,"Local Area Network (LAN) Settings");
        if (windowHandle != IntPtr.Zero)
        {
            if(runDll32 != null) runDll32.Kill();
            break;
        }
        i++;
    }
}
//the Process RunDll32
Process runDll32;
//And here is your code with my code appended
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\\Windows\\System32\\RunDll32.exe";
proc.Arguments = "shell32.dll,Control_RunDLL inetcpl.cpl,Internet,4";//open Internet Properties window        
proc.UseShellExecute = false;
runDll32 = Process.Start(proc);

Thread.Sleep(100);//Sleep to be sure the Window is really created
//Get the handle to the window "Internet Properties"
IntPtr mainHandle = FindWindow(null, "Internet Properties");
//Find the tab "Connections", this tab has class "#32770" and is a child window of the window "Internet Properties"
IntPtr child = FindWindowEx(mainHandle, IntPtr.Zero, "#32770", "Connections");
//Get the button "LAN settings"
IntPtr button = GetDlgItem(child, 0x62C);
//Create new thread and start it to find the Lan settings window 
//we have to do this now because for some reason, after the LAN settings window shows
//we can't call any code in our class.
new Thread(SearchForLanSettingsWindow).Start();
//Click on the LAN settings button to Show the LAN settings window
ClickWindow(button);

我想,您应该找到另一种解决方案来完成您原本想要的工作。这种解决方案只是一个技巧。您可能希望使用MoveWindow win32函数将所有对话框移出屏幕。

PS:này,emlàm(hayvẫnđanghọc?)trongngànhtitthậtà? Ởđâuvậy?几岁?很高兴见到你在堆栈上流过:)