個人代碼庫の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

轉載於:https://www.cnblogs.com/AsionTang/archive/2010/11/20/1885705.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章