,VC.net   工具软件   办公软件   操作系统   网络安全   设计在线   程序开发   教程宝典   软件下载   软件论坛,VC.net
您的位置:软件 > 开发者网络 > 微软开发专栏 > Visual Studio.net专栏 > VC.net > 正文
基于VC.NET的GDI+编程入门之画刷
[文章信息]
作者:刘涛
时间:2005-04-19
出处:天极网
责任编辑:方舟
[文章导读]
基于线条的、封闭的图形需要画笔来表现,封闭的形状的典型的特点在于它可以用图形、颜色或模式来填充
advertisement
热点推荐
· VB实现SQL Server 2000存储过程调用
· Java解析网络数据流的三种特殊方法
· 多媒体教程:网页表单文本域类型
· 体验V5E地形编辑:整体控制
· 打造完美可随意安装的WinXP镜像
[正文]

上一页  1 2 3 4  下一页

  (三)纹理画刷

  网格画刷依赖于已经预先设计的模式来填充图形,在某些情况下,需要设计自己的图案来填充一个图形。要这么做,必须执行两个步骤,首先设计一个图形并存储为一个文件,然后创建一个纹理画刷,并将图案传递给它。

  纹理画刷拥有图案,并且通常使用它来填充封闭的图形。为了对它初始化,可以使用一个已经存在的别人设计好了的图案,或使用常用的设计程序设计的自己的图案,同时应该使图案存储为常用图形文件格式,如BMP格式。这里有一个设计好的位图,被存储为Papers.bmp文件。


图四、位图效果图

  有了图案,这时候就可以使用TextureBrush类,它有多种构造函数,最简单的构造函数只有一个Image对象做为参数,这个构造函数的语法是:

public: TextureBrush(Image *bitmap);

  这个构造函数使用位图作为参数。初始化画刷后,你可以使用它来填充封闭的图形。例如你可以调用Fill...方法。下面是例子:

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers);
e->Graphics->FillRectangle(brushPapers, 5, 5, 430, 280);
}


图五、代码运行效果图

  如果使用这个构造函数,编译器自身将对图案的尺寸和位置进行定位,尽管位置必须在(0,0)处,但长和宽必须小于或等于设计的图形尺寸,例如,有一个图案是48x48像素,你使用的图形的宽度和高度必须小于等于48,这样就允许只使用图形的一部分,为此需要使用下面的构造函数:

public: TextureBrush(Image *bitmap, Rectangle destination);

  这个函数的第二个参数规定了图案的尺寸,如果愿意使用十进制的矩形参数,可以使用如下构造函数:

public: TextureBrush(Image *bitmap, RectangleF destination);

  这有一个例子:

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers,
new Rectangle(0, 0, 40, 42));
e->Graphics->FillRectangle(brushPapers, 5, 5, 438, 290);
}


图六、代码运行效果图

  目前为止我们使用的构造函数都是以平铺的方式在每个定位的矩形内绘制图案。为了更有趣,TextureBrush类提供了一个参数用来指定覆盖模式,可以使用下面的构造函数,

public: TextureBrush(Image *bitmap, WrapMode wrapMode);
public: TextureBrush(Image *bitmap, WrapMode wrapMode, Rectangle destination);
public: TextureBrush(Image *bitmap, WrapMode wrapMode, RectangleF destination);

  位图和尺寸参数与上述含义一样,wrapMode参数是枚举WrapMode的一个成员,枚举WrapMode定义在System::Drawing::Drawing2D名字空间中。

  它有下面的成员:

  ·Clamp:在分配的矩形内只绘制一个图案。

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers, WrapMode::Clamp);
e->Graphics->FillRectangle(brushPapers, 5, 5, 438, 290);
}


图七、Clamp方式效果图

  ·Tile:以平铺方式绘图

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers,
WrapMode::Tile, Rectangle(0, 0, 40, 42));
e->Graphics->FillRectangle(brushPapers, 5, 5, 438, 290);
}


图八、Tile方式效果图

  ·TileFlipX:水平方向交差对称方式

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers, WrapMode::TileFlipX);
e->Graphics->FillRectangle(brushPapers, 5, 5, 430, 290);
}


图九、TileFlipX方式效果图

  ·TileFlipXY:水平竖直方向均交叉对称

private: System::Void Form1_Paint(System::Object * sender,
System::Windows::Forms::PaintEventArgs * e)
{
Bitmap *bmpPapers = new Bitmap(S"Papers.bmp");
TextureBrush *brushPapers = new TextureBrush(bmpPapers,
WrapMode.TileFlipXY, Rectangle(0, 0, 42, 42));
e->Graphics->FillRectangle(brushPapers, 5, 5, 412, 244);
}


图十、TileFlipXY方式效果图


上一页  1 2 3 4  下一页

发表评论推荐给朋友我想参加相关培训打印我对此感兴趣订阅电子杂志
天极社区邀请您:写博客日记  上传相片   论坛聊天  订阅电子杂志  推荐网摘   免费图铃工具
笔名:   请您注意:

 遵守国家有关法律、法规,尊重网上道德,承担一切因您的行为而直接或间接引起的法律责任。

 天极网拥有管理笔名和留言的一切权利。
评论:
 
,VC.net相关内容,VC.net焦点新闻
  • VC++2005快速构建安全的应用程序
  • 完美的C++:C++/CLI
  • 体验Visual Studio 2005中C++语言
  • 超越C++:下一代C++——C++/CLI简介
  • Visual C++ 2005中混合代码的初始化
  • FVD刺激高清碟机加速商业化 抢占商机最重要
  • 3家搜索引擎集体诉讼8848 吕春维未敢出席
  • 杨元庆:没有准备不会获批的备用方案
  • 军队信息化诞生新领域 电子军务呼之欲出
  • 世界经济论坛公布信息化程度全球最新排名
  • 2004政务绩效评估:政府门户尚处于发展阶段
  • 甲骨文出资5.15亿美元 意图收购RetekInc
  • 技术并购:帮你突破传统增长的“天花板”
  • ,VC.netAdvertisement