WPF: Remove the title/control box

12,011

Solution 1

Well, try this

WindowStyle="none"

like this:

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"  WindowStyle="None"
    MinHeight="350" MaxHeight="350" MinWidth="525" MaxWidth="525">
<Grid>

</Grid>
</Window>

Edit:

It looks a little stupid but this way (with Min- and MaxHeight/Width at the same size) you can prevent the window from beeing resized

Solution 2

This is an alternative way of doing it. To remove the Max-/minimize you need to change the ResizeMode like this

<Window x:Class="MyWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="" Height="350" Width="525" ResizeMode="NoResize">
    <Grid>

    </Grid>
</Window>

After that you can remove the Close button by adding this ( read more here )

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

public MainWindow()
{
    SourceInitialized += Window_SourceInitialized;
}

void Window_SourceInitialized(object sender, EventArgs e)
{
    var hwnd = new WindowInteropHelper(this).Handle;
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
Share:
12,011
moevi
Author by

moevi

Updated on June 19, 2022

Comments

  • moevi
    moevi almost 2 years

    I've just switched over from WinForms to wpf and in WinForms removing the whole title box is very simple, just set the title="" and ControlBox=false.

    Now there's many suggestions on how to do this with wpf, all of them using native Win32 calls. Although they do remove the control box, they still leave a thicker border at the top.

    I'm certain it's doable using some kind of native call, but how?

  • Tokk
    Tokk over 13 years
    but us will let you have the title bar, too. WindowsStyle="none" does exatly what is shown in the Picture and is much more simple ;-)
  • Filip Ekberg
    Filip Ekberg over 13 years
    @Tokk, yup saw that. Changed my answer to say it's an alternative way! :)
  • moevi
    moevi over 13 years
    Oh, I feel kinda stupid now. Anyway, I realize now that I have tried that solution before, but the reason it didn't work was because I had also set ResizeMode=NoResize, this completely removes the border. So I guess the real question is: How do I get the WindowStyle=None look and at the same time prevent the window from being resized?