简体   繁体   中英

WPF Thumb drag and move with WindowState.Maximized

I have custom window with WindowState=WindowState.Maximized with border and thumb inside in the border, it seems that when the WindowState=WindowState.Maximized I cannot drag and move the custom window to different screen.

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

I've also overridden MouseLeftButtonDown method and using DragMove() inside but without success. I've also tried to subscribe to thumb's MouseLeftButtonDown and write there DragMove() but without success.

By default, maximized windows cannot be moved, thus Left and Top have no effect. One option would be to register to the Thumb.DragStarted event and check if the window is maximized. If yes, you can set WindowState.Normal and successively update the Left and Top properties.

In code, this would look somewhat like this:

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;

}

You can learn more about GetCursorPos here .

However, I would strongly advise you to use the WindowChrome class that comes with .NET 4.5 and that was also suggested by Max in the comments. You just have to use the following code and you have the functionality you're asking for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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