2013-07-09 37 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); 
    
    相关问题