C# 在桌面周围拖动 WPF 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/867140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drag a WPF Form around the desktop
提问by Grant
i am trying to make a c# WPF form where i can drag it around the screen by clicking on it and moving with the mouse. the forms characteristics include being completely transparent and containing only one image. This being said the window style is none and it is not displayed in the taskbar. So essentially all you can see when the app is running is a little image - and ideally i want to be able to drag it around the desktop if i click and hold the left mouse button and move it about.
我正在尝试制作 ac# WPF 表单,我可以通过单击它并用鼠标移动来在屏幕上拖动它。表单的特征包括完全透明且仅包含一张图像。这就是说窗口样式为 none 并且它不显示在任务栏中。所以基本上你在应用程序运行时可以看到的只是一个小图像 - 理想情况下,如果我点击并按住鼠标左键并移动它,我希望能够在桌面上拖动它。
Does anyone know a simple way i can accomplish this or have i overlooked a build in function?
有谁知道一个简单的方法我可以完成这个还是我忽略了一个内置的功能?
Thanks.
谢谢。
采纳答案by Josh
You can use the Window.DragMove method in the mouse down event of the window.
您可以在窗口的鼠标按下事件中使用 Window.DragMove 方法。
回答by EPiddy
Previous answers hit on the answer, but the full example is this:
以前的答案符合答案,但完整的例子是这样的:
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
DragMove();
}
}
回答by HelloSam
If you, like me, want to have a little bit more control about the DoDragMove() - says, to have the window always stay in the border of the current desktop, I have made this.
如果你和我一样,想要对 DoDragMove() 有更多的控制 - 说,让窗口始终停留在当前桌面的边界,我已经做了这个。
Usage:
用法:
public partial class MainWindow : Window
{
private WindowsDragger _dragger;
public MainWindow()
{
InitializeComponent();
_dragger = new WindowsDragger(this);
}
}
Helper Class:
辅助类:
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;
}
}
where WpfScreen is from here: How to get the size of the current screen in WPF?
WpfScreen 来自哪里:How to get the size of current screen in WPF?
回答by Pranavan Maru
on windows load or the grid load event you can use a delegate to trigger the DragMove() function.
在窗口加载或网格加载事件上,您可以使用委托来触发 DragMove() 函数。
`private void Grid_Loaded(object sender, RoutedEventArgs e) {
`私有无效Grid_Loaded(对象发送者,RoutedEventArgs e){
this.MouseDown += delegate{DragMove();};
}`
回答by TheMiddleMan
Here is some simplified code to drag the WPF form around your screen. You might have seen some of this code on different posts, I just modified it to fit the needs of dragging the WPF form.
下面是一些简化的代码,用于在屏幕上拖动 WPF 表单。您可能已经在不同的帖子中看到了其中的一些代码,我只是对其进行了修改以适应拖动 WPF 表单的需要。
Keep in mind we need to grab the form position on the MouseLeftButtonDown, so we can keep the mouse pointer positioned in the same spot on the form as we are dragging it around the screen.
请记住,我们需要在 MouseLeftButtonDown 上抓取表单位置,以便在我们在屏幕上拖动鼠标指针时将鼠标指针定位在表单上的同一位置。
You will also need to add the following reference to get the mouse position relative to the screen: System.Windows.Forms
您还需要添加以下引用以获取相对于屏幕的鼠标位置:System.Windows.Forms
Properties Needed:
所需属性:
private bool _IsDragInProgress { get; set; }
private System.Windows.Point _FormMousePosition {get;set;}
Code:
代码:
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);
}