0

我已经定制了一个无边框窗口,它是no styleand no borderAllowsTransparency="True"并且在这个窗口的顶部有一个矩形功能,而不是内置的标题栏。

一般情况下,拖动最大化窗口的标题栏即可恢复窗口,但在我自定义的无边框窗口中,我不知道如何实现。谁能帮我?

这是简单的代码:

<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>

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

编辑:

通过拖动原来的Window,我们会发现我们需要拖动一点距离然后改变window.state,那么窗口的位置就会和光标的坐标差不多了。

4

1 回答 1

0

您可以添加 MouseUp 和 MouseMove 处理程序

    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;
        }
    }
于 2013-09-18T02:07:01.647 回答