个人代码库のC#可移动按钮“相关代码”

Technorati 标签: C#,可移动代码,按钮
        #region 可移动按钮“相关代码”

        Point mouse_offset;
        private void Button_MouseDown(object sender , MouseEventArgs e)
        {
            mouse_offset = e.Location; //将当前鼠标相对于“窗体”左上角的坐标赋值给mouse_offset
        }

        private void Button_MouseMove(object sender , MouseEventArgs e)
        {

            if ( e.Button == MouseButtons.Left )
            {
                //将相对屏幕坐标转换为相对工作区的坐标。
                int left = PointToClient(Control.MousePosition).X - mouse_offset.X;
                int top = PointToClient(Control.MousePosition).Y - mouse_offset.Y;
                
                //左右可能越界
                if(left<0)
                    left=0;
                if ( left > this.Width )
                    left = this.Width - ( (Button)sender ).Width;

                //上下可能越界
                if ( top < 0 )
                    top = 0;
                if ( top > this.Height )
                    top = this.Height - ( (Button)sender ).Height;

                
                ( (Button)sender ).Left = left;
                ( (Button)sender ).Top = top;
            }
        }

        #endregion
作者:Asion Tang
凡是没有注明[转载]的文章,本Blog发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/AsionTang/p/1885705.html