WPF无边框拖动、全屏、缩放

先看效果

  1. 无边框
    设置WindowStyle=“None”,窗口无关闭及缩放按钮,但还有黑边;设置AllowsTransparency=“True”,黑边没有了。
  2. 全屏,遮住任务栏
			 
            this.WindowState = System.Windows.WindowState.Normal;
            this.WindowStyle = System.Windows.WindowStyle.None;
            this.ResizeMode = System.Windows.ResizeMode.NoResize;
            //this.Topmost = true;
            
			//设置全屏
            this.Left = 0.0;
            this.Top = 0.0;
            this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
  1. 拖动
//xaml
MouseMove="Move_MouseMove"

//cs
		private void Move_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }
  1. 另一种拖动方式

不能拖动太快

        System.Drawing.Point MouseCursor_Point;
        System.Drawing.Point MouseCursor_Point_Aux;
        double mTop = 0;
        double mLeft = 0;
        private void Button_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed)
            {
                if (rightMouseDown)
                {
                    MouseCursor_Point_Aux = System.Windows.Forms.Cursor.Position;
                    this.Left = this.mLeft + MouseCursor_Point_Aux.X - MouseCursor_Point.X;
                    this.Top = this.mLeft + MouseCursor_Point_Aux.Y - MouseCursor_Point.Y;
                    Console.WriteLine($"({this.Left},{this.Top})");
                }
                else
                {
                    rightMouseDown = true;
                    mTop = this.Top;
                    mLeft = this.Left;
                    MouseCursor_Point = System.Windows.Forms.Cursor.Position;
                }
            }
            else
            {
                rightMouseDown = false;
            }
        }

或者,这种需要慢点拖,否则会有问题

        System.Drawing.Point MouseCursor_Point;
        System.Drawing.Point MouseCursor_Point_Aux;
        
        private void Button_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed)
            {
                MouseCursor_Point_Aux = System.Windows.Forms.Cursor.Position;
                this.Left += MouseCursor_Point_Aux.X - MouseCursor_Point.X;
                this.Top += MouseCursor_Point_Aux.Y - MouseCursor_Point.Y;
            }
            MouseCursor_Point = System.Windows.Forms.Cursor.Position;
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值