繁体   English   中英

WPF Thumb拖动并使用WindowState.Maximized

[英]WPF Thumb drag and move with WindowState.Maximized

我有一个带有WindowState=WindowState.Maximized自定义窗口,边框内有边框和拇指,看来当WindowState=WindowState.Maximized我无法将自定义窗口拖动并移动到其他屏幕。

Xaml:

    <Window x:Class="WpfApplication3.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">
        <Border Name="headerBorder" 
            Width="Auto" 
            Height="50" 
            VerticalAlignment="Top"
            CornerRadius="5,5,0,0" 
            DockPanel.Dock="Top" 
            Background="Red" 
            BorderThickness="1,1,1,1"
            BorderBrush="Yellow">
            <Grid x:Name="PART_Title">
                <Thumb x:Name="headerThumb" 
                    Opacity="0" 
                    Background="{x:Null}" 
                    Foreground="{x:Null}" 
                    DragDelta="headerThumb_DragDelta"/>
            </Grid>
        </Border>
    </Window>

C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        WindowState = System.Windows.WindowState.Maximized;
    }

    private void headerThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
    {
        Left = Left + e.HorizontalChange;
        Top = Top + e.VerticalChange;
    }
}

我也重写了MouseLeftButtonDown方法,并在内部使用DragMove() ,但没有成功。 我也尝试订阅thumb的MouseLeftButtonDown并在其中编写DragMove()但没有成功。

默认情况下,最大化的窗口无法移动,因此“ LeftTop上”无效。 一种选择是注册到Thumb.DragStarted事件,并检查窗口是否最大化。 如果是,则可以设置WindowState.Normal并依次更新LeftTop属性。

在代码中,这看起来像这样:

private void Thumb_OnDragStarted(object sender, DragStartedEventArgs e)
{
    // If the window is not maximized, do nothing
    if (WindowState != WindowState.Maximized)
        return;

    // Set window state to normal
    WindowState = WindowState.Normal;

    // Here you have to determine the initial Left and Top values
    // for the window that has WindowState normal
    // I would use something like the native 'GetCursorPos' (in user32.dll)
    // function to get the absolute mouse point on all screens 
    var point = new Win32Point();
    GetCursorPos(ref point);
    Left = point - certainXValue;
    Top = point - certainYValue;

}

您可以在此处了解有关GetCursorPos更多信息。

但是,我强烈建议您使用.NET 4.5附带的WindowChrome类,Max在注释中也建议使用该类。 您只需要使用以下代码,即可获得所需的功能:

<Window x:Class="ThumbMaximizedWindow.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"
        WindowState="Maximized">
    <WindowChrome.WindowChrome>
        <WindowChrome />
    </WindowChrome.WindowChrome>

</Window>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM