拖动无标题栏的窗体,需要处理的三个事件
生活随笔
收集整理的這篇文章主要介紹了
拖动无标题栏的窗体,需要处理的三个事件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼 ?1?using?System;
?2?using?System.Collections.Generic;
?3?using?System.ComponentModel;
?4?using?System.Data;
?5?using?System.Drawing;
?6?using?System.Linq;
?7?using?System.Text;
?8?using?System.Windows.Forms;
?9?
10?namespace?Invoicing_Tube
11?{
12?????public?partial?class?login?:?Form
13?????{
14?????????public?login()
15?????????{
16?????????????InitializeComponent();
17?????????}
18?????????//定義一下坐標
19?????????private?Point?mouseOffset;
20?????????//判斷一下是否按下了左鍵默認的為False
21?????????private?bool?isMouseDown?=?false;
22?????????private?void?login_MouseDown(object?sender,?MouseEventArgs?e)
23?????????{
24?????????????try
25?????????????{
26?????????????????//X軸
27?????????????????int?xOffset;
28?????????????????//Y軸
29?????????????????int?yOffset;
30?????????????????//當按下左鍵時
31?????????????????if?(e.Button?==?MouseButtons.Left)
32?????????????????{
33??????????????????????//X軸為當前的位置加上窗體的長度
34?????????????????????xOffset?=?-e.X?-?SystemInformation.FrameBorderSize.Width;
35?????????????????????//Y軸為當前的位置加上窗體的高度
36?????????????????????yOffset?=?-e.Y?-?SystemInformation.CaptionHeight?-
37?????????????????????????SystemInformation.FrameBorderSize.Height;
38?????????????????????//綜合一下坐標
39?????????????????????mouseOffset?=?new?Point(xOffset,?yOffset);
40?????????????????????//表示按下了左鍵
41?????????????????????isMouseDown?=?true;
42?????????????????}
43?????????????}
44?????????????catch?(Exception?ee)
45?????????????{
46?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
47?????????????}
48?????????}
49?
50?????????private?void?login_MouseMove(object?sender,?MouseEventArgs?e)
51?????????{
52?????????????try
53?????????????{
54?????????????????//如果按下了左鍵
55?????????????????if?(isMouseDown)
56?????????????????{
57?????????????????????//前當的坐標==鼠標的位置
58?????????????????????Point?mousePos?=?Control.MousePosition;
59?????????????????????mousePos.Offset(mouseOffset.X,?mouseOffset.Y);
60?????????????????????Location?=?mousePos;
61?????????????????}
62?????????????}
63?????????????catch?(Exception?ee)
64?????????????{
65?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
66?????????????}
67?????????}
68?
69?????????private?void?login_MouseUp(object?sender,?MouseEventArgs?e)
70?????????{
71?????????????try
72?????????????{
73?????????????????if?(e.Button?==?MouseButtons.Left)
74?????????????????{
75?????????????????????isMouseDown?=?false;
76?????????????????}
77?????????????}
78?????????????catch?(Exception?ee)
79?????????????{
80?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
81?????????????}
82?????????}
83?????}
84?}
85?
86?
?2?using?System.Collections.Generic;
?3?using?System.ComponentModel;
?4?using?System.Data;
?5?using?System.Drawing;
?6?using?System.Linq;
?7?using?System.Text;
?8?using?System.Windows.Forms;
?9?
10?namespace?Invoicing_Tube
11?{
12?????public?partial?class?login?:?Form
13?????{
14?????????public?login()
15?????????{
16?????????????InitializeComponent();
17?????????}
18?????????//定義一下坐標
19?????????private?Point?mouseOffset;
20?????????//判斷一下是否按下了左鍵默認的為False
21?????????private?bool?isMouseDown?=?false;
22?????????private?void?login_MouseDown(object?sender,?MouseEventArgs?e)
23?????????{
24?????????????try
25?????????????{
26?????????????????//X軸
27?????????????????int?xOffset;
28?????????????????//Y軸
29?????????????????int?yOffset;
30?????????????????//當按下左鍵時
31?????????????????if?(e.Button?==?MouseButtons.Left)
32?????????????????{
33??????????????????????//X軸為當前的位置加上窗體的長度
34?????????????????????xOffset?=?-e.X?-?SystemInformation.FrameBorderSize.Width;
35?????????????????????//Y軸為當前的位置加上窗體的高度
36?????????????????????yOffset?=?-e.Y?-?SystemInformation.CaptionHeight?-
37?????????????????????????SystemInformation.FrameBorderSize.Height;
38?????????????????????//綜合一下坐標
39?????????????????????mouseOffset?=?new?Point(xOffset,?yOffset);
40?????????????????????//表示按下了左鍵
41?????????????????????isMouseDown?=?true;
42?????????????????}
43?????????????}
44?????????????catch?(Exception?ee)
45?????????????{
46?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
47?????????????}
48?????????}
49?
50?????????private?void?login_MouseMove(object?sender,?MouseEventArgs?e)
51?????????{
52?????????????try
53?????????????{
54?????????????????//如果按下了左鍵
55?????????????????if?(isMouseDown)
56?????????????????{
57?????????????????????//前當的坐標==鼠標的位置
58?????????????????????Point?mousePos?=?Control.MousePosition;
59?????????????????????mousePos.Offset(mouseOffset.X,?mouseOffset.Y);
60?????????????????????Location?=?mousePos;
61?????????????????}
62?????????????}
63?????????????catch?(Exception?ee)
64?????????????{
65?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
66?????????????}
67?????????}
68?
69?????????private?void?login_MouseUp(object?sender,?MouseEventArgs?e)
70?????????{
71?????????????try
72?????????????{
73?????????????????if?(e.Button?==?MouseButtons.Left)
74?????????????????{
75?????????????????????isMouseDown?=?false;
76?????????????????}
77?????????????}
78?????????????catch?(Exception?ee)
79?????????????{
80?????????????????MessageBox.Show(ee.Message.ToString(),?"提示信息",?MessageBoxButtons.OK,?MessageBoxIcon.Warning);
81?????????????}
82?????????}
83?????}
84?}
85?
86?
?
文章轉自:http://home.cnblogs.com/sufei/轉載于:https://www.cnblogs.com/angleSJW/archive/2010/08/23/1806315.html
總結
以上是生活随笔為你收集整理的拖动无标题栏的窗体,需要处理的三个事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript 的观察者模式的原理
- 下一篇: Lift当中的AnyVarTrati源码