温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

通过控件拖动窗体

发布时间:2020-06-15 18:38:30 来源:网络 阅读:551 作者:小天小闹 栏目:编程语言

 适用于无边框窗体,自定义标题栏

 

之前在网上找的,如http://www.cnblogs.com/yuxuan/archive/2010/09/25/1834346.html这篇博文,开始移动时会瞬移几个像素.....

 

 

 

  1. using System; 
  2.  
  3. using System.Collections.Generic; 
  4.  
  5. using System.Text; 
  6.  
  7. using System.Windows.Forms; 
  8.  
  9. using System.Drawing; 
  10.  
  11.   
  12.  
  13. namespace EW.XIS.Common 
  14.  
  15.  
  16.     /// <summary> 
  17.  
  18.     /// <para>说明:窗体拖动类,通过这个类提供的方法实现窗体上任意控件可辅助拖动窗体</para> 
  19.  
  20.     /// </summary> 
  21.  
  22.     public class DragFormClass 
  23.  
  24.     { 
  25.  
  26.   
  27.  
  28.         private static bool isMouseDown = false
  29.  
  30.         private static Point mouseOffset; 
  31.  
  32.         private static Form _form; 
  33.  
  34.         /// <summary> 
  35.  
  36.         /// 在窗体上增加拖拽事件 
  37.  
  38.         /// </summary> 
  39.  
  40.         /// <param name="control">控件对象</param> 
  41.  
  42.         public static void bindControl(Control control) 
  43.  
  44.         { 
  45.  
  46.             //如果控件为空    
  47.  
  48.             if (control == null
  49.  
  50.             { 
  51.  
  52.                 return
  53.  
  54.             } 
  55.  
  56.             _form = control.FindForm(); 
  57.  
  58.             //增加鼠标拖动窗体移动事件   
  59.  
  60.             control.MouseMove += new MouseEventHandler(control_MouseMove); 
  61.  
  62.             control.MouseDown += new MouseEventHandler(control_MouseDown); 
  63.  
  64.             control.MouseUp += new MouseEventHandler(control_MouseUp); 
  65.  
  66.         } 
  67.  
  68.   
  69.  
  70.         /// <summary> 
  71.  
  72.         /// 鼠标按下之时,保存鼠标相对于窗体的位置 
  73.  
  74.         /// </summary> 
  75.  
  76.         /// <param name="sender"></param> 
  77.  
  78.         /// <param name="e"></param> 
  79.  
  80.         private static void control_MouseDown(object sender, MouseEventArgs e) 
  81.  
  82.         { 
  83.  
  84.             if (Control.MouseButtons == MouseButtons.Left) 
  85.  
  86.             { 
  87.  
  88.   
  89.  
  90.                 Point p=_form.PointToClient(Control.MousePosition); 
  91.  
  92.                 mouseOffset = new Point(-p.X, -p.Y); 
  93.  
  94.                 isMouseDown = true
  95.  
  96.             } 
  97.  
  98.         } 
  99.  
  100.         /// <summary> 
  101.  
  102.         /// 移动鼠标的时候改变窗体位置 
  103.  
  104.         /// </summary> 
  105.  
  106.         /// <param name="sender"></param> 
  107.  
  108.         /// <param name="e"></param> 
  109.  
  110.         private static void control_MouseMove(object sender, MouseEventArgs e) 
  111.  
  112.         { 
  113.  
  114.             if (Control.MouseButtons == MouseButtons.Left) 
  115.  
  116.             { 
  117.  
  118.                 if (isMouseDown) 
  119.  
  120.                 { 
  121.  
  122.                     Point mouse = Control.MousePosition; 
  123.  
  124.                     mouse.Offset(mouseOffset.X, mouseOffset.Y); 
  125.  
  126.                     _form.Location = new Point(mouse.X, mouse.Y); 
  127.  
  128.                      
  129.  
  130.                 } 
  131.  
  132.             } 
  133.  
  134.         } 
  135.  
  136.         /// <summary> 
  137.  
  138.         /// 松开鼠标的时候,重设事件 
  139.  
  140.         /// </summary> 
  141.  
  142.         /// <param name="sender"></param> 
  143.  
  144.         /// <param name="e"></param> 
  145.  
  146.         private static void control_MouseUp(object sender, MouseEventArgs e) 
  147.  
  148.         { 
  149.  
  150.             if (e.Button == MouseButtons.Left) 
  151.  
  152.             { 
  153.  
  154.                 isMouseDown = false
  155.  
  156.             } 
  157.  
  158.         } 
  159.  
  160.   
  161.  
  162.     } 
  163.  
  164.  
  165.   

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI