WPF窗口覆盖任务栏

时间:2015-08-02 14:56:53

标签: c# .net wpf visual-studio

我创建了一个自定义WPF窗口,其中WindowStyle = None,AllowTransparency = True,ResizeMode = CanMinimize。

我有两个事件(我创建的用于理解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/ba3ADoL Issue Description Image

同样在恢复窗口时,如果鼠标指针超出窗口范围,则仍不会将焦点放在桌面上。如果稍后手动移动鼠标。专注于桌面。这种行为对我来说似乎很奇怪。任何帮助将不胜感激。

先谢谢。

1 个答案:

答案 0 :(得分:0)

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

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

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

您可以找到教程here

这是一个简短的例子usage