首 页 | 新 闻 | 技术中心 | 第二书店 | 《程序员》 | 《开发高手》 | 社 区 | 黄 页 | 人 才
移 动专 题SUNIBM微 软微 创精 华Donews人 邮
我的技术中心 
我的分类 我的文档
全部文章 发表文章
专栏管理 使用说明



 RSS 订阅 
最新文档列表
Windows/.NET
.NET  (rss)    
Visual C++  (rss)    
Delphi  (rss)    
Visual Basic  (rss)    
ASP  (rss)    
JavaScript  (rss)    
Java/Linux
Java  (rss)    
Perl  (rss)    
综合
其他开发语言  (rss)    
文件格式  (rss)    
企业开发
游戏开发  (rss)    
网站制作技术  (rss)    
数据库
数据库开发  (rss)    
软件工程
其他  (rss)    

积极原创作者 
NinGoo (28)
amh (72)
nizhigang2000 (8)
superyan (27)
iiprogram (83)
hongbo781202 (81)
Kendiv (113)
TechnoFantasy (52)
feifei1018 (19)
coofucoo (108)
CSDN - 文档中心 - .NET 阅读:6295   评论: 0    参与评论
标题   在运行时使用鼠标移动控件和改变控件的大小     选择自 dy_2000_abc 的 Blog
关键字   控件 位置 大小
出处  

在运行时使用鼠标移动控件和改变控件的大小

 

我写了一个类,使用它可以通过鼠标自由移动所有放到窗体上的控件,也可以自由的改变其大小。这个类只可以处理窗体上的控件,如果你有兴趣,可以补充一点代码,使得这个类可以处理更复杂的情形,比如,在Panel控件上的控件。

类代码

public class Resize

       {

              bool IsMoving=false;

              int ctrlLastWidth=0;

              int ctrlLastHeight=0;

              int ctrlWidth;

              int ctrlHeight;

              int ctrlLeft;

              int ctrlTop;

              int cursorL;

              int cursorT;

              int ctrlLastLeft;

              int ctrlLastTop;

              int Htap;

              int Wtap;

              bool ctrlIsResizing=false;

              System.Drawing.Rectangle ctrlRectangle = new System.Drawing.Rectangle();

              private Control ctrl;

              private Form frm;

              public Resize(Control c,Form frm)

              {

                     ctrl=c;

                     this.frm=frm;

                     this.Htap=this.frm.Height-this.frm.ClientRectangle.Height;

                     this.Wtap=this.frm.Width-this.frm.ClientRectangle.Width;

                     ctrl.MouseDown+=new MouseEventHandler(MouseDown);

                     ctrl.MouseMove+=new MouseEventHandler(MouseMove);

                     ctrl.MouseUp+=new MouseEventHandler(MouseUp);

              }

              private void MouseMove(object sender,MouseEventArgs e)

              {

                     if (frm==null)

                            return;

                     if (e.Button==MouseButtons.Left)

                     {

                            if(this.IsMoving)

                            {

                                   if (ctrlLastLeft == 0)

                                          ctrlLastLeft = ctrlLeft;

                                   if (ctrlLastTop==0)

                                          ctrlLastTop = ctrlTop;

                                   int locationX=(Cursor.Position.X-this.cursorL+this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Location.X);

                                   int locationY=(Cursor.Position.Y-this.cursorT+this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Location.Y);

                                   if(locationX<this.frm.DesktopLocation.X+this.Wtap)

                                          locationX=this.frm.DesktopLocation.X+this.Wtap;

                                   if(locationY<this.frm.DesktopLocation.Y+this.Htap)

                                          locationY=this.frm.DesktopLocation.Y+this.Htap;

                                   this.ctrlLeft=locationX;

                                   this.ctrlTop=locationY;

                                   ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLastLeft,this.ctrlLastTop);

                                   ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                                   ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                                   ctrlLastLeft = ctrlLeft;

                                   ctrlLastTop = ctrlTop;

                                   ctrlRectangle.Location = new System.Drawing.Point(ctrlLeft,ctrlTop);

                                   ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                                   ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                                   return;

                            }

                            int sizeageX = (Cursor.Position.X-this.frm.DesktopLocation.X-this.Wtap-this.ctrl.Location.X);

                            int sizeageY = (Cursor.Position.Y-this.frm.DesktopLocation.Y-this.Htap-this.ctrl.Location.Y);

                            if (sizeageX < 2)

                                   sizeageX = 1;

                            if (sizeageY < 2)

                                   sizeageY = 1;

                            ctrlWidth = sizeageX;

                            ctrlHeight = sizeageY;

                            if (ctrlLastWidth == 0)

                                   ctrlLastWidth = ctrlWidth;

                            if (ctrlLastHeight==0)

                                   ctrlLastHeight = ctrlHeight;

                            if (ctrlIsResizing)

                            {

                                   ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.ctrl.Left+this.Wtap,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

                                   ctrlRectangle.Size = new System.Drawing.Size(ctrlLastWidth,ctrlLastHeight);

                            }

                            ctrlIsResizing = true;

                            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                            ctrlLastWidth = ctrlWidth;

                            ctrlLastHeight = ctrlHeight;

                            ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

                            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                     }

              }

              private  void MouseDown(object sender,MouseEventArgs e)

              {

                     if (frm==null)

                            return;

                     if (e.X<this.ctrl.Width-10||e.Y<this.ctrl.Height-10)

                     {

                            this.IsMoving=true;

                            this.ctrlLeft=this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left;

                            this.ctrlTop=this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top;

                            this.cursorL=Cursor.Position.X;

                            this.cursorT=Cursor.Position.Y;

                            this.ctrlWidth=this.ctrl.Width;

                            this.ctrlHeight=this.ctrl.Height;

                     }

                     ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);

                     ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                     ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

              }

              private void MouseUp(object sender,MouseEventArgs e)

              {

                     if (frm==null)

                            return;

                     ctrlIsResizing = false;

                     if (this.IsMoving)

                     {

                            ctrlRectangle.Location = new System.Drawing.Point(this.ctrlLeft,this.ctrlTop);

                            ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                            ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                            this.ctrl.Left=this.ctrlLeft-this.frm.DesktopLocation.X-this.Wtap;

                            this.ctrl.Top=this.ctrlTop-this.frm.DesktopLocation.Y-this.Htap;

                            this.IsMoving=false;

                            this.ctrl.Refresh();

                            return;

                     }

                     ctrlRectangle.Location = new System.Drawing.Point(this.frm.DesktopLocation.X+this.Wtap+this.ctrl.Left,this.frm.DesktopLocation.Y+this.Htap+this.ctrl.Top);

                     ctrlRectangle.Size = new System.Drawing.Size(ctrlWidth,ctrlHeight);

                     ControlPaint.DrawReversibleFrame(ctrlRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Dashed);

                     this.ctrl.Width = ctrlWidth;

                     this.ctrl.Height = ctrlHeight;

                     this.ctrl.Refresh();

              }

       }

 

}

如何使用这个类

假设窗体上有两个控件listBox1toolBar1,你可以在适当的时机(一般在窗体的Load事件中)加入下面的语句就可以了。

Resize r1=new  Resize(this.toolBar1,this);

Resize r4= new Resize(this.listBox1,this);

其他

你也可以添加一些方法,使得在需要的时候可以停止鼠标移动控件的功能,比如:

public void Stop()

{

ctrl.MouseDown- =new MouseEventHandler(MouseDown);

ctrl.MouseMove- =new MouseEventHandler(MouseMove);

ctrl.MouseUp- =new MouseEventHandler(MouseUp);

}


相关文章
对该文的评论