自定义窗体标题栏

在没有标题栏的form里,用panel做透明标题栏,实现和form标题栏一样的可移动窗体的效果。

1.新建form窗体

2.蒋一个panel控件放到窗体最上端,将panel控件的BackColor属性设置为Transparent透明。

3.加如以下代码

1 private Point mouseOffset; //记录鼠标指针的坐标
2 privatebool isMouseDown =false; //记录鼠标按键是否按下
3 privatevoid panel2_MouseEnter(object sender, EventArgs e)
4 {
5 this.panel2.BackColor = Color.FromArgb(65, 204, 212, 230);//鼠标移动到控件上时显示为半透明效果。
6 }
7
8 privatevoid panel2_MouseLeave(object sender, EventArgs e)
9 {
10 this.panel2.BackColor = Color.Transparent;//鼠标离开控件时控件变透明。
11
12 }
13
14 privatevoid panel2_MouseDown(object sender, MouseEventArgs e)
15 {
16 int xOffset;
17 int yOffset;
18 if (e.Button == MouseButtons.Left)
19 {
20 xOffset =-e.X;
21 yOffset =-e.Y;
22 mouseOffset =new Point(xOffset, yOffset);
23 isMouseDown =true;
24 }
25
26 }
27
28 privatevoid panel2_MouseUp(object sender, MouseEventArgs e)
29 {
30 // 修改鼠标状态isMouseDown的值
31 // 确保只有鼠标左键按下并移动时,才移动窗体
32 if (e.Button == MouseButtons.Left)
33 {
34 isMouseDown =false;
35 }
36
37 }
38
39 privatevoid panel2_MouseMove(object sender, MouseEventArgs e)
40 {
41 if (isMouseDown)
42 {
43 Point mousePos = Control.MousePosition;
44 mousePos.Offset(mouseOffset.X, mouseOffset.Y);
45 Location = mousePos;
46 }
47
48 }
49
posted @ 2011-04-22 21:37  毒漫情人  阅读(288)  评论(0编辑  收藏  举报