让咱们也玩窗体拖动,支持用TextBox,Label,ListBox...

小弟学WinForm编程不久,所以有很多东西不知道,还望大哥大姐们多多指导!

下面的这个窗体拖动类呐,是在http://www.webjx.com/htmldata/2005-02-27/1109468136.html这里学习相关知识后实践的结果,也不知道有哪些缺点,还望看过的朋友指正一下!

程序内有大部分注释了,欢迎大家指正了!

 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>原创地址:http://Yoker.cnblogs.com</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
Tag标签: 拖动,WinForm
posted @ 2008-06-14 16:48 Yoker.Wu 阅读(1626) 评论(5)  编辑 收藏

  回复  引用  查看    
#1楼 2008-06-14 17:37 | Gray Zhang      
我认为做成一个组件比较好,这样就有了可视化编辑的支持,比如用下拉框选需要被拖动的组件,刚刚才为了交作业写了个移动控件的组件...
  回复  引用  查看    
#2楼 2008-06-14 20:50 | 梁逸晨      
我觉得纯粹客户端的的东西,还是不要和服务端挂钩比较好。要不然会给以后的维护带来很大麻烦。
  回复  引用  查看    
#3楼 2008-06-15 10:13 | Henllyee Cui      
@Gray Zhang
楼主这个好像是cs的拖动吧,winform的。
  回复  引用  查看    
#4楼 2008-06-16 03:07 | LixingTie      
其实,不需要这么麻烦,只要调用一下API,windows还是有很多API让我们调用的。

public partial class Form1 : Form
{
[DllImport("user32")]
private static extern bool ReleaseCapture();

[DllImport("user32")]
private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0Xf010;
public const int HTCAPTION = 0x0002;

public Form1()
{
InitializeComponent();
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION,0);
}
}
  回复  引用  查看    
#5楼 [楼主]2008-06-16 09:46 | Yoker.Wu      
@LixingTie
呵呵,每次我看到这样的代码,就觉得很别扭。不喜欢[DllImport("user32")]
这样子的东西。

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-06-14 16:50 编辑过


相关链接: