读书人

C# winform程序拖动有关问题

发布时间: 2012-10-23 12:12:22 作者: rapoo

C# winform程序拖动问题
form的FormBorderStyle = FormBorderStyle.None,在Form里面有个webBrowser控件,我想实现拖动的效果,最好有代码,谢谢各位大哥了!

[解决办法]
那你只能用mousedown, mousemove, mouseup 事件去做
如:

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
[解决办法]
先在页面中用JS给text赋值
再在程序里获取这两个值给form的left, top赋值

HTML code
<script language="javascript" type="text/javascript">    var x0=0,y0=0,x1=0,y1=0;    var moveable=false;    //开始拖动    function startDrag(obj){    if(document.elementFromPoint(event.x,event.y).getAttribute('type')==null)    {        if(event.button==1){            obj.setCapture();            x0 = event.clientX;            y0 = event.clientY;            x1 = parseInt(obj.parentNode.offsetLeft);            y1 = parseInt(obj.parentNode.offsetTop);            moveable = true;        }    }else{        moveable=false;    }    }    //拖动    function Drag(obj){        if(moveable){            obj.parentNode.style.left = x1 + event.clientX - x0;            obj.parentNode.style.top = y1 + event.clientY - y0;        }    }    //停止拖动    function stopDrag(obj){        if(moveable){        obj.releaseCapture();        moveable = false;            document.getElementById("x").value=x1 + event.clientX - x0;            document.getElementById("y").value=y1 + event.clientY - y0;            document.getElementById("btDrag").click();        }    }    </script></head><body  onMouseDown="startDrag(this)" onMouseUp="stopDrag(this)" onMouseMove="Drag(this)" style="border:solid 1px red">    <input type="text" id="y">    <input type="text" id="x">    <input id="btDrag" value="drag" type="button" style="display:block"/>   </body>
[解决办法]
http://blog.csdn.net/qqq122281069/archive/2010/02/03/5283012.aspx

读书人网 >C#

热点推荐