一个更好的例子
下面我给大家介绍一个更好的例子,该实例的界面就 和微软的Windows Media Player 7的界面差 不多。不过在这个例子中,
我只是介绍了如何 制作出界面,并没有实现媒体播放的功能,要实现那些功能,还需要 您自行完善。该实例运行的界面如下:
1.将某种颜色设置为窗体的背景颜色,然后将窗体的 TransparenceKey属性设置为那种颜色,同时将窗体 的FormBorderStyle属性设置为None。
2.重载Form_Paint( )函数:
< br>
protected override vo id OnPaint(PaintEventArgs e)< br>或是this.Paint += new System. Windows.Form.PaintEventHandler (Form_Paint)。
3.程序 的主体部分的函数如下:
pr ivate void Form_Paint(object s ender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle mainRect = ne w Rectangle(0, 0, 695, 278);
Region mainRegion = new R egion(mainRect);
e.Graph ics.SetClip(mainRegion, Combin eMode.Replace);
Poin t point1 = new Point(0, 32);
Point point2 = new Point( 9, 20);
Point point3 = n ew Point(21, 13);
Point point4 = new Point(34, 9);
// 创建一个以点为元素的数组
P oint[] curvePoints = { point1, point2, point3, point4 };
// 创建一个GraphicsPath对象并添加一条曲 线
GraphicsPath myPath = new GraphicsPath();
myPa th.AddCurve(curvePoints, 0, 3, 0.8f);
myPath.AddLine(3 6, 9, 378, 9);
point1.X= 378;
point1.Y=9;
point2.X =387;
point2.Y=5;
point3. X=394;
point3.Y=0;
Point[ ] curvePoints2 = { point1, poi nt2, point3 };
myPath.Ad dCurve(curvePoints2, 0, 2, 0.8 f);
myPath.AddLine(394, 0, 0, 0);
Region Exclude Region3 = new Region(myPath);
< br> e.Graphics.ExcludeClip(E xcludeRegion3);
GraphicsPath myPath3 = new Gra phicsPath();
point1.X=0;
point1.Y=180;
point2.X=1 9;
point2.Y=198;
point3.X =62;
point3.Y=204;
point4 .X=83;
point4.Y=221;
Poin t point5 = new Point(93, 248);
Point point6 = new Poin t(102, 267);
Point point 7 = new Point(125, 278);
Point[] curvePoints3 = { poin t1, point2, point3, point4, point5, point6, point7 };
myPath3.AddCurve(curvePoin ts3, 0, 6, 0.8f);
myPath 3.AddLine(125, 278, 90, 300);
< br> myPath3.AddLine(90, 300, 0, 300);
Region Exclude Region2 = new Region(myPath3);
e.Graphics.ExcludeClip( ExcludeRegion2);
point1. X=454;
point1.Y=0;
point2 .X=470;
point2.Y=12;
poin t3.X=481;
point3.Y=34;
Po int[] curvePoints4 = { point1, point2, point3 };
Graph icsPath myPath2 = new Graphics Path();
myPath2.AddCurve (curvePoints4, 0, 2, 0.8f);
myPath2.AddLine(481, 30, 4 81, 76);
myPath2.AddLine (481, 76, 495, 76);
myPa th2.AddLine(495, 76, 495, 0);
< br> Region ExcludeRegion4 = new Region(myPath2);
e.G raphics.ExcludeClip(ExcludeReg ion4);
GraphicsPath myPa th5 = new GraphicsPath();
point1.X=481;
point1.Y=76;
point2.X=494;
point2.Y=115;
point3.X=481;
point3.Y=1 58;
Point[] curvePoints5 = { point1, point2, point3 };
myPath5.AddCurve(curveP oints5, 0, 2, 0.8f);
myP ath5.AddLine(481, 158, 481, 27 9);
myPath5.AddLine(481, 255, 495, 279);
myPath5 .AddLine(495, 279, 495, 0);
Region ExcludeRegion6 = ne w Region(myPath5);
e.Gra phics.ExcludeClip(ExcludeRegio n6);
point1.X=480;
po int1.Y=250;
point2.X=469 ;
point2.Y=264;
point3.X= 446;
point3.Y=278;
Point[ ] curvePoints6 = { point1, poi nt2, point3 };
GraphicsP ath myPath4 = new GraphicsPath ();
myPath4.AddCurve(cur vePoints6, 0, 2, 0.8f);
myPath4.AddLine(450, 277, 495, 279);
Region ExcludeReg ion5 = new Region(myPath4);
e.Graphics.ExcludeClip(Exc ludeRegion5);
e.Grap hics.DrawImage(img, 0, 0, 695, 278);
// 重设剪切好的区 域
e.Graphics.ResetClip() ;
}
< br>
该函数运用Regio n类和GraphicsPath类实现了程序的主界面,函数中具 体的数据可能会因图片大小等原因而有所不同,至于其他的消息响应 函数读者可以参看附带的源码文件(Source.rar)。 < br>
总结
通过本文,我们 不难发现在Visual C#下创建不规则窗体以及自定义形状的 控件是件相当容易的事。我们仅仅需要一幅不规则的图像就可以完成 不规则窗体的创建;我们也只要用到GraphicsPath类对 象就可轻易地创建出自定义形状的控件。相信读者在读完本文后,对 在.Net下创建丰富的用户界面有了基本的了解。希望各位读者因 此能创建出更丰富的、更赋时代特色的用户界面。