2010-03-19 6 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); 

     } 
    } 

그러나 나는 "어떤 프로세스가이 객체에 연결되지 않습니다 얻을 ". 아무도 도와 줄 수 있습니까? 또는 다른 아이디어를 어떻게 새 브라우저 창 whith 지정된 크기와 위치를 실행하려면?

답변

0

아, 해결책을 찾았습니다! 하지만, 적어도 내 whit IE에서 작동합니다. (내 시스템에서는 ...)

Windows가 프로세스와 창을 연결할 때까지 기다려야합니다. 이와 같이 :

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

그리고 MainWindowHandle을 얻을 수 있습니다.

관련 문제