在练习中,感觉项目中如用上移动控件的方法会更好,于是自己写了一个:只是功能有限,需再完善。
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace scws_Repl
10![]()
{
11public partial class Form1 : Form
12![]()
{
13public Form1()
14![]()
{
15InitializeComponent();
16}
17
18设置成可拖动窗体 与mousemove事件冲突#region 设置成可拖动窗体 与mousemove事件冲突
19
20//private const int WM_NCHITTEST = 0x84;
21//private const int HTCLIENT = 0x1;
22//private const int HTCAPTION = 0x2;
23
24//protected override void WndProc(ref Message m)
25//{
26// switch (m.Msg)
27// {
28// case WM_NCHITTEST:
29// base.WndProc(ref m);
30// if ((int)m.Result == HTCLIENT)
31// m.Result = (IntPtr)HTCAPTION;
32// return;
33// break;
34// }
35// base.WndProc(ref m);
36//}
37#endregion
38
39
40private System.Drawing.Point mousePoint;
41private System.Windows.Forms.Button mybutton = null;
42
43private void button1_Click(object sender, EventArgs e)
44![]()
{
45mybutton = new Button();
46this.Controls.Add(mybutton);
47mybutton.Text = "newbutton";
48mybutton.MouseDown += new MouseEventHandler(mybutton_MouseDown);
49mybutton.MouseMove += new MouseEventHandler(mybutton_MouseMove);
50mybutton.MouseUp += new MouseEventHandler(mybutton_MouseUp);
51}
52![]()
53![]()
54private void Form1_MouseMove(object sender, MouseEventArgs e)
55![]()
{
56this.mousePoint.X = e.X;
57this.mousePoint.Y = e.Y;
58this.linkLabel1.Text = "相对窗口 X:" + mousePoint.X.ToString();
59this.linkLabel2.Text = "相对窗口 Y:" + mousePoint.Y.ToString();
60this.Text = "相对屏幕 X:" + Cursor.Position.X.ToString() + " Y:" + Cursor.Position.Y.ToString();
61}
62
63/**//// <summary>
64/// 记录鼠标位置
65/// </summary>
66private Point mousePos;
67private int diffx, diffy;
68/**//// <summary>
69/// 记录是否可以移动
70/// </summary>
71private bool beginmove = false;
72
73private void mybutton_MouseMove(object sender, MouseEventArgs e)
74![]()
{
75if (beginmove)
76![]()
{
77Button b1 = (Button)sender;
78//
79diffx = mousePos.X - e.X;
80diffy = mousePos.Y - e.Y;
81b1.Location = new Point(b1.Location.X - diffx, b1.Location.Y - diffy);
82![]()
83}
84}
85
86private void mybutton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
87![]()
{
88beginmove = true;//开始移动
89mousePos = new Point(e.X, e.Y);
90}
91
92private void mybutton_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
93![]()
{
94if (e.Button == MouseButtons.Left)
95![]()
{
96beginmove = false;
97Button b1 = (Button)sender;
98diffx = b1.Location.X - mousePos.X + e.X;
99diffy = b1.Location.Y - mousePos.Y + e.Y;
100this.linkLabel3.Text = b1.Text;
101b1.BackColor = Color.Red;
102this.linkLabel4.Text = b1.BackColor.ToString();
103this.linkLabel5.Text = diffx.ToString();
104this.linkLabel6.Text = diffy.ToString();
105}
106}
107}
108}
关于移动和拖动及缩放大小的代码如下:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Text;
7using System.Windows.Forms;
8
9namespace scws_Repl
10![]()
{
11public partial class Form2 : Form
12![]()
{
13public Form2()
14![]()
{
15InitializeComponent();
16}
17
18private enum EnumMousePointPosition
19![]()
{
20MouseSizeNone = 0, //'无
21MouseSizeRight = 1, //'拉伸右边框
22MouseSizeLeft = 2, //'拉伸左边框
23MouseSizeBottom = 3, //'拉伸下边框
24MouseSizeTop = 4, //'拉伸上边框
25MouseSizeTopLeft = 5, //'拉伸左上角
26MouseSizeTopRight = 6, //'拉伸右上角
27MouseSizeBottomLeft = 7, //'拉伸左下角
28MouseSizeBottomRight = 8,//'拉伸右下角
29MouseDrag = 9 // '鼠标拖动
30}
31
32//定义几个变量
33const int Band = 5;
34const int MinWidth = 10;
35const int MinHeight = 10;
36private EnumMousePointPosition m_MousePointPosition;
37private Point p, p1;
38
39//定义自己的MyMouseDown事件
40private void MyMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
41![]()
{
42p.X = e.X;
43p.Y = e.Y; p1.X = e.X;
44p1.Y = e.Y;
45}
46
47//定义自己的MyMouseLeave事件
48private void MyMouseLeave(object sender, System.EventArgs e)
49![]()
{
50m_MousePointPosition = EnumMousePointPosition.MouseSizeNone;
51this.Cursor = Cursors.Arrow;
52}
53
54
55//设计一个函数,确定光标在控件不同位置的样式
56private EnumMousePointPosition MousePointPosition(Size size, System.Windows.Forms.MouseEventArgs e)
57![]()
{
58if ((e.X >= -1 * Band) | (e.X <= size.Width) | (e.Y >= -1 * Band) | (e.Y <= size.Height))
59![]()
{
60if (e.X < Band)
61![]()
{
62if (e.Y < Band)
{ return EnumMousePointPosition.MouseSizeTopLeft; }
63else
64![]()
{
65if (e.Y > -1 * Band + size.Height)
66![]()
{ return EnumMousePointPosition.MouseSizeBottomLeft; }
67else
68![]()
{ return EnumMousePointPosition.MouseSizeLeft; }
69}
70}
71else
72![]()
{
73if (e.X > -1 * Band + size.Width)
74![]()
{
75if (e.Y < Band)
76![]()
{ return EnumMousePointPosition.MouseSizeTopRight; }
77else
78![]()
{
79if (e.Y > -1 * Band + size.Height)
80![]()
{ return EnumMousePointPosition.MouseSizeBottomRight; }
81else
82![]()
{ return EnumMousePointPosition.MouseSizeRight; }
83}
84}
85else
86![]()
{
87if (e.Y < Band)
88![]()
{ return EnumMousePointPosition.MouseSizeTop; }
89else
90![]()
{
91if (e.Y > -1 * Band + size.Height)
92![]()
{ return EnumMousePointPosition.MouseSizeBottom; }
93else
94![]()
{ return EnumMousePointPosition.MouseDrag; }
95}
96}
97}
98}
99else
100![]()
{ return EnumMousePointPosition.MouseSizeNone; }
101}
102
103//定义自己的MyMouseMove事件,在这个事件里,会使用上面设计的函数
104private void MyMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
105![]()
{
106Control lCtrl = (sender as Control);
107
108if (e.Button == MouseButtons.Left)
109![]()
{
110switch (m_MousePointPosition)
111![]()
{
112case EnumMousePointPosition.MouseDrag:
113lCtrl.Left = lCtrl.Left + e.X - p.X;
114lCtrl.Top = lCtrl.Top + e.Y - p.Y;
115break;
116case EnumMousePointPosition.MouseSizeBottom:
117lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
118p1.X = e.X;
119p1.Y = e.Y; //'记录光标拖动的当前点
120break;
121case EnumMousePointPosition.MouseSizeBottomRight:
122lCtrl.Width = lCtrl.Width + e.X - p1.X;
123lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
124p1.X = e.X;
125p1.Y = e.Y; //'记录光标拖动的当前点
126break;
127case EnumMousePointPosition.MouseSizeRight:
128lCtrl.Width = lCtrl.Width + e.X - p1.X;
129// lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
130p1.X = e.X;
131p1.Y = e.Y; //'记录光标拖动的当前点
132break;
133case EnumMousePointPosition.MouseSizeTop:
134lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
135lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
136break;
137case EnumMousePointPosition.MouseSizeLeft:
138lCtrl.Left = lCtrl.Left + e.X - p.X;
139lCtrl.Width = lCtrl.Width - (e.X - p.X);
140break;
141case EnumMousePointPosition.MouseSizeBottomLeft:
142lCtrl.Left = lCtrl.Left + e.X - p.X;
143lCtrl.Width = lCtrl.Width - (e.X - p.X);
144lCtrl.Height = lCtrl.Height + e.Y - p1.Y;
145p1.X = e.X;
146p1.Y = e.Y; //'记录光标拖动的当前点
147break;
148case EnumMousePointPosition.MouseSizeTopRight:
149lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
150lCtrl.Width = lCtrl.Width + (e.X - p1.X);
151lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
152p1.X = e.X;
153p1.Y = e.Y; //'记录光标拖动的当前点
154break;
155case EnumMousePointPosition.MouseSizeTopLeft:
156lCtrl.Left = lCtrl.Left + e.X - p.X;
157lCtrl.Top = lCtrl.Top + (e.Y - p.Y);
158lCtrl.Width = lCtrl.Width - (e.X - p.X);
159lCtrl.Height = lCtrl.Height - (e.Y - p.Y);
160break;
161default:
162break;
163}
164if (lCtrl.Width < MinWidth) lCtrl.Width = MinWidth;
165if (lCtrl.Height < MinHeight) lCtrl.Height = MinHeight;
166
167}
168else
169![]()
{
170m_MousePointPosition = MousePointPosition(lCtrl.Size, e); //'判断光标的位置状态
171switch (m_MousePointPosition) //'改变光标
172![]()
{
173case EnumMousePointPosition.MouseSizeNone:
174this.Cursor = Cursors.Arrow; //'箭头
175break;
176case EnumMousePointPosition.MouseDrag:
177this.Cursor = Cursors.SizeAll; //'四方向
178break;
179case EnumMousePointPosition.MouseSizeBottom:
180this.Cursor = Cursors.SizeNS; //'南北
181break;
182case EnumMousePointPosition.MouseSizeTop:
183this.Cursor = Cursors.SizeNS; //'南北
184break;
185case EnumMousePointPosition.MouseSizeLeft:
186this.Cursor = Cursors.SizeWE; //'东西
187break;
188case EnumMousePointPosition.MouseSizeRight:
189this.Cursor = Cursors.SizeWE; //'东西
190break;
191case EnumMousePointPosition.MouseSizeBottomLeft:
192this.Cursor = Cursors.SizeNESW; //'东北到南西
193break;
194case EnumMousePointPosition.MouseSizeBottomRight:
195this.Cursor = Cursors.SizeNWSE; //'东南到西北
196break;
197case EnumMousePointPosition.MouseSizeTopLeft:
198this.Cursor = Cursors.SizeNWSE; //'东南到西北
199break;
200case EnumMousePointPosition.MouseSizeTopRight:
201this.Cursor = Cursors.SizeNESW; //'东北到南西
202break;
203default:
204break;
205}
206}
207}
208
209
210//制作一个初始化过程,将界面panel1上的所有控件都绑定MyMouseDown、MyMouseLeave、MyMouseMove事件,记得在Form初始化或者Form_Load时先执行它。
211private void initProperty()
212![]()
{
213for (int i = 0; i < this.panel1.Controls.Count; i++)
214![]()
{
215this.panel1.Controls[i].MouseDown += new System.Windows.Forms.MouseEventHandler(MyMouseDown);
216this.panel1.Controls[i].MouseLeave += new System.EventHandler(MyMouseLeave);
217this.panel1.Controls[i].MouseMove += new System.Windows.Forms.MouseEventHandler(MyMouseMove);
218}
219}
220
221private void Form2_Load(object sender, EventArgs e)
222![]()
{
223initProperty();
224}
225}
226}
转载于:https://www.cnblogs.com/winnxm/archive/2008/05/26/1207917.html