WPF最小化任务栏单击

时间:2011-06-06 18:29:48

标签: c# wpf winapi minimize

我有一个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() { }
    }

3 个答案:

答案 0 :(得分:14)

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

答案 1 :(得分:3)

我过去曾使用此代码来使用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();
        }
    }
 }

答案 2 :(得分:1)

我刚刚意识到,如果ResizeMode = NoResize比这更发生,如果它等于CanResize而不是禁用操作系统功能以通过任务栏图标点击最小化。我投票决定关闭这个问题。