简体   繁体   English

SetWindowPos实现

[英]SetWindowPos implementation

I need open two instances of internet browser and each instance open in different monitor (have two) from console app. 我需要打开两个Internet浏览器实例,每个实例都从控制台应用程序在不同的监视器(有两个)中打开。 I found SetWindowPos method and can't find way to use it. 我找到了SetWindowPos方法,找不到使用它的方法。 In my case it doesn't do anything... 就我而言,它什么也没做...

Please help me way to right using of this method... 请帮助我正确使用此方法...

Here is the code what I'm using for: 这是我正在使用的代码:

[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);
    }

Thanks David 谢谢大卫

That window handle you're passing to the method is empty as the process has not had time to open its main window. 您要传递给该方法的窗口句柄为空,因为该进程没有时间打开其主窗口。

Try adding a reasonable timeout before calling SetWindowPos, a second or two should be enough: 尝试在调用SetWindowPos之前添加一个合理的超时时间,一两秒钟就足够了:

process.Start();

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

SetWindowPos(...);

This code would work, for example, with notepad.exe. 例如,此代码可与notepad.exe一起使用。 With iexplore.exe is not working because process.MainWindowHandle == IntPtr.Zero , and process.HasExited == true . 使用iexplore.exe无法正常工作,因为process.MainWindowHandle == IntPtr.Zeroprocess.HasExited == true You need to find the right way to find a window handle. 您需要找到找到窗口句柄的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM