DragMove()问题,请帮忙! [英] DragMove() problem, help pls!

查看:67
本文介绍了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.

推荐答案

这是我从我发布的示例中删除的一个例子这里 [ ^ ]和在其中 [ ^ ] ...也许它会起作用。



Here's an example I ripped from my examples posted here[^] and here[^]...maybe it will work.

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;
        }
    }
}


除了Mark Salsbery之外,这是一个解决方案。他的代码可以工作,但是当你执行mousedown时控制窗口被拖动时,它的事件不起作用,因为CaptureMouse()函数会阻止它。



例如,我有一个关闭按钮(实际上是一个标签),但它的mousedown事件将无法工作,因为CaptureMouse()函数以某种方式阻止它。



解决方案是在OnMouseMove事件中使用capturemouse函数。因此,如果鼠标在mousedown事件后没有移动,则capturemouse将不会阻止其他控件的事件。



This is a solution in addition to Mark Salsbery's. His code works, but when you have a control on the area that your window is dragged when you perform a mousedown, it's events don't work because the CaptureMouse() function blocks it.

For example, I have a "close" button (which is actually a label), but it's mousedown event won't work because the CaptureMouse() function somehow blocks it.

Solution is to use capturemouse function in the "OnMouseMove" event. So if mouse doesn't move after mousedown event, capturemouse will not block other controls' events.

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();
	}
}


这可能有点旧,但我最近遇到了类似的问题并针对类似问题调整了解决方案使这个工作没有生涩的动作。使用PreviewMouseLeftButtonDown和PreviewMouseMove事件,比较初始点和当前点,并在鼠标实际开始移动时仅调用DragMove。

This might be a little old but I ran into a similar issue recently and adapted a solution on a similar issue to enable this to work without a jerky motion. Using the PreviewMouseLeftButtonDown and PreviewMouseMove events, you compare the initial point and current point and only call DragMove when the mouse actually starts moving.
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()问题,请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆