如何在桌面空间中移动不规则的wpf窗口? [英] How do I moving an irregularly wpf window in desktop space?

查看:75
本文介绍了如何在桌面空间中移动不规则的wpf窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个带有Background ="{x:Null}",AllowsTransparency ="True"和WindowStyle ="None"属性的WPF窗口,并且其中有一个对象,我想通过光标在桌面空间中用下面的鼠标移动此不规则窗口代码,但不能正常工作,有什么主意吗?

Hi everybody,

I have a WPF window with Background="{x:Null}", AllowsTransparency="True" and WindowStyle="None" properties and there is an object within it, I want moving this irregular window by cursor mouse in desktop space with below code, but not work correctly, is there any idea?

bool inDrag;
Point dragPoint;

private void Body3D_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    dragPoint = e.GetPosition(this);
    inDrag = true;
}

private void Body3D_MouseMove(object sender, 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.Top = pointMoveTo.X;
        this.Left = pointMoveTo.Y;
    }
}

private void Body3D_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    inDrag = false;
}



任何帮助请...

在此先感谢您.



Any help pls...

Thanks in advance.

推荐答案

您不会相信,但这就是您所需要的:-):

You not gonna believe that, but this is all you need :-):

public partial class MyWindow : Window {

    //...

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
        base.OnMouseLeftButtonDown(e);
        this.DragMove(); //begin dragging mode
    } //...

} //class MyWindow



也就是说,它已经为您完成.请参阅 http://msdn.microsoft.com/en-us/library/system .windows.window.dragmove.aspx [ ^ ].

—SA



That is, it''s already done for you. See http://msdn.microsoft.com/en-us/library/system.windows.window.dragmove.aspx[^].

—SA


这篇关于如何在桌面空间中移动不规则的wpf窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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