单击任务栏上的 WPF 最小化

发布于 2024-11-13 22:11:01 字数 1570 浏览 2 评论 0原文

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

I have a WPF application that by stakeholder requirement must have a WindowStyle="None", ResizeMode="NoResize" and AllowTransparency="True". I know that by not using the Windows chrome, you have to re-implement many of the OS window-handling features. I was able to create a working custom minimize button, however I was not able to re-implement the feature where Windows minimize the application when you click on the Taskbar icon at the bottom of your screen.

The user requirement is such that the application should minimize on taskbar icon click and restore on clicking again. The latter has never stopped working but I have not been able to implement the former. Here is the code that I am using:

    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() { }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青芜 2024-11-20 22:11:01

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

Use ResizeMode="CanMinimize". This will allow you to minimize to the taskbar.

痴骨ら 2024-11-20 22:11:01

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

I have used this code in the past to minimize/maximize Windows using WPF's WindowStyle=None

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();
        }
    }
 }
嘴硬脾气大 2024-11-20 22:11:01

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

I just realized that if ResizeMode=NoResize than this happens, if it is equal to CanResize than you do not disable the OS feature to minimize via Taskbar icon click. I'm voting to close this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文