[c#]StartInfo.WindowStyle = ProcessWindowStyle.Hidden 仍不工作

标签: windows C#
发布时间: 2013/12/8 3:24:51
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
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 属性窗口"和局域网设置窗口后它叫,但此代码不起作用。帮帮我!

解决方法 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++ 来知道控件的 id OK and LAN settings 的。控件 ID OK1 和控件 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、 làm (干草 vẫn đang học?) em trong ngành 锡 thật à?Ở đâu vậy?多大?很高兴认识你堆栈上超过流量:)

赞助商