转:http://www.cnblogs.com/qilinzhi/archive/2008/04/19/1160977.html
在Windows CE上的Form可以控制它的大小,但是不能像winform上的窗体一样随意拖动,并且当控制Form大小后Form只会显示其工作区,而不会显示它的caption。
为了实现窗体拖动,我们可以使用模拟的方式实现,用鼠标的事件来模拟拖动。最下面有示例代码下载。
为了实现可以拖动的窗体,首先要为窗体做如下设置:
将要拖动的Form的如下属性全部设为false
ControlBox,MinimizeBox,MaximizeBox
将FormBorderStyle设为FormBorderStyle.None。
这样才能使窗体以自定义的大小显示。
由于浮动的窗体不会显示caption,所以我们要自己模拟一个capting,首先我们写一个继承于Panel的类TitleBarPanel,用作captiong,代码如下:
1
public
class
TitleBarPanel:System.Windows.Forms.Panel
2

{
3
internalboolmouseDown=false;
4
internalPointoldPoint=Point.Empty;
5
6
protectedSystem.Windows.Forms.LabeltitleLabel;
7
8
9
publicTitleBarPanel(stringtitle)
10

{
11
this.titleLabel=newLabel();
12
titleLabel.Top=0;
13
this.Controls.Add(this.titleLabel);
14
this.titleLabel.Font=newSystem.Drawing.Font("Tahoma",8F,System.Drawing.FontStyle.Bold);
15
this.titleLabel.ForeColor=System.Drawing.Color.White;
16
this.titleLabel.Location=newSystem.Drawing.Point(4,2);
17
this.titleLabel.Size=newSystem.Drawing.Size(100,16);
18
this.titleLabel.Text=title;
19
}
20
21
22
overrideprotectedvoidOnMouseDown(MouseEventArgse)
23

{
24
mouseDown=true;
25
oldPoint=newPoint(e.X,e.Y);
26
((FloatingDialog)this.Parent).Moving=true;
27
}
28
29
30
overrideprotectedvoidOnMouseMove(MouseEventArgse)
31

{
32
if(mouseDown)
33

{
34
intdx,dy;
35
dx=e.X-oldPoint.X;
36
dy=e.Y-oldPoint.Y;
37
38
this.Parent.Location=newPoint(this.Parent.Left+dx,this.Parent.Top+dy);
39
40
this.Parent.Refresh();
41
}
42
}
43
44
overrideprotectedvoidOnMouseUp(MouseEventArgse)
45

{
46
mouseDown=false;
47
((FloatingDialog)this.Parent).Moving=false;
48
this.Parent.Refresh();
49
}
50
}
其中有一个Label用于caption显示的标题。
mouseDown属性用于表示鼠标是否处于按下状态。
该类重写了OnMouseDown,OnMouseMove,OnMouseUp事件,用于实现拖动。
在MouseDown事件触发的时候,将当olePoint设为当前点,并将包含该标题的窗体的moving属性设为true。
FloatingDialog的代码在下面。
当MouseMove事件触发时,如果鼠标是按下状态,就计算出位移量dx,dy,并将窗体的位置加上算出的偏移量。接着调用Refresh方法刷新窗体,使窗体触发OnPaint方法重画。
OnMouseUp触发时,将mouseDown设为false,并将窗体的moving属性设为false,并刷新窗体。
为了实现可拖动的窗体,我写了一个基类FloatingDialog,只要继承于该类的窗体即可实现拖动。代码如下:
public
class
FloatingDialog:Form

{
privateSystem.ComponentModel.IContainercomponents=null;

/**////<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">如果应释放托管资源,为true;否则为false。</param>
protectedoverridevoidDispose(booldisposing)


{
if(disposing&&(components!=null))


{
components.Dispose();
}
base.Dispose(disposing);
}

protectedTitleBarPaneltitlePanel;

protectedPenblackPen=newPen(Color.Black);
privateboolmoving=false;

internalboolMoving


{

get
{returnmoving;}

set
{moving=value;}
}

publicFloatingDialog(stringtitle)


{
InitializeComponent(title);
}

publicvoidInitializeComponent(stringtitle)


{
this.SuspendLayout();

//实例化TitlePanel
this.titlePanel=newTitleBarPanel(title);

//使Form浮动
this.FormBorderStyle=FormBorderStyle.None;
this.ControlBox=false;
this.MinimizeBox=false;
this.MaximizeBox=false;

//设置titlepanel
this.titlePanel.Bounds=newRectangle(0,0,this.Width,16);
this.titlePanel.BackColor=System.Drawing.SystemColors.ActiveCaption;
this.Controls.Add(this.titlePanel);

this.AutoScaleDimensions=newSystem.Drawing.SizeF(96F,96F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll=false;
//this.ClientSize=newSystem.Drawing.Size(240,100);


this.ResumeLayout(false);
}

protectedoverridevoidOnPaint(PaintEventArgse)


{
Graphicsg=e.Graphics;

if(!titlePanel.mouseDown)


{
base.OnPaint(e);
}

Rectangler=this.ClientRectangle;
//r.X=1;
//r.Y=1;
r.Height-=1;

r.Width-=1;
g.DrawRectangle(blackPen,r);


}
}
该类中有三个属性,titlePanel, blackPen, moving.
titlePanel即为前面写的TitleBarPanel类,为窗体的标题。
由于窗体浮动显示的时候是没有边框,所以要自己画一个边框上去,balckPen就是用来画边框的。
moving用于表示窗体是否在移动。
该类重写了OnPaint事件,用于画边框。
剩下的工作就是添加一个FloatingForm窗体,并继承于FloatingDialog类,
在主Form中调用如下方法:
public
void
AddFloatingForm()

{
this.SuspendLayout();

FloatingFormfloatingForm=newFloatingForm();
floatingForm.Location=newPoint((this.Width-floatingForm.Width)/2,50);

this.Controls.Add(floatingForm);
floatingForm.Show();
floatingForm.BringToFront();

this.ResumeLayout();
}
即可实现FloatingForm拖动
示例代码:下载