2010-03-19 84 views
0

我創建了一個新的實例,並試圖重新調整瀏覽器的新實例是這樣的:如何cpecified位置開始新的瀏覽器窗口蒙山cpecified大小

[System.Runtime.InteropServices.DllImport("user32.dll")] 
    private static extern bool GetWindowInfo(IntPtr hwnd, ref tagWINDOWINFO pwi); 

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
    public struct tagRECT 
    { 
     /// LONG->int 
     public int left; 

     /// LONG->int 
     public int top; 

     /// LONG->int 
     public int right; 

     /// LONG->int 
     public int bottom; 
    } 

    [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
    public struct tagWINDOWINFO 
    { 
     /// DWORD->unsigned int 
     public uint cbSize; 

     /// RECT->tagRECT 
     public tagRECT rcWindow; 

     /// RECT->tagRECT 
     public tagRECT rcClient; 

     /// DWORD->unsigned int 
     public uint dwStyle; 

     /// DWORD->unsigned int 
     public uint dwExStyle; 

     /// DWORD->unsigned int 
     public uint dwWindowStatus; 

     /// UINT->unsigned int 
     public uint cxWindowBorders; 

     /// UINT->unsigned int 
     public uint cyWindowBorders; 

     /// ATOM->WORD->unsigned short 
     public ushort atomWindowType; 

     /// WORD->unsigned short 
     public ushort wCreatorVersion; 
    } 


    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 

    [System.Runtime.InteropServices.DllImport("user32.dll")] 
    private static extern bool UpdateWindow(IntPtr hWnd); 



    private void button2_Click(object sender, EventArgs e) 
    { 

     using (System.Diagnostics.Process browserProc = new System.Diagnostics.Process()) 
     { 
      browserProc.StartInfo.FileName = webBrowser1.Url.ToString(); 
      browserProc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Minimized; 
      int i= browserProc.Id; 
      tagWINDOWINFO info = new tagWINDOWINFO(); 
      info.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(info); 

      browserProc.Start(); 

      GetWindowInfo(browserProc.MainWindowHandle, ref info); 

      browserProc.WaitForInputIdle(); 
      string str = browserProc.MainWindowTitle; 
      MoveWindow(browserProc.MainWindowHandle, 100, 100, 100, 100, true); 
      UpdateWindow(browserProc.MainWindowHandle); 

     } 
    } 

但我得到一個「沒有過程與此對象關聯」。誰能幫忙? 或MB其他想法如何運行新的瀏覽器窗口whith指定的大小和位置?

回答

0

哦,我惡魔解決方案!但是,它起作用(至少在我的系統中)只有蒙山IE ...

我們需要等待,直到Windows與進程關聯一個窗口。像這樣:

while (browserProc.MainWindowHandle == IntPtr.Zero)//пока винда не ассоциировала окно с процессом 
      { 
       Thread.Sleep(50);// ждём 50 мс 
       browserProc.Refresh();// обновялем процесс 
      } 

然後我們可以獲得MainWindowHandle。

相關問題