WPF中的“覆盖最小化”按钮

时间:2019-03-12 06:49:20

标签: c# wpf override minimize

在WPF中,当用户单击Minimize按钮时,我希望窗口状态仍为正常状态。单击时没有任何反应。但是我不想禁用Minimize按钮,Minimize按钮已启用并且可见,单击时什么都不做。
我该怎么办?

2 个答案:

答案 0 :(得分:2)

您可以在StateChanged事件上实现此目的。 在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">

在代码中:

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

答案 1 :(得分:1)

这是this answer的略微修改形式,由于不必要的调整大小,我将其视为不是重复的形式:

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;
    }
}