简体   繁体   中英

Override minimize button in WPF

In WPF, when user click on Minimize button, I want window state still normal state. Nothing happened when click it. But I don't want to disable Minimize button, Minimize button is enable and visible, just do nothing when click.
How can I do it?

You can achieve this on StateChanged event. In XAML:

<Window x:Class="WpfApp.MainWindow"
    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"
    StateChanged="Window_StateChanged">

In Code:

private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.WindowState == WindowState.Minimized)
        this.WindowState = WindowState.Normal;
}

This is a slightly modified form of this answer , which I'll treat as not being a duplicate due to the unneccessary resize:

using System.Windows.Interop;

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

        this.SourceInitialized += new EventHandler(OnSourceInitialized);
    }

    private void OnSourceInitialized(object sender, EventArgs e)
    {
        HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
        source.AddHook(new HwndSourceHook(HandleMessages));
    }

    private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        // 0x0112 == WM_SYSCOMMAND, 'Window' command message.
        // 0xF020 == SC_MINIMIZE, command to minimize the window.
        if (msg == 0x0112 && ((int)wParam & 0xFFF0) == 0xF020)
        {
            // Cancel the minimize.
            handled = true;
        }

        return IntPtr.Zero;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM