WPF window should not be moved,resized and should only contain minimize,close buttons

4.6k views Asked by At

I'm trying to set up a WPF window.

  • Window should be in maximized state always
  • Window cannot be moved or resized
  • It should contain minimize and close button but not maximize button

I tried the following XAML code

<Window x:Class="BasicImagingStandAlone"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myUserControl="clr-namespace:WpfUserControlLibrary;assembly=WpfUserControlLibrary"
    Title="BasicImagingStandAlone" Icon="desktopicon.png" MinWidth="600" MinHeight="350" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    Height="600" Width="1200" WindowState="Maximized" WindowStyle="None"  ResizeMode="NoResize">
</Window>

The output of the xaml is a window in maximized state which cannot be moved or resized but with no buttons. How can i achieve all the requirments at once?

3

There are 3 answers

0
Nitin On

In order to touch the titlebar of the window you will have to import user32 APIs as described in this answer:

Disable Minimize Button, but Keep Cross and Maximize Buttons - WPF, C#

Also you can set the ResizeMode to CanMinimize. This will disable the maximize button. But in order to hide it you will have to use user32 apis as described above

3
Anuraj On

I couldn't find XAML only way. But this code will help you

To show close and minimize buttons

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ResizeMode="CanMinimize" WindowState="Maximized"
        Title="MainWindow" Height="350" Width="425">
    <Grid>

    </Grid>
</Window>

And if you double click on the window title bar, it will resize to normal, to avoid this use following code.

public MainWindow()
{
    InitializeComponent();
    this.SizeChanged += MainWindow_SizeChanged;
}

private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
    if (WindowState == System.Windows.WindowState.Normal)
    {
        WindowState = System.Windows.WindowState.Maximized;
    }
}

Hope it helps :)

0
Ignatius On

This question is almost 2 years old, but just in case someone has the same requirements.

In WPF you can use the helper class HwndSource to hook into the window procedure which then can be used to process window messages.

So in XAML set WindowState="Maximized", ResizeMode="CanMinimize", and override the SourceInitialized :

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    WindowState="Maximized" ResizeMode="CanMinimize" SourceInitialized="MainWindow_SourceInitialized">

And the code-behind (a modification from an answer in this SO question):

private void MainWindow_SourceInitialized(object sender, EventArgs e)
{
    WindowInteropHelper helper = new WindowInteropHelper(this);
    HwndSource source = HwndSource.FromHwnd(helper.Handle);
    source.AddHook(WndProc);
}

const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int SC_RESTORE = 0xF120;

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    switch (msg)
    {
        case WM_SYSCOMMAND:
            int command = wParam.ToInt32() & 0xfff0;
            if (command == SC_MOVE)
            {
                // prevent user from moving the window
                handled = true;
            }
            else if (command == SC_RESTORE && WindowState == WindowState.Maximized)
            {
                // prevent user from restoring the window while it is maximized
                // (but allow restoring when it is minimized)
                handled = true;
            }
            break;
        default:
            break;
    }
    return IntPtr.Zero;
}

Don't forget to include: using System.Windows.Interop;

This should meet OP's 3 requirements:

  • Window in maximized state
  • Window cannot be moved or resized (including by dragging or double-clicking the title bar)
  • Window's minimize and close buttons are enabled, but restore/maximize button is disabled