【NCRE】C# WindowForm实现屏幕最顶端拖动

来源:互联网 发布:syse Linux 进入图形化 编辑:程序博客网 时间:2024/04/29 10:37

   需求:


           窗体置顶,同时显示在任何窗体的最上方,在电脑屏幕上实现左右拖拽。


    代码:

           

<span style="font-size:24px;">        private Point mouseOffset;     //记录鼠标指针的坐标           private bool isMouseDown = false; //记录鼠标按键是否按下        private bool isBoundary = false;    //是否在边界上        private void frmxuanfukuang_MouseDown_1(object sender, MouseEventArgs e)        {            int xOffset;            //int yOffset;            if (e.Button == MouseButtons.Left)            {                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;                //yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height;                mouseOffset = new Point(xOffset, 0);                //this.MouseMove += frmxuanfukuang_MouseMove;                isBoundary = false;                isMouseDown = true;            }        }        private void frmxuanfukuang_MouseMove_1(object sender, MouseEventArgs e)        {            Rectangle ScreenArea = System.Windows.Forms.Screen.GetWorkingArea(this);            int screenWidth = ScreenArea.Width; //屏幕宽度             Point mousePos = Control.MousePosition;            mousePos.Offset(mouseOffset.X, 0);            if (this.Location.X < 0)            {                this.Location = new Point(10, this.Location.Y);                isMouseDown = false;            }            if (this.Location.X + this.Width > screenWidth)            {                this.Location = new Point(screenWidth - 10 - this.Width, this.Location.Y);                isMouseDown = false;            }            if (isMouseDown == true)            {                mousePos.Y = 0;                Location = mousePos;            }        }        private void frmxuanfukuang_MouseUp_1(object sender, MouseEventArgs e)        {            // 修改鼠标状态isMouseDown的值               // 确保只有鼠标左键按下并移动时,才移动窗体               if (e.Button == MouseButtons.Left)            {                //this.MouseMove -= frmxuanfukuang_MouseMove;                isMouseDown = false;            }        }        //设置窗体显示状态         [DllImport("user32.dll")]        private static extern int SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);        </span>

         需要绑定上面的事件。




  

1 0
原创粉丝点击