设为首页
加入收藏
联系我们
首页 基础教程 技术文档 实例分析 数 据 库 疑难杂症 ASP.NET 七夕许愿树 技术论坛
最新文章
  .NET上传图片加文字…
 在vs.net bate…
 C#中利用正则表达式实现…
 Visual C#的SQ…
 关于使用存储过程创建分页
 通用分页显示查询存储过程
 大数据量的分页
 什么是web.confi…
 ASP.NET 配置文件…
 对“三层结构”的深入理解…
 多个关键字的查询问题
 VB和C# 语法对比图 …
 C#基础全接触
 用C#压缩和修复Acce…
 C#中的类型相等与恒等(…
推荐文章
 关于C#中的REF和黓认…
 读书笔记c#高级编程 委…
 【算法】C#快速排序类
 Visual C#的SQ…
 C#中调用API
 Infragistics…
 C#接口转换
 C#读取设备信息
 用.net操作word
 C# MessageBo…
 Visual C#中的数…
 雅虎公司C#笔试题,看看…
 C#.NET使用NHib…
 .net学习之运算符重载…
 Visual C# 3.…
热门文章
 ADO.Net与ADO在…
 开发ASP.NET下的M…
 用C#+XMI技术进行U…
 什么是虚拟机?
 C#基础全接触
 C#中利用正则表达式实现…
 雅虎公司C#笔试题,看看…
 用Visual C# 2…
 C#学习第一天
 C#语言初级入门(1)
 VB和C# 语法对比图 …
 远程重启计算机(C#)
 什么是B/S三层?
 Visual C#的SQ…
 Visual C#常用函…
创建不规则窗体和控件(3)
阅读正文 文字大小:增大 减小  文字行距:增大 减小   双击自动滚屏
本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

一个更好的例子>

下面我给大家介绍一个更好的例子,该实例的界面就和微软的Windows Media Player 7的界面差不多。不过在这个例子中,
我只是介绍了如何制作出界面,并没有实现媒体播放的功能,要实现那些功能,还需要您自行完善。该实例运行的界面如下:

1.将某种颜色设置为窗体的背景颜色,然后将窗体的TransparenceKey属性设置为那种颜色,同时将窗体的FormBorderStyle属性设置为None。

2.重载Form_Paint()函数:

protected override void  OnPaint(PaintEventArgs e)
或是this.Paint += new System.Windows.Form.PaintEventHandler(Form_Paint)。


3.程序的主体部分的函数如下:

private void Form_Paint(object sender, PaintEventArgs e)
	{
		Graphics g = e.Graphics;
			Rectangle mainRect = new Rectangle(0, 0, 695, 278);
			Region mainRegion = new Region(mainRect);
			e.Graphics.SetClip(mainRegion, CombineMode.Replace);
    			Point point1 = new Point(0, 32);
			Point point2 = new Point(9, 20);
			Point point3 = new Point(21, 13);
			Point point4 = new Point(34, 9);
    			// 创建一个以点为元素的数组
			Point[] curvePoints = { point1, point2, point3, point4 };
			// 创建一个GraphicsPath对象并添加一条曲线
			GraphicsPath myPath = new GraphicsPath();
			myPath.AddCurve(curvePoints, 0, 3, 0.8f);
			myPath.AddLine(36, 9, 378, 9);
			point1.X=378;point1.Y=9;
			point2.X=387;point2.Y=5;
			point3.X=394;point3.Y=0;
			Point[] curvePoints2 = { point1, point2, point3 };
			myPath.AddCurve(curvePoints2, 0, 2, 0.8f);
			myPath.AddLine(394, 0, 0, 0);
			Region ExcludeRegion3 = new Region(myPath);
			e.Graphics.ExcludeClip(ExcludeRegion3);
    
			GraphicsPath myPath3 = new GraphicsPath();
			point1.X=0;point1.Y=180;
			point2.X=19;point2.Y=198;
			point3.X=62;point3.Y=204;
			point4.X=83;point4.Y=221;
			Point point5 = new Point(93, 248);
			Point point6 = new Point(102, 267);
			Point point7 = new Point(125, 278);
			Point[] curvePoints3 = { point1, point2, point3, point4, 
point5, point6, point7 };
			myPath3.AddCurve(curvePoints3, 0, 6, 0.8f);
			myPath3.AddLine(125, 278, 90, 300);
			myPath3.AddLine(90, 300, 0, 300);
			Region ExcludeRegion2 = new Region(myPath3);
			e.Graphics.ExcludeClip(ExcludeRegion2);
			point1.X=454;point1.Y=0;
			point2.X=470;point2.Y=12;
			point3.X=481;point3.Y=34;
			Point[] curvePoints4 = { point1, point2, point3 };
			GraphicsPath myPath2 = new GraphicsPath();
			myPath2.AddCurve(curvePoints4, 0, 2, 0.8f);
			myPath2.AddLine(481, 30, 481, 76);
			myPath2.AddLine(481, 76, 495, 76);
			myPath2.AddLine(495, 76, 495, 0);
			Region ExcludeRegion4 = new Region(myPath2);
			e.Graphics.ExcludeClip(ExcludeRegion4);
			GraphicsPath myPath5 = new GraphicsPath();
			point1.X=481;point1.Y=76;
			point2.X=494;point2.Y=115;
			point3.X=481;point3.Y=158;
			Point[] curvePoints5 = { point1, point2, point3 };
			myPath5.AddCurve(curvePoints5, 0, 2, 0.8f);
			myPath5.AddLine(481, 158, 481, 279);
			myPath5.AddLine(481, 255, 495, 279);
			myPath5.AddLine(495, 279, 495, 0);
			Region ExcludeRegion6 = new Region(myPath5);
			e.Graphics.ExcludeClip(ExcludeRegion6);

			point1.X=480;point1.Y=250;
			point2.X=469;point2.Y=264;
			point3.X=446;point3.Y=278;
			Point[] curvePoints6 = { point1, point2, point3 };
			GraphicsPath myPath4 = new GraphicsPath();
			myPath4.AddCurve(curvePoints6, 0, 2, 0.8f);
			myPath4.AddLine(450, 277, 495, 279);
			Region ExcludeRegion5 = new Region(myPath4);
			e.Graphics.ExcludeClip(ExcludeRegion5);

			e.Graphics.DrawImage(img, 0, 0, 695,278);
    
			// 重设剪切好的区域
			e.Graphics.ResetClip();
	}


该函数运用Region类和GraphicsPath类实现了程序的主界面,函数中具体的数据可能会因图片大小等原因而有所不同,至于其他的消息响应函数读者可以参看附带的源码文件(Source.rar)。

总结


通过本文,我们不难发现在Visual C#下创建不规则窗体以及自定义形状的控件是件相当容易的事。我们仅仅需要一幅不规则的图像就可以完成不规则窗体的创建;我们也只要用到GraphicsPath类对象就可轻易地创建出自定义形状的控件。相信读者在读完本文后,对在.Net下创建丰富的用户界面有了基本的了解。希望各位读者因此能创建出更丰富的、更赋时代特色的用户界面。

本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

中国C#技术交流QQ群:6337034  10976424  9383681  35248582  35248645
版权所有:中国C#技术学习中心 Copyright © 20010-20012
建议浏览分辨率使用:1024*768分辨率
粤ICP备05002251号