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