SetWindowPos实现

时间:2012-08-22 15:55:14

标签: c# .net multiple-monitors

我需要打开两个互联网浏览器实例,每个实例在控制台应用程序的不同显示器(有两个)中打开。我发现SetWindowPos方法并没有找到使用它的方法。就我而言,它没有做任何事......

请帮助我正确使用这种方法...

以下是我正在使用的代码:

[DllImport("user32.dll")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

    public static void Launch()
    {
        Process process = new Process();

        process.StartInfo.FileName = "iexplore.exe";
        process.StartInfo.Arguments = "microsoft.com";
        process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;


        process.Start();

        Rectangle monitor = Screen.AllScreens[1].WorkingArea;
        SetWindowPos(process.MainWindowHandle, 0, monitor.Left, monitor.Top, monitor.Width - 200, monitor.Height, 0);
    }

谢谢大卫

2 个答案:

答案 0 :(得分:0)

您传递给该方法的窗口句柄为空,因为该进程没有时间打开其主窗口。

尝试在调用SetWindowPos之前添加合理的超时,一两秒就足够了:

process.Start();

System.Threading.Thread.Sleep(1000);
process.WaitForInputIdle(); // just in case

SetWindowPos(...);

答案 1 :(得分:0)

此代码可用于例如notepad.exe。由于process.MainWindowHandle == IntPtr.Zeroprocess.HasExited == true,iexplore.exe无效。您需要找到找到窗口句柄的正确方法。

相关问题