Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a WPF window to the back?

Tags:

c#

.net

window

wpf

In my application I have a window I use for plotting debug data. When it loads, I would like to open it "in the background", behind all other windows.

What's the best way to achieve this?

like image 728
Bab Yogoo Avatar asked Jul 25 '09 06:07

Bab Yogoo


People also ask

How do I set the WPF window to the center of the screen?

To open a new window and have it centred on the screen, set the WindowStartupLocation property to CenterScreen. You can do this using the XAML or in code before the window is displayed. Run the program and click the button to see the result.

How do I make a WPF window movable?

Open the Visual Studio. Select the C# language and "WPF" Application. Name the project as "Borderless and draggable window". Click on the "OK" button .

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

Is WPF backend or frontend?

This presentation framework can be used to build visually stunning UI for windows, desktop and web applications. WPF also has flexibility in building apps under service-oriented architecture. WPF uses C# as backend language and XAML as the front-end language.


2 Answers

You can use the following code:

[DllImport("user32.dll")]
static extern bool SetWindowPos(
    IntPtr hWnd,
    IntPtr hWndInsertAfter,
    int X,
    int Y,
    int cx,
    int cy,
    uint uFlags);

const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;

static readonly IntPtr HWND_BOTTOM = new IntPtr(1);

static void SendWpfWindowBack(Window window)
{
    var hWnd = new WindowInteropHelper(window).Handle;
    SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
}

Source: http://www.aeroxp.org/board/lofiversion/index.php?t4983.html

like image 168
huseyint Avatar answered Sep 27 '22 22:09

huseyint


Is there any particular reason you don't want to show the window in minimized state and allow user to show it? If showing window in minimized state solves your problem, use

<Window WindowState="Minimized" (...)>
like image 28
maciejkow Avatar answered Sep 27 '22 22:09

maciejkow