我正在尝试制作 ac# WPF 表单,我可以通过单击它并用鼠标移动来在屏幕上拖动它。表格的特点包括完全透明并且只包含一个图像。这就是说窗口样式是none,它不会显示在任务栏中。所以本质上,当应用程序运行时你能看到的只是一个小图像——理想情况下,如果我单击并按住鼠标左键并移动它,我希望能够在桌面上拖动它。
有谁知道我可以做到这一点的简单方法,还是我忽略了内置功能?
谢谢。
以前的答案命中了答案,但完整的例子是这样的:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
您可以在窗口的鼠标按下事件中使用 Window.DragMove 方法。
下面是一些简化的代码,用于在屏幕上拖动 WPF 表单。您可能已经在不同的帖子中看到了其中的一些代码,我只是对其进行了修改以适应拖动 WPF 表单的需要。
请记住,我们需要在 MouseLeftButtonDown 上获取表单位置,这样当我们在屏幕上拖动鼠标指针时,我们可以将鼠标指针定位在表单上的同一位置。
您还需要添加以下引用以获取鼠标相对于屏幕的位置:System.Windows.Forms
所需属性:
private bool _IsDragInProgress { get; set; }
private System.Windows.Point _FormMousePosition {get;set;}
代码:
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this._IsDragInProgress = true;
this.CaptureMouse();
this._FormMousePosition = e.GetPosition((UIElement)this);
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (!this._IsDragInProgress)
return;
System.Drawing.Point screenPos = (System.Drawing.Point)System.Windows.Forms.Cursor.Position;
double top = (double)screenPos.Y - (double)this._FormMousePosition.Y;
double left = (double)screenPos.X - (double)this._FormMousePosition.X;
this.SetValue(MainWindow.TopProperty, top);
this.SetValue(MainWindow.LeftProperty, left);
base.OnMouseMove(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
this._IsDragInProgress = false;
this.ReleaseMouseCapture();
base.OnMouseLeftButtonUp(e);
}
如果你和我一样,想要对 DoDragMove() 有更多的控制 - 说,让窗口始终保持在当前桌面的边界,我已经做到了。
用法:
public partial class MainWindow : Window
{
private WindowsDragger _dragger;
public MainWindow()
{
InitializeComponent();
_dragger = new WindowsDragger(this);
}
}
助手类:
class WindowsDragger
{
private readonly Window _window;
private Point _dragDelta;
public WindowsDragger(Window window)
{
_window = window;
_window.MouseLeftButtonDown += MouseLeftButtonDown;
_window.MouseLeftButtonUp += MouseLeftButtonUp;
_window.MouseMove += MouseMove;
}
void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_dragDelta = e.GetPosition(_window);
Mouse.Capture(_window);
}
void MouseMove(object sender, MouseEventArgs e)
{
if (Equals(_window, Mouse.Captured))
{
var pos = _window.PointToScreen(e.GetPosition(_window));
var verifiedPos = CoerceWindowBound(pos - _dragDelta);
_window.Left = verifiedPos.X;
_window.Top = verifiedPos.Y;
}
}
void MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (Equals(_window, Mouse.Captured))
Mouse.Capture(null);
}
private Vector CoerceWindowBound(Vector newPoint)
{
// Snap to the current desktop border
var screen = WpfScreen.GetScreenFrom(_window);
var wa = screen.WorkingArea;
if (newPoint.X < wa.Top) newPoint.X = wa.Top;
if (newPoint.Y < wa.Left) newPoint.Y = wa.Left;
if (_window.Width + newPoint.X > wa.Right) newPoint.X = wa.Right - _window.Width;
if (_window.Height + newPoint.Y > wa.Bottom) newPoint.Y = wa.Bottom - _window.Height;
return newPoint;
}
}
WpfScreen 来自这里:如何在 WPF 中获取当前屏幕的大小?
在窗口加载或网格加载事件上,您可以使用委托来触发 DragMove() 函数。
`private void Grid_Loaded(object sender, RoutedEventArgs e) {
this.MouseDown += delegate{DragMove();};
}`