2014-09-01 95 views
1

我有一个侧杆的应用程序(对话运形成应用程序),其控制使用华廷自动化相关的IE浏览器。如何将窗口的Z-order设置为刚好在激活的窗口下?

当侧边栏被激活时,我想也带来相关的浏览器前进,但我不想赢得表格应用程序失去焦点

我曾尝试下面的代码的许多设置/变化,但WinForms应用程序失去只要它被激活集中到浏览器,所以可以压在表格上没有任何按键!

private void BrowserControlForm_Activated(object sender, EventArgs e) 
    { 
     if (this.Browser != null) 
     { 
      SetWindowPos(this.Browser.hWnd, -1, this.Width, 0, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - this.Width, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height, 0); 
     } 
    } 

问:什么是将其他窗口刚下(Z-为了明智)当前激活(的WinForms)窗口的正确方法是什么?

该示例代码还会调整浏览器占用屏幕的其余部分,但这与问题无关。更多的现有代码不会帮助解决问题。

更新:

SWP_NOACTIVATE失败带来的浏览器窗口前都:

SetWindowPos(this.CareCheckBrowser.hWnd, -1, this.Width, 0, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - this.Width, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height, 0x0010 /*SWP_NOACTIVATE*/); 

插入后的当前窗口的句柄还是失去焦点:

SetWindowPos(this.Browser.hWnd, (int)this.Handle, this.Width, 0, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - this.Width, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height, 0); 

设置当前窗口到浏览器不工作后,由于焦点已经丢失(因此按钮点击仍然是我) gnored)。焦点/激活笔触刚上来回任何点击例如为:

SetWindowPos(this.Browser.hWnd, 0, this.Width, 0, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - this.Width, System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height, 0x0004); 
SetForegroundWindow(this.Browser.hWnd); 
SetForegroundWindow(this.Handle); 
+0

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showwithoutactivation%28v=vs 0.110%29.aspx – 2014-09-01 15:46:49

+0

@Hans帕桑特:这是一个'Form'方法。我需要提出独立的IE浏览器实例。这可以应用于一个单独的IE实例(我只有一个'hWnd')吗? – 2014-09-01 15:48:42

+0

然后,您需要SetWindowPos()的SWP_NOACTIVATE选项。 – 2014-09-01 15:49:55

回答

1

采取所有打开的窗口句柄和偷看那些没有最小化。遍历列表浏览器的手柄不同的是:

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool IsIconic(IntPtr hWnd); //returns true if window is minimized 

private List<IntPtr> windowsHandles = new List<IntPtr>(); 
//fill list with window handles 

for (i = 0; i < windowsHandles.Count; i++) 
{ 
    if (windowsHandles[i] != browserHandle && windowsHandles[i] != this.Handle && !IsIconic(windowsHandles[i])) 
    { 
     SetWindowPos(windowsHandles[i], HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); 
    } 
} 

瓦尔特

+0

我扩展了这个以排除当前的应用程序hWnd以及浏览器hWnd,并且它可以作为一种享受。 *“不要抬高桥,降低河流”*。做得好。谢谢:) – 2014-09-02 09:05:56

+0

@TrueBlueAussie只是为了好奇。如何使用* GetWindow()或EnumWindows()*获取句柄? – 2014-09-02 11:33:15

+0

我使用'EnumWindows'与'IsWindowVisible()'和'!IsIconic()'来过滤掉打开的应用程序窗口句柄。 – 2014-09-02 11:45:30