1
votes

I'm creating a custom WPF window with, WindowStyle=None, AllowsTransparency=True, and ResizeMode = CanMinimize.

I have two events(That I created to understand events in WPF), PreviewMouseDoubleClick and PreviewMouseMove.

Here's the 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>

Here's the Code Behind:

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

On Dragging this window, it covers the taskbar. I am not sure as to why. Here's the link of the image that describes the behavior I'm talking about: http://imgur.com/ba3ADoL Issue Description Image

Also on restoring the window, if the mouse pointer is out of bounds of the window, the focus is still not given to Desktop. If the mouse is manually moved a bit later. Focus is given to Desktop. This behavior seems strange to me. Any help will be greatly appreciated.

Thanks in Advance.

1

1 Answers

0
votes

I would rather use WindowChrome class in an answer by dss539 at: How to create custom window chrome in wpf?

.NET 4.5 added a new class that greatly simplifies this.

The WindowChrome class enables you to extend Windows Presentation Foundation (WPF) content into the non-client area of a window that is typically reserved for the operating system’s window manager.

You can find a tutorial here.

And here's a short example usage.