【问题标题】:How can I keep window backmost in WPF?如何在 WPF 中将窗口保持在最后?
【发布时间】:2011-06-30 06:46:34
【问题描述】:

我有一个生成全屏窗口的小型 .NET 程序。我想将此窗口保留在最后面的窗口(即其他窗口应在其顶部打开,并且单击时不应出现在最前面)。在 Windows Presentation Foundation 下是否有任何实用的方法可以做到这一点?

【问题讨论】:

标签: .net wpf vb.net window z-order


【解决方案1】:

据我所知,您必须 P/Invoke 才能正确执行此操作。调用SetWindowPos function,指定窗口句柄和HWND_BOTTOM 标志。

这会将您的窗口移动到 Z 顺序的底部,并防止它遮挡其他窗口。

示例代码:

Private Const SWP_NOSIZE As Integer = &H1
Private Const SWP_NOMOVE As Integer = &H2
Private Const SWP_NOACTIVATE As Integer = &H10

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr,
                                     X As Integer, Y As Integer,
                                     cx As Integer, cy As Integer,
                                     uFlags As Integer) As Boolean
End Function


Public Sub SetAsBottomMost(ByVal wnd As Window)
    ' Get the handle to the specified window
    Dim hWnd As IntPtr = New WindowInteropHelper(wnd).Handle

    ' Set the window position to HWND_BOTTOM
    SetWindowPos(hWnd, New IntPtr(1), 0, 0, 0, 0,
                 SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE)
End Sub

【讨论】:

    猜你喜欢
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2017-09-25
    • 2020-01-23
    相关资源
    最近更新 更多