覆盖WPF中的最小化按钮

问题描述 投票:0回答:2

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

c# wpf override minimize
2个回答
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;
    }
}

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.