我有一个生成全屏窗口的小型 .NET 程序。我想将此窗口保留在最后面的窗口(即其他窗口应在其顶部打开,并且在单击时不应出现在最前面)。在 Windows Presentation Foundation 下有什么实用的方法可以做到这一点吗?
最佳答案
据我所知,您必须 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
关于.net - 如何在 WPF 中保持窗口靠后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5049409/