Tuesday, August 11, 2015

WPF: How to remove maximize and minimize buttons from a window

1. No need to keep the window resizable (dialog box):

<Window x:Class="Vurdalakov.AboutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="About" Height="200" Width="300"
        ResizeMode="NoResize">

2. If you want to keep the window resizable:

namespace Vurdalakov
{
    using System;
    using System.Runtime.InteropServices;
    using System.Windows;
    using System.Windows.Interop;

    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();

            this.SourceInitialized += (s, e) =>
            {
                var handle = new WindowInteropHelper(s as Window).Handle;
                var style = GetWindowLong(handle, GWL_STYLE);
                SetWindowLong(handle, GWL_STYLE, (int)(style & ~(WS_MAXIMIZEBOX | WS_MINIMIZEBOX)));
            };
        }

        private const Int32 GWL_STYLE = -16;
        private const Int32 WS_MAXIMIZEBOX = 0x10000;
        private const Int32 WS_MINIMIZEBOX = 0x20000;

        [DllImport("user32.dll")]
        private static extern Int32 GetWindowLong(IntPtr hWnd, Int32 nIndex);

        [DllImport("user32.dll")]
        private static extern Int32 SetWindowLong(IntPtr hWnd, Int32 nIndex, Int32 dwNewLong);
    }
}

No comments:

Post a Comment