如何在WPF中保持窗口最后?

时间:2011-02-19 07:03:57

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

我有一个小型.NET程序,可以生成一个全屏窗口。我想把这个窗口保持在最后面的窗口(即其他窗口应该在它上面打开,点击时它不应该出现在前面)。在Windows Presentation Foundation下有没有实用的方法?

1 个答案:

答案 0 :(得分:2)

据我所知,你必须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