2013-07-09 65 views
0

我正在寫一個應該作爲計劃任務運行的.exe文件,以檢查是否需要在特定顯示器上打開運行.Xbaps的IE窗口。我有一個檢查應該運行哪些網址的代碼,如果它不是我使用此代碼來啓動它,然後將其移動到正確的顯示器:獲取第二個IE窗口在不同的顯示器上打開

Process myProcess = Process.Start("iexplore.exe", "-new -k " + "http://server01:123/software.client.xbap"); 
myProcess.WaitForInputIdle(); 
Thread.Sleep(500); 
MoveWindowToMonitor(myProcess.MainWindowHandle, 1); 

窗口移動代碼:

private static void MoveWindowToMonitor(IntPtr windowHandler, int monitor) 
{ 
    RECT windowRec = new RECT(); 
    GetWindowRect(windowHandler, ref windowRec); 

    int width = windowRec.Right - windowRec.Left; 
    int height = windowRec.Top - windowRec.Bottom; 

    if (width < 0) 
     width = width * -1; 

    if (height < 0) 
     height = height * -1; 


    SetWindowPos(windowHandler, (IntPtr)SpecialWindowHandles.HWND_TOP, Screen.AllScreens[monitor].WorkingArea.Left, 
      Screen.AllScreens[monitor].WorkingArea.Top, width, height, SetWindowPosFlags.SWP_SHOWWINDOW); 

} 

運行這是一個快速測試版本,可以打開第一個IE窗口,啓動Xbap,然後快速將其移至其他監視器。當我第二次運行它時,沒有關閉第一個IE窗口,我總是得到InvalidOperationException

「進程已退出,所以請求的信息不可用。」

我檢查我的任務管理器,因爲這正在發生,我居然得到詳細信息下,我第一次運行任務的任務的每個隨後的執行,有兩種IEXPLORE.EXE項,只有一個額外的IEXPLORER.EXE 。我還爲每個xbap啓動一個PresentationHost.exe。

任何人有任何想法我做錯了或更好的方式來做到這一點? 我的最終目標是能夠做到這一點:顯示器2

  • 啓動IE Kiosk模式與特定URL Y:

    • 啓動IE Kiosk模式監視器1與特定的URL X上
  • +0

    你可以只創建自己的窗口和使用ActiveX在其中主機IE瀏覽器。 –

    +0

    這會有多困難?你有一個簡單的例子嗎? IE仍然能夠以全屏模式運行? – JonD

    +0

    這裏有一些信息:http://msdn.microsoft.com/en-us/library/aa752041(v=vs.85).aspx - 你可以根據需要配置外觀(基本上你得到的是IE窗口的「內部」區域 - 實際顯示網頁的位)。代碼項目有幾個例子。 –

    回答

    0

    啓動IE進程後,它會執行一些有趣的事情,並且您啓動的進程可能偶爾會立即結束,因爲另一個進程會佔用該窗口。

    我會做的是,使用下面的方法是EnumTheWindows將逐步通過每個可見的窗口運行,並尋找Internet Explorer或我的baseURL。然後我將該窗口句柄傳遞給GetURL並獲取IE窗口正在運行的特定URL。這使我可以使用ConfirmProcessIsOnProperMonitor()和MoveWindowToMonitor()來獲取適當監視器上的窗口。

    重要的東西:

    private static bool ConfirmProcessIsOnProperMonitor(IntPtr windowHandler, int monitor) 
    { 
        //make sure you don't go to an incorrect monitor 
        if (monitor >= Screen.AllScreens.Count()) monitor = Screen.AllScreens.Count() - 1; 
    
        RECT windowRec = new RECT(); 
        GetWindowRect(windowHandler, ref windowRec); 
    
        if (windowRec.Left != Screen.AllScreens[monitor].WorkingArea.Left || windowRec.Top != Screen.AllScreens[monitor].WorkingArea.Top) 
         return false; 
        else 
         return true; 
    } 
    
    private static void MoveWindowToMonitor(IntPtr windowHandler, int monitor) 
    { 
        //make sure you don't go to an incorrect monitor 
        if (monitor >= Screen.AllScreens.Count()) monitor = Screen.AllScreens.Count() - 1; 
    
        RECT windowRec = new RECT(); 
        GetWindowRect(windowHandler, ref windowRec); 
    
        int width = windowRec.Right - windowRec.Left; 
        int height = windowRec.Top - windowRec.Bottom; 
    
        if (width < 0) 
         width = width * -1; 
    
        if (height < 0) 
         height = height * -1; 
    
    
        SetWindowPos(windowHandler, (IntPtr)SpecialWindowHandles.HWND_TOP, Screen.AllScreens[monitor].WorkingArea.Left, 
          Screen.AllScreens[monitor].WorkingArea.Top, width, height, SetWindowPosFlags.SWP_SHOWWINDOW); 
    
    } 
    
    protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam) 
    { 
        int size = GetWindowTextLength(hWnd); 
        if (size++ > 0 && IsWindowVisible(hWnd)) 
        { 
         StringBuilder sb = new StringBuilder(size); 
         GetWindowText(hWnd, sb, size); 
         string windowText = sb.ToString(); 
    
         if (windowText.ToLower().Contains(_baseURL) || windowText.ToLower().Contains("internet explorer")) 
         { 
          string url = GetURL(hWnd); 
          _windowhandles.Add(hWnd, url); 
         } 
        } 
        return true; 
    } 
    
    private static string GetURL(IntPtr intPtr) 
    { 
        foreach (InternetExplorer ie in new ShellWindows()) 
        { 
         if (ie.HWND == intPtr.ToInt32()) 
         { 
          string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName); 
          if ((fileNameWithoutExtension != null) && fileNameWithoutExtension.ToLower().Equals("iexplore")) 
          { 
           return ie.LocationURL; 
          } 
          else 
          { 
           return null; 
          } 
         } 
        } 
    
        return null; 
    } 
    

    難讀窗口API代碼:

    [DllImport("user32.dll", SetLastError = true)] 
    private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle); 
    
    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    public static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, StringBuilder msgbody); 
    
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); 
    
    [DllImport("user32.dll", SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect); 
    [StructLayout(LayoutKind.Sequential)] 
    private struct RECT 
    { 
        public int Left; 
        public int Top; 
        public int Right; 
        public int Bottom; 
    } 
    
    protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); 
    
    [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
    protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); 
    [DllImport("user32.dll", CharSet = CharSet.Unicode)] 
    protected static extern int GetWindowTextLength(IntPtr hWnd); 
    [DllImport("user32.dll")] 
    protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); 
    [DllImport("user32.dll")] 
    protected static extern bool IsWindowVisible(IntPtr hWnd); 
    
    相關問題