c#WPF窗体操作类

发布时间 2023-04-17 11:21:41作者: PublicClassNoah
WPF窗体屏幕边缘吸附
  #region 侧吸
        private new bool Hide = false;       //用来表示当前隐藏状态,例如Hide=false就是不在隐藏状态
        string type = "";                   //用来表示窗口隐藏在哪个方向
        private void SliderTimer_Tick(object sender, EventArgs e)   
        {
            //this.TopMost = false;        //窗体不显示在所有软件最前面
            int dif = 20;
            Mouses.POINT mp = new Mouses.POINT();//获得当前鼠标位置 
            Mouses.GetCursorPos(out mp);
            Point pt = new Point(mp.X, mp.Y);
            System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.PrimaryScreen;
            int width = screen.Bounds.Width;            //获取屏幕的宽度     
            int height = screen.Bounds.Height;          //获取屏幕的高度
            if (!this.RestoreBounds.Contains(pt))       //判断鼠标是否在窗体内 
            {
                //如果不在窗体内
                if (Hide == false)
                {
                    if (this.Left < 10)           //窗口左边碰到屏幕最左边
                    {
                        this.Left = -this.Width + 3;    //隐藏窗口,窗口宽度加3取反(为负数)
                        Hide = true;
                        type = "left";                  //窗口隐藏在左边
                    }
                    else if (width - this.Left - this.Width < 10)//窗口右边碰到屏幕最右边
                    {
                        this.Left = width - 3;            //隐藏窗口,窗口宽度加3取反(为负数)
                        Hide = true;
                        type = "right";                 //窗口隐藏在右边
                    }
                    else if (this.Top < 10)      //窗口上边碰到屏幕最上边
                    {
                        this.Top = -this.Height + 3;    //隐藏窗口,窗口高度加3取反(为负数)
                        Hide = true;
                        type = "up";                    //窗口隐藏在上边
                    }
                }
            }
            else
            {    //如果在窗体内
                if (this.Left > dif && type == "left")
                {
                    Hide = false;
                }
                else if (width - this.Left - this.Width > dif && type == "right")
                {
                    Hide = false;
                }
                else if (this.Top > dif && type == "up")
                {
                    Hide = false;
                }
                //如果在窗体内且之前是隐藏状态
                if (Hide == true)
                {
                    this.Topmost = true;        //窗体显示在所有软件最前面
                    if (type == "left")
                    {
                        this.Left = 0;
                        Hide = false;
                    }
                    else if (type == "right")
                    {
                        this.Left = width - this.Width;
                        Hide = false;
                    }
                    else if (type == "up")
                    {
                        this.Top = 0;
                        Hide = false;
                    }
                }
            }
        }
       
        private DispatcherTimer SlideTimer;

        private void EnableSlider()
        {
            this.SlideTimer = new DispatcherTimer();
            this.SlideTimer.Tick += SliderTimer_Tick;
            this.SlideTimer.Interval = TimeSpan.FromMilliseconds(10);
            this.SlideTimer.Start();
            this.SizeChanged += (o, e) =>
            {
                if (this.WindowState == WindowState.Minimized)
                {
                    this.SlideTimer.Stop();
                }
                else if (this.WindowState == WindowState.Normal)
                {
                    this.SlideTimer.Start();
                }
            };
        }
        #endregion
View Code

WPF中窗体移动

   base.OnMouseLeftButtonDown(e);
            //获取鼠标相对标题栏位置
            Point Pos = e.GetPosition(this);
            //如果鼠标位置在标题栏内 允许拖动
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (Pos.X >= 0 && Pos.X < this.ActualWidth && Pos.Y >= 0 && Pos.Y < 200)
                {
                    this.DragMove();
                }
            }
View Code