单击任务栏时WPF最小化

本文关键字:最小化 WPF 任务栏 单击 | 更新日期: 2023-09-27 17:58:27

我有一个WPF应用程序,根据利益相关者的要求,它必须具有WindowStyle="None"、ResizeMode="NoResize"和AllowTransparency="True"。我知道,如果不使用Windows chrome,您必须重新实现许多操作系统窗口处理功能。我能够创建一个可工作的自定义最小化按钮,但我无法重新实现Windows在单击屏幕底部的任务栏图标时最小化应用程序的功能。

用户要求应用程序应尽量减少任务栏上的图标点击,并在再次点击时恢复。后者从未停止工作,但我未能实施前者。这是我正在使用的代码:

    public ShellView(ShellViewModel viewModel)
    {
        InitializeComponent();
        // Set the ViewModel as this View's data context.
        this.DataContext = viewModel;
        this.Loaded += new RoutedEventHandler(ShellView_Loaded);
    }
    private void ShellView_Loaded(object sender, RoutedEventArgs e)
    {
        var m_hWnd = new WindowInteropHelper(this).Handle;
        HwndSource.FromHwnd(m_hWnd).AddHook(WindowProc);
    }
    private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        if (msg == NativeMethods.CS_DBLCLKS)
        {
            this.WindowState = WindowState.Minimized;
            // handled = true
        }
        return IntPtr.Zero;
    }
    /// <summary>
    /// http://msdn.microsoft.com/en-us/library/ms646360(v=vs.85).aspx
    /// </summary>
    internal class NativeMethods
    {
        public const int SC_RESTORE = 0xF120;
        public const int SC_MINIMIZE = 0xF020;
        public const int SC_CLOSE = 0xF060;
        public const int WM_SYSCOMMAND = 0x0112;
        public const int WS_SYSMENU = 0x80000;
        public const int WS_MINIMIZEBOX = 0x20000;
        public const int CS_DBLCLKS = 0x8;
        NativeMethods() { }
    }

单击任务栏时WPF最小化

使用ResizeMode="CanMinimize"。这将允许您最小化到任务栏。

我过去曾使用此代码使用WPF的WindowStyle=None 最小化/最大化Windows

private void MinimizeButton_Click(object sender, RoutedEventArgs e)
{
    this.WindowState = WindowState.Minimized;
}
private void MaximizeButton_Click(object sender, RoutedEventArgs e)
{
    AdjustWindowSize();
}
private void AdjustWindowSize()
{
    if (this.WindowState == WindowState.Maximized)
    {
        this.WindowState = WindowState.Normal;
    }
    else
    {
        this.WindowState = WindowState.Maximized;
    }
}
private void FakeTitleBar_MouseDown(object sender, MouseButtonEventArgs e)
{
    if(e.ChangedButton == MouseButton.Left)
    {
        if (e.ClickCount == 2)
        {
            AdjustWindowSize();
        }
        else
        {
            Application.Current.MainWindow.DragMove();
        }
    }
 }

我刚刚意识到,如果ResizeMode=NoResize,如果它等于CanResize,则不禁用操作系统功能,通过任务栏图标单击来最小化。我投票结束这个问题。