【问题标题】:In WPF, when drag the customized titlebar in the no-border window, the window will be restored, how to implement it?在WPF中,当在无边框窗口中拖动自定义的标题栏时,窗口会恢复,如何实现呢?
【发布时间】:2013-09-22 14:51:30
【问题描述】:

我已经定制了一个无边框窗口,它是no style 和no border 和AllowsTransparency="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,那么窗口的位置就会和光标的坐标差不多了。

【问题讨论】:

    标签: c# wpf window noborder


    【解决方案1】:

    您可以添加 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;
            }
        }
    

    【讨论】:

    • 请参阅编辑。如果简单地设置_isMouseDown 和WindowState,位置将被恢复,而不是保持光标位置几乎相同。现在,我正在阅读“archive.msdn.microsoft.com/WPFShell”,也许它会给我一些启发。
    猜你喜欢
    • 2018-08-15
    • 2012-04-16
    • 1970-01-01
    • 2014-11-29
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    相关资源
    最近更新 更多