本文介绍了DragMove()问题,请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我的问题是关于WPF中的 DragMove()
方法,当我使用时这个方法在 MouseLeftButtonDown
事件之后,它不起作用其他命令 OnMouseLeftButtonUp
事件。
请告诉我如何在 DragMove
之后显示消息。
Hi,
Ok, my problem is about DragMove()
method in WPF, when I use this method on MouseLeftButtonDown
event, after it don't work other commands OnMouseLeftButtonUp
event.
Please tell me how to show the message after DragMove
.
public partial class myWindow : Window
{
public myWindow()
{
this.InitializeComponent();
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
DragMove();
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
MessageBox.Show("this is a test");
base.OnMouseLeftButtonUp(e);
}
}
另外,我试图模拟 DragMove
,但它无法正常工作!实际上,当我快速移动窗口时,它不会保留光标!
In addition, I tried to simulate the DragMove
, but it doesn't work correctly! actually, when I move the window to around quickly, it don't remain with cursor!
public partial class myWindow : Window
{
public myWindow()
{
this.InitializeComponent();
inDrag = false;
}
bool inDrag;
Point dragPoint;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
dragPoint = e.GetPosition(this);
inDrag = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (inDrag)
{
Point pointMoveTo;
// Find the current mouse position in screen coordinates.
pointMoveTo = this.PointToScreen(e.GetPosition(this));
// Compensate for the position the control was clicked.
pointMoveTo.Offset(-dragPoint.X, -dragPoint.Y);
// Compensate for the non-client region (title bar).
// This code is not necessary if you explicitly hide the title bar
// by setting the form's BorderStyle to None.
//pointMoveTo.Offset(0, -25);
// Move the window.
this.Left = pointMoveTo.X;
this.Top = pointMoveTo.Y;
}
base.OnMouseMove(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
inDrag = false;
MessageBox.Show("this is a test");
base.OnMouseLeftButtonUp(e);
}
}
任何帮助表示赞赏。
提前谢谢。
Any help is appreciated.
Thanks in advance.
推荐答案
public partial class myWindow : Window
{
public myWindow()
{
this.InitializeComponent();
}
bool inDrag = false;
Point anchorPoint;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
anchorPoint = PointToScreen(e.GetPosition(this));
inDrag = true;
CaptureMouse();
e.Handled = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (inDrag)
{
Point currentPoint = PointToScreen(e.GetPosition(this));
this.Left = this.Left + currentPoint.X - anchorPoint.X;
this.Top = this.Top + currentPoint.Y - anchorPoint.Y;
anchorPoint = currentPoint;
}
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
if (inDrag)
{
ReleaseMouseCapture();
inDrag = false;
e.Handled = true;
}
}
}
private bool inDrag = false;
private Point anchorPoint;
private bool iscaptured = false;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
anchorPoint = PointToScreen(e.GetPosition(this));
inDrag = true;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (inDrag) {
if (!iscaptured) {
CaptureMouse();
iscaptured = true;
}
Point currentPoint = PointToScreen(e.GetPosition(this));
this.Left = this.Left + currentPoint.X - anchorPoint.X;
this.Top = this.Top + currentPoint.Y - anchorPoint.Y;
anchorPoint = currentPoint;
}
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
if (inDrag) {
inDrag = false;
iscaptured = false;
ReleaseMouseCapture();
}
}
private Point startPoint;
private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(this);
}
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point newPoint = e.GetPosition(this);
if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(newPoint.X - startPoint.X) > SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(newPoint.Y - startPoint.Y) > SystemParameters.MinimumVerticalDragDistance))
{
this.DragMove();
}
}
这篇关于DragMove()问题,请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!