How can I keep window backmost in WPF?

1.5k views Asked by At

I have a small .NET program that produces a fullscreen 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?

1

There are 1 answers

0
Cody Gray - on strike On BEST ANSWER

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