C# winform程序拖动问题

zy121229899 2010-02-03 01:25:38
form的FormBorderStyle = FormBorderStyle.None,在Form里面有个webBrowser控件,我想实现拖动的效果,最好有代码,谢谢各位大哥了!
...全文
177 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuanhuiqiao 2010-02-03
  • 打赏
  • 举报
回复
DragDrop?
zy121229899 2010-02-03
  • 打赏
  • 举报
回复
感谢7楼的,问题解决了。不过拖动总感觉别扭...
q122281069 2010-02-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/qqq122281069/archive/2010/02/03/5283012.aspx
ck11926375 2010-02-03
  • 打赏
  • 举报
回复
先在页面中用JS给text赋值
再在程序里获取这两个值给form的left, top赋值


<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>



HtmlElement element;
HtmlElement elementX;
HtmlElement elementY;

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
element = doc.GetElementById("btDrag");
elementX = doc.GetElementById("hdragx");
elementY = doc.GetElementById("hdragy");
//绑定html事件
element.AttachEventHandler("onclick", new EventHandler(MoveForm));
}

private void MoveForm(object sender, EventArgs e)
{
//获取html中的值
int x = Convert.ToInt32(elementX.GetAttribute("value"));
int y = Convert.ToInt32(elementY.GetAttribute("value"));
this.Left = this.Left + x;
this.Top = this.Top + y;
}
JavaK 2010-02-03
  • 打赏
  • 举报
回复
那你只能用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>原创地址: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
zy121229899 2010-02-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 javak 的回复:]
例子:
1.创建一个C#工程文件,默认的窗体时Form1 。

2.在View面板上点击Code.

3.将下面的代码粘贴到Form1类中

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

4.在Form1中改写鼠标消息

protected override void WndProc(ref Message m)
{
      switch(m.Msg)
      {
            case WM_NCHITTEST:
                      base.WndProc(ref m);
                      if ((int)m.Result == HTCLIENT)
                            m.Result = (IntPtr)HTCAPTION;
                            return;
                            break;
    }
    base.WndProc(ref m);
}

5.保存并运行工程。

6.试试看,点击窗体的任何地方,是不是都可以拖动窗体啊? MSDN上的
[/Quote]

这种我也知道,问题是我webBrowser是把窗体覆盖了,重写的鼠标消息根本获取不到
zy121229899 2010-02-03
  • 打赏
  • 举报
回复
整个界面就只显示页面 webBrowser把form窗体占满了的
JavaK 2010-02-03
  • 打赏
  • 举报
回复
例子:
1.创建一个C#工程文件,默认的窗体时Form1 。

2.在View面板上点击Code.

3.将下面的代码粘贴到Form1类中

private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;

4.在Form1中改写鼠标消息

protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_NCHITTEST:
base.WndProc(ref m);
if ((int)m.Result == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
return;
break;
}
base.WndProc(ref m);
}

5.保存并运行工程。

6.试试看,点击窗体的任何地方,是不是都可以拖动窗体啊? MSDN上的
cykevin 2010-02-03
  • 打赏
  • 举报
回复
不知道你想拖动谁

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧