程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現窗體和任意控件的拖動

C#實現窗體和任意控件的拖動

編輯:關於C#

程序內有大部分注釋了,歡迎大家指正了!

Code

[copy to clipboard]

CODE:

1using System;
2using System.Drawing;
3using System.Windows.Forms;
4
5namespace Yoker.FormUtils
6{
7  /**//// <summary>
8  /// <para>說明:窗體拖動類,通過這個類提供的方法實現窗體上任意控件可輔助拖動窗體</para>
9  /// <para>作者:Yoker.Wu</para>
10  /// <para>原創地址:[url]http://Yoker.cnblogs.com[/url]</para>
11  /// </summary>
12  public class dragFormClass
13  {
14    private static bool isMouseDown = false;
15    private static Point mouseOffset;
16    private static Form _form;
17    public dragFormClass() { }
18
19    /**//// <summary>
20    /// 在窗體上增加拖拽事件
21    /// </summary>
22    /// <param name="control">控件對象</param>
23    public static void bindControl(Control control)
24    {
25      //如果控件為空 
26      if (control == null)
27      {
28        return;
29      }
30      _form = control.FindForm();
31      //增加鼠標拖動窗體移動事件
32      control.MouseMove += new MouseEventHandler(control_MouseMove);
33      control.MouseDown += new MouseEventHandler(control_MouseDown);
34      control.MouseUp += new MouseEventHandler(control_MouseUp);
35    }
36    /**//// <summary>
37    /// 鼠標按下之時,保存鼠標相對於窗體的位置
38    /// </summary>
39    /// <param name="sender"></param>
40    /// <param name="e"></param>
41    private static void control_MouseDown(object sender, MouseEventArgs e)
42    {
43      if (e.Button == MouseButtons.Left)
44      {
45        Control control = sender as Control;
46        int offsetX = - e.X;
47        int offsetY = - e.Y;
48        //判斷是窗體還是控件,從而改進鼠標相對於窗體的位置
49        if (!(control is System.Windows.Forms.Form))
50        {
51          offsetX = offsetX - control.Left;
52          offsetY = offsetY - control.Top;
53        }
54        //判斷窗體有沒有標題欄,從而改進鼠標相對於窗體的位置
55        if (_form.FormBorderStyle != FormBorderStyle.None)
56        {
57          offsetX = offsetX - SystemInformation.FrameBorderSize.Width;
58          offsetY = offsetY - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight;
59        }
60        mouseOffset = new Point(offsetX, offsetY);
61        isMouseDown = true;
62      }
63    }
64    /**//// <summary>
65    /// 移動鼠標的時候改變窗體位置
66    /// </summary>
67    /// <param name="sender"></param>
68    /// <param name="e"></param>
69    private static void control_MouseMove(object sender, MouseEventArgs e)
70    {
71      if (isMouseDown)
72      {
73        Point mouse = Control.MousePosition;
74        mouse.Offset(mouseOffset.X, mouseOffset.Y);
75        _form.Location = mouse;
76      }
77    }
78    /**//// <summary>
79    /// 松開鼠標的時候,重設事件
80    /// </summary>
81    /// <param name="sender"></param>
82    /// <param name="e"></param>
83    private static void control_MouseUp(object sender, MouseEventArgs e)
84    {
85      if (e.Button == MouseButtons.Left)
86      {
87        isMouseDown = false;
88      }
89    }
90  }
91}
92

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved