随心所欲移动Panel

  C# Winform编程时,有时需要在程序执行时,使窗体中的panel控件可以随意的移动,这时可以采用下面这种方法:

  主要包括以下两步:

      @1:给panel(此处以 RealGLPanel为例说明)添加MouseDown和MouseMove事件处理函数,如此处对应为: RealGLPanel_MouseMove

    和 RealGLPanel_MouseDown

  @2:编写两个函数的实现代码(真的不长,不要怕)

  具体的代码实现如下:

 1         private void RealGLPanel_MouseDown(object sender, MouseEventArgs e)
 2         {
 3             oldRealGLPLPt.X = this.RealGLPanel.Location.X;
 4             oldRealGLPLPt.Y = this.RealGLPanel.Location.Y;
 5 
 6             RealGLPLPt.X = Cursor.Position.X;
 7             RealGLPLPt.Y = Cursor.Position.Y;
 8             this.RealGLPanel.BringToFront();
 9         }
10 
11         private void RealGLPanel_MouseMove(object sender, MouseEventArgs e)
12         {
13             if (e.Button == MouseButtons.Left)
14             {
15                 int tx, ty;                
16                 tx = Cursor.Position.X - RealGLPLPt.X;
17                 ty = Cursor.Position.Y - RealGLPLPt.Y;
18 
19                 if (tx != 0 || ty != 0)
20                 {
21                     int tempx = oldRealGLPLPt.X + tx;
22                     int tempy = oldRealGLPLPt.Y + ty;
23 
24                     //此处可以添加对tempx和tempy的限定的代码
25                     
26                     this.RealGLPanel.Location = new System.Drawing.Point(tempx, tempy);
27                 }
28                 this.RealGLPanel.BringToFront();
29             }
30         }        

 

转载于:https://www.cnblogs.com/lxw0109/p/PANEL.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值