如何将窗口的WindowPos Z顺序设置为激活窗口的正下方

本文关键字:窗口 设置 正下方 顺序 激活 WindowPos | 更新日期: 2023-09-27 18:20:17

我有一个侧栏应用程序(对话Win表单应用程序),它使用Watin自动化控制关联的IE浏览器。

当侧栏被激活时,我也想让相关的浏览器向前移动,,但我不希望获胜表格应用程序失去焦点

我尝试了以下代码的许多设置/变体,但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);
        }
    }

Q。将另一个窗口放在当前激活的(Winforms)窗口下方(Z向)的正确方式是什么

示例代码还调整了浏览器的大小,占据了屏幕的剩余部分,但这与问题无关更多的现有代码无助于解决问题。

更新:

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);

如何将窗口的WindowPos Z顺序设置为激活窗口的正下方

获取所有打开的窗口句柄并查看那些未最小化的句柄。遍历除浏览器句柄之外的列表:

[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);
    }
}

阀门