In WPF, when drag the customized titlebar in the no-border window, the window will be restored, how to implement it?

StackOverflow https://stackoverflow.com/questions/18862173

  •  29-06-2022
  •  | 
  •  

I have customized a no-border window which is no style and no border and AllowsTransparency="True", and in the top of this window there're a rectangle functionally instead of the build-in title bar.

Generally, drag the title bar of the maximized window then the window will be restored, but in my customized no-border window, I no idea to implement it. Who can help me?

This is simple code:

<Window x:Class="WpfApplication24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        WindowStyle="None"
        AllowsTransparency="True">
    <DockPanel LastChildFill="False">
        <Rectangle DockPanel.Dock="Top" x:Name="DragRectangle" Height="30" Fill="#FF123456" MouseLeftButtonDown="DragRectangle_MouseLeftButtonDown"/>
        <Button x:Name="ToMaxButton" Content="ToMaxButton" Click="ToMaxButton_Click" Height="30" Margin="5"/>
        <Button x:Name="ToNormalButton" Content="ToNormalButton" Click="ToNormalButton_Click" Height="30" Margin="5"/>
        <Button x:Name="CloseButton" Content="CloseButton" Click="CloseButton_Click" Height="30" Margin="5"/>
    </DockPanel>
</Window>

and

namespace WpfApplication24
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void DragRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void ToMaxButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Maximized;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void ToNormalButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Normal;
        }
    }
}

Edited:

Through by draging the original Window, we will find that we need drag a little distance then changed the window.state, then the position of the window will be that the coordinate of the cursor is nearly same.

有帮助吗?

解决方案

You could add a MouseUp and MouseMove handler

    private bool _isMouseDown = false;

    private void DragRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        _isMouseDown = true;
        this.DragMove();
    }

    private void DragRectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        _isMouseDown = false;
    }

    private void DragRectangle_MouseMove(object sender, MouseEventArgs e)
    {
        // if we are dragging and Maximized, retore window
        if (_isMouseDown && this.WindowState == System.Windows.WindowState.Maximized)
        {
            _isMouseDown = false;
            this.WindowState = System.Windows.WindowState.Normal;
        }
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top