1

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?

Luan Pham
  • 187
  • 11
  • why would you want to do that? You asked the question two minutes ago and was marked as dublicate... – Denis Schaf Mar 12 '19 at 06:54
  • maybe this will help ? Analogically with the minimize: https://stackoverflow.com/questions/3001525/how-to-override-default-window-close-operation – Denis Wasilew Mar 12 '19 at 06:58
  • Possible duplicate of [Is it possible to change the event of Minimize button of a WPF window?](https://stackoverflow.com/questions/25132426/is-it-possible-to-change-the-event-of-minimize-button-of-a-wpf-window) – hessam hedieh Mar 12 '19 at 06:59
  • @DenisSchaf I'm finding new way to solve my problem. Previous question is not like this – Luan Pham Mar 12 '19 at 06:59
  • Possible duplicate of [Window StateChanging event in WPF](https://stackoverflow.com/questions/926758/window-statechanging-event-in-wpf) – Rekshino Mar 12 '19 at 07:59

2 Answers2

2

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;
    }
}
Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
1

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;
}
Mathivanan KP
  • 1,979
  • 16
  • 25
  • I've tried this solution but my app is "flicker", I don't know how to explain, looking for another solution. Thanks for your help! – Luan Pham Mar 12 '19 at 07:17