C# WPF 将 window 移动到别处

C# WPF move window elsewhere

我很乐意移动应用程序。 由于光学原因,我已经移除了框架,现在我无法移动应用程序,就好像我有一个框架一样。 在图片 2 上,您会看到这个框架,一旦进入这个框架并且持有 Maustaste 就可以移动应用程序。 我也想对我的应用程序执行此操作,请参见图 1。 我怎样才能做到这一点? 如果我可以再次移动我的应用程序? 例如,如果我按下白色 canvas 以便移动它。

<Window x:Name="windowsForm" x:Class="Vorschau.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Vorschau"
        mc:Ignorable="d"
        Title="MainWindow" Height="514.583" Width="805.208" FontFamily="Century Gothic" WindowStartupLocation="CenterScreen" BorderThickness="0" ResizeMode="NoResize" WindowStyle="None" Icon="C:\Users\benutzer\Documents\Visual Studio 2015\Projects\Vorschau\Vorschau\img\coordinates.ico">
    <Canvas HorizontalAlignment="Left" Height="60" VerticalAlignment="Top" Width="185" Background="#FFE57E31">
        <Canvas Height="96" Canvas.Top="419" Width="185" Background="#FF2C373F">
            <Label x:Name="lbCopyright" Content="©  Name 2017" Canvas.Left="10" Canvas.Top="61" Width="121" Foreground="#FF1B1D1F"/>
        </Canvas>
        <Canvas Height="359" Canvas.Top="60" Width="185" Background="#FF37424A"/>
        <Canvas Height="60" Canvas.Left="185" Width="610">
            <Label x:Name="lbClose" Content="X" Canvas.Left="578" FontSize="20"/>
            <Label x:Name="lbMinimize" Content="-" Canvas.Left="556" FontSize="22" Canvas.Top="-2"/>
        </Canvas>
        <Canvas Height="455" Canvas.Left="185" Canvas.Top="60" Width="618" Background="#FFD1CFD0"/>
        <Image x:Name="image" Height="38" Canvas.Left="10" Canvas.Top="10" Width="38" Source="C:\Users\benutzer\Documents\Visual Studio 2015\Projects\Vorschau\Vorschau\img\coordinates64.png"/>
        <Label x:Name="lbLogoname" Content="Vorschaukomponente" Canvas.Left="37" Canvas.Top="10" Width="143" FontWeight="Bold" Foreground="White"/>
    </Canvas>
</Window>

添加以下代码使您的 window 可移动。

public partial class MainWindow : Window
{
    public MainWindow ()
    {
        InitializeComponent();
        MouseDown += Window_MouseDown;
    }

    private void Window_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ChangedButton == MouseButton.Left)
            DragMove();
    }
}

代码是从 this answer 复制的,MyWindow 替换为 MainWindow。非常感谢用户 Phaeze。