代码改变世界

拖动无标题栏的窗体,需要处理的三个事件

2010-08-23 11:59  calm_水手  阅读(166)  评论(0编辑  收藏  举报
代码
 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 

 

文章转自:http://home.cnblogs.com/sufei/