[英]How to SetWindowPos Z-order of a window to just under the activated window?
我有一個側欄應用程序(對話框Win窗體應用程序),該應用程序使用Watin自動化控制關聯的IE瀏覽器。
激活側欄后,我還希望使關聯的瀏覽器前進, 但我不希望Win Forms應用程序失去焦點 。
我已經嘗試了以下代碼的許多設置/變體,但是一旦激活了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);
}
}
問:將另一個窗口放置在當前激活的(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);
由於焦點已經丟失,因此將當前窗口設置為瀏覽器之后的頂部無效(因此仍會忽略按鈕單擊)。 焦點/激活只會在任何點擊上來回滑動,例如:
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);
抓住所有打開的窗戶把手,然后偷看那些沒有最小化的把手。 遍歷列表,但瀏覽器的句柄除外:
[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);
}
}
瓦爾特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.