我有一个带有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(),但没有成功。我还试着订阅拇指的MouseLeftButtonDown,并在那里写DragMove(),但没有成功。
发布于 2015-05-20 15:28:22
默认情况下,无法移动最大化的窗口,因此Left和Top没有任何影响。一个选项是注册到Thumb.DragStarted事件并检查窗口是否最大化。如果是,可以设置WindowState.Normal并连续更新Left和Top属性。
在代码中,这看起来有点像这样:
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 这里的知识。
然而,强烈建议您使用 WindowChrome 类,这是.NET 4.5附带的,也是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>https://stackoverflow.com/questions/30348249
复制相似问题