您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 嵌入式开发 > 正文


-ASP入门到精通
-矢量图象设计三剑客
-Visual Basic精彩编程100例
-玩转Excel“单元格格式”

用.NET Compact Framework创建图像按钮
2003-04-28· ·蚕蛹编译··yesky

1 2  下一页


  一、概述

  .NET Compact Framework为开发者设计用户界面提供大量的控件。首先必需知道在.NET Compact Framework1.0版本中并没有.NET Framework所有的特性。另外要求知道在WIN CE平台上的内存、CPU、其它资源。意义是说,必需完全了解在.NET Framework和.NET Compact Framework两者之间的各种差异.例如,大部份的标准控件如按钮,列表框和标签都不能对其OnPanit事件重载以及自画。在技术白皮书中有一个简单图像按钮的主要例子。在这个例子中主要表现在自定义自画按钮和在.NET Compact Framework中画线,文字和图片时用最佳的实用方法来实现最理想的图像性能。

  二、不能缺少什么?

  由于设备的限制,在.NET Compact Framework中并不支持所有的GDI+特性.同样你也可能知道,GDI+分成下列三大类别:

  1. 2-D vector graphics(2-D向量绘图)

  2. Imageing(成像)

  3. Typography(排版)

  2-D Vectore Graphics在 .NET Compact Framework中,Graphics对象核心仅仅是用来做画椭圆,线,图像,多边形,方形,字符串,填充椭圆,填充多边形,填充方形和填充区域。

  在Imaging这一类别中,Bitmap类支持图像处理(在小块区域)或保存图片文件。Typography(排版)的分类是有关字体变化,大小及样式。.NET Compact Framework提供对本程序的支持。但用不着害怕!我们仍然可以用GDI功能的子集创建强大的绘图应用程序。正好关注一下.NET Compact Framework中的System.Windows.Forms.DataGrid控件.该控件是在我们完全利用自画控件在上.NETCompact Framework的平台上创造的真实的高性能图形用来表示多行多列。

  三、建立图像的最佳实践

  1, 当我们建立自画控件时,衍生自System.Windows.Forms.Control和重载OnPaint以及OnPaintBackground事件。

using System;
using System.Drawing;
using System.Windows.Forms;

public class MyButton : Control
{

protected override void OnPaint(PaintEventArgs e )
{
//Some drawing logic
Graphics gr = e.Graphics;
gr.DrawImage(image, 0, 0);
Graphics gr = e.Graphics;
gr.DrawImage(image, 0, 0);
gr.DrawRectangle(pen, this.ClientRectangle);
}

protected override void OnPaintBackground(PaintEventArgs e )
{
//Do nothing
}

}

  2、建立好画笔(Pen)、位图(Bitmap)、笔刷(Brush)对象,然后用它们的OnPaint事件。操作系统需要一些时间来处理OnPaint事件。在实例化和清除这些对象的同时也就影响了图像性能。

  3、用双缓存来减少图像在屏幕刷新时的闪烁。通常,双缓存逻辑上按下面的样子:

protected override void OnPaint(PaintEventArgs e )
{
Graphics gxOff; //Offscreen graphics

if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
}

gxOff = Graphics.FromImage(m_bmpOffscreen);

gxOff.Clear(this.BackColor);
//Draw some bitmap
gxOff.DrawImage(bmpParent, 0, 0, bmpRect, GraphicsUnit.Pixel);

//Boundary rectangle
Rectangle rc = this.ClientRectangle;
rc.Width--;
rc.Height--;

//Draw boundary
gxOff.DrawRectangle(new Pen(Color.Black), rc);
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

base.OnPaint(e);
}

  在以上代码中,我们用Graphics对象建立了一个空的位图(Bitmap),能过Graphics类的静态方法FormImage来获取图像在控件中的大小,我们将在内存中把这些图形对象画好,当所有的好了后,才准确的把这些图形对像画在控件上,可以通过.NET Compact Framework中的ImageAttributes的参数来实现位图的透明。

ImageAttributes imageAttr = new ImageAttributes();
// Make a transparent key
imageAttr.SetColorKey(Color.White, Color.White);
//Use the key when drawing
gxOff.DrawImage(m_bmp, imgRect, 0, 0, img.Width, img.Height,GraphicsUnit.Pixel, imageAttr);


  记住那些低和高的颜色键值,在SetColorKey方法中应该设置成一样的值。主要是因为.NET Compact Framework不支持用任何方法设置透明色。

1 2  下一页

【责任编辑:方舟】
【发表评论】【关闭窗口】
■ 相关内容
 微软发布.NET Compact Framework
 三汇研制成功首块国产CompactPCI语音卡
 摩托罗拉CompactPCI助JETSTREAM实现VoDSL
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!