2014-09-01 97 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