How can I keep window backmost in WPF?
I have a small .NET program that produces a fullscre开发者_StackOverflowen window. I would like to keep this window the backmost window (i.e. other windows should open on top of it, and it should not come to the front when clicked on). Is there any practical way to do this under Windows Presentation Foundation?
As far as I know, you'll have to P/Invoke to do this right. Call the SetWindowPos
function, specifying the handle to your window and the HWND_BOTTOM
flag.
This will move your window to the bottom of the Z order, and prevent it from obscuring other windows.
Sample code:
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
精彩评论