【问题标题】:WPF window covers taskbarWPF 窗口覆盖任务栏
【发布时间】:2015-08-02 14:56:52
【问题描述】:

我正在使用 WindowStyle=None、AllowsTransparency=True 和 ResizeMode = CanMinimize 创建一个自定义 WPF 窗口。

我有两个事件(我创建它是为了理解 WPF 中的事件),PreviewMouseDoubleClick 和 PreviewMouseMove。

这是 XAML:

<Style TargetType="{x:Type local:CustomWindow}">
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="AllowsTransparency" Value="True"/>
    <Setter Property="ResizeMode" Value="CanMinimize"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid Background="{TemplateBinding GridBackground}">
                        <ContentPresenter/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace CustomWindows
{
    public class CustomWindow : Window
    {
        /* Dependency properties */


        public Brush GridBackground
        {
            get { return (Brush)GetValue(GridBackgroundProperty); }
            set { SetValue(GridBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for GridBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty GridBackgroundProperty =
            DependencyProperty.Register("GridBackground", typeof(Brush), typeof(Window), new PropertyMetadata(null));




        /* Constructors */

       static CustomWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow)));
        }   



        /* overriders */

        public override void OnApplyTemplate()
        {
            PreviewMouseDoubleClick += (s, e) => 
            {
                WindowState = (WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
            };

            PreviewMouseMove += (s, e) => 
            {
                if(Mouse.LeftButton == MouseButtonState.Pressed)
                {
                    DragMove();
                }
            };

            base.OnApplyTemplate();
        }
    }
}

拖动此窗口时,它会覆盖任务栏。我不确定为什么。 这是描述我正在谈论的行为的图像链接: http://imgur.com/ba3ADoLIssue Description Image

同样在恢复窗口时,如果鼠标指针超出窗口的范围,焦点仍然没有给桌面。如果稍后手动移动鼠标。重点是桌面。这种行为对我来说似乎很奇怪。任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: c# .net wpf visual-studio


    【解决方案1】:

    我宁愿在 dss539 的回答中使用 WindowChrome 类:How to create custom window chrome in wpf?

    .NET 4.5 添加了一个新类,大大简化了这一点。

    WindowChrome 类使您能够将 Windows Presentation Foundation (WPF) 内容扩展到通常为操作系统的窗口管理器保留的窗口的非客户区。

    你可以找到教程here

    这是一个简短的例子usage

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      • 1970-01-01
      相关资源
      最近更新 更多