我已经自定义了一个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.state,那么窗口的位置将是光标的坐标几乎相同。
答案 0 :(得分:0)
您可以添加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;
}
}