春节期间作了一个浮动的控件,照着windows MDI创体做的,感觉还挺有意识的,放上来记录一下,呵呵。
标尺 和 网格 有的代码是来原于网络,具体作者是谁不记得了,谢过了。也拜个年了。
codeproject上有篇文章不错的,是画标尺的
http://www.codeproject.com/cs/miscctrl/ruler.asp
先来个画面截图把

上图中SubFrom1和SubFrom2就是两个浮动控件,他们可以改变大小,前端表示,最大化,复原等一些动作。
当然了还可以在里面添加其他的控件, 其中SubForm1中加就添加了一个标尺的控件,
添加方法如下

浮动控件中添加其他控件
1
RulerControl rc = new RulerControl();
2
rc.Size = this.subForm1.WorkSize;
3
rc.Location = new Point(0, 0);
4
rc.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
5
| System.Windows.Forms.AnchorStyles.Left)
6
| System.Windows.Forms.AnchorStyles.Right)));
7
this.subForm1.Controls.Add(rc);
下面把几个主要的功能的实现说一下,
1· 鼠标按下这个控件前端显示,
先是捕获整个控件的上的左键按下事件,然后使用了Controls.SetChildIndex() 方法来控制Z轴的次序。
代码如下
1
protected override void WndProc(ref Message m)
2

{
3
// 右键按下
4
// LBUTTONDOWN
5
if (m.Msg == 0x210)
6
{
7
if (!(isMaxSize || this.Equals(Parent.TopLevelControl)))
8
{
9
this.Parent.Controls.SetChildIndex(this, 0);
10
}
11
}
12
base.WndProc(ref m);
13
}
上面代码中的 0x210 查了老半天也没有查到,后来在代码中加Debug才得到的,太苯了 呵呵!
2·控件的移动
开始没有控制移动的范围,结果移到窗口外边了,在也找不到了。

控件移动
1
private bool isMouseDown = false;
2
int oldX = 0;
3
int oldY = 0;
4
5
private void lblTitle_MouseDown(object sender, MouseEventArgs e)
6

{
7
if (!isMaxSize)
8
{
9
isMouseDown = true;
10
oldX = e.X;
11
oldY = e.Y;
12
}
13
}
14
15
private void lblTitle_MouseUp(object sender, MouseEventArgs e)
16

{
17
isMouseDown = false;
18
}
19
20
private void lblTitle_MouseMove(object sender, MouseEventArgs e)
21

{
22
23
if (isMouseDown)
24
{
25
int x = Location.X + (e.X - oldX);
26
int y = Location.Y + (e.Y - oldY);
27
28
// 边界设定
29
if (x < lblTitle.Height - Width)
30
{
31
x = lblTitle.Height - Width;
32
}
33
else if (x > (Parent.Width - lblTitle.Height))
34
{
35
x = Parent.Width - lblTitle.Height;
36
}
37
if (y < 0)
38
{
39
y = 0;
40
}
41
else if (y > (Parent.Height - lblTitle.Height))
42
{
43
y = Parent.Height - lblTitle.Height;
44
}
45
46
this.Location = new Point(x, y);
47
}
48
}
3·最大化与复原
在最大化的时候除了要记住开始的大小外,还要记住位置。

最大化与复原
1
private Size oldS ;
2
private Point oldLoc;
3
private bool isMaxSize = false;
4
5
private void lblTitle_DoubleClick(object sender, EventArgs e)
6

{
7
if (isMaxSize)
8
{
9
Size = oldS;
10
Location = oldLoc;
11
isMaxSize = false;
12
13
Anchor = ((AnchorStyles)((( AnchorStyles.Left | AnchorStyles.Top))));
14
15
this.reSizeControl1.Enabled = true;
16
}
17
else
18
{
19
20
oldS = Size;
21
oldLoc = Location;
22
23
Location = new Point(0, 0);
24
Anchor = ((AnchorStyles)(((AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right))));
25
26
this.reSizeControl1.Enabled = false;
27
isMaxSize = true;
28
Size = new Size(Parent.Width, Parent.Height);
29
}
30
31
Invalidate();
32
}
4·控件容器。
在控件中方一个panel用于来放置其他控件 然后覆盖父类的Controls方法,返回panel的Controls,
这时需要注意一下,应该在执行完InitializeComponent()之后才能让它返回panel的Controls。
否则还是返回基类的Controls。

public new ControlCollection Controls
1
private bool isLoad = false;
2
public SubForm()
3

{
4
InitializeComponent();
5
6
isLoad = true;
7
// Application.AddMessageFilter(this);
8
lblTitle.Text = this.Name;
9
10
this.reSizeControl1.MinSize = new Size(this.lblTitle.Height * 3, this.lblTitle.Height + this.reSizeControl1.Height);
11
}
12
13
public new ControlCollection Controls
14

{
15
get
16
{
17
if (isLoad)
18
{
19
return pnlMain.Controls;
20
}
21
else
22
{
23
return base.Controls;
24
}
25
}
26
}
整个工程的代码
浮动控件今天就到这儿了。