代码改变世界

一个拖动无标题栏窗体的方法 修正

2010-02-04 17:29  苏飞  阅读(3674)  评论(5编辑  收藏  举报

下载全部代码:http://www.cckan.net/forum.php?mod=viewthread&tid=68

上次文章地址http://www.cnblogs.com/sufei/archive/2009/05/06/1450683.html

上次写了一个一个拖动无标题栏窗体的方法 发现有一些问题就是每次单击的时候 会向上移动两个像素,现在修改一下                                                          

 

 

代码
 private bool isMouseDown = false;
        
private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置


        
private void Messages_MouseDown(object sender, MouseEventArgs e)
        {
            
try
            {
                
if (e.Button == MouseButtons.Left)
                {
                    isMouseDown 
= true;
                    FormLocation 
= this.Location;
                    mouseOffset 
= Control.MousePosition;
                }

            }
            
catch (Exception)
            {

            }

        }


        
private void Messages_MouseMove(object sender, MouseEventArgs e)
        {
            
try
            {
                
int _x = 0;
                
int _y = 0;
                
if (isMouseDown)
                {
                    Point pt 
= Control.MousePosition;
                    _x 
= mouseOffset.X - pt.X;
                    _y 
= mouseOffset.Y - pt.Y;

                    
this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
                }

            }
            
catch (Exception )
            {

            }

        }

        
private void Messages_MouseUp(object sender, MouseEventArgs e)
        {
            
try
            {
                isMouseDown 
= false;

            }
            
catch (Exception)
            {

            }

        }