| |
用.NET Compact Framework创建图像按钮 |
|
时间: 2003-04-28 来自:yesky |
 |
|
三、应用规则
在实际的操作中,我们将建立一个自画的图像按钮控件.需要在按钮上面显示一个图片,当用户在按按钮时可以看到效果。
我们开始 Visual Studio .NET中建立一个SmartDevice项目的Windows应用程序.在这个项目中我们加入一个新的ImageButton类。
using System; using System.Drawing; using System.Windows.Forms;
public class ImageButton : Control { //Private members private Image image; //flag to indicate the pressed state private bool bPushed; private Bitmap m_bmpOffscreen;
public ImageButton() { bPushed = false; //default minimal size this.Size = new Size(21, 21); }
protected override void OnPaint(PaintEventArgs e ) { //Some drawing logic }
protected override void OnPaintBackground(PaintEventArgs e ) { //Do nothing } } | 在以上代码中,我们用Control控件中的ImageButton类,和重载OnPaint以及OnPaintBackground事件,在上面提到,ImageButton将要一个图片用来显示,所以我们添加图像属性:
public Image Image { get { return image; } set { image = value; } } | 当然,还有重载OnPaint事件来绘制按钮
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e ) { Graphics gxOff; //Offscreen graphics Rectangle imgRect; //image rectangle Brush backBrush; //brush for filling a backcolor
if (m_bmpOffscreen == null) //Bitmap for doublebuffering { m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height); }
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
if (!bPushed) backBrush = new SolidBrush(Parent.BackColor); else //change the background when it's pressed backBrush = new SolidBrush(Color.LightGray);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
if (image != null) { //Center the image relativelly to the control int imageLeft = (this.Width - image.Width) / 2; int imageTop = (this.Height - image.Height) / 2;
if (!bPushed) { imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height); } else //The button was pressed { //Shift the image by one pixel imgRect = new Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height); } //Set transparent key ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image)); //Draw image gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
if (bPushed) //The button was pressed { //Prepare rectangle Rectangle rc = this.ClientRectangle; rc.Width--; rc.Height--; //Draw rectangle gxOff.DrawRectangle(new Pen(Color.Black), rc); }
//Draw from the memory bitmap e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e); } | 根据上面章节的提要,我们成功的按.NET Compact Framework的画图方法写下了这些代码。我们先建立好绘图对象的缓存,并尽可能的用双缓存来减少图像闪烁。我们用下面的函数来帮助识别背景色,使图像透明:
private Color BackgroundImageColor(Image image) { Bitmap bmp = new Bitmap(image); return bmp.GetPixel(0, 0); } | 上面的函数返加位图左上角的颜色。当在画按钮时,我们用bPushed标志判断按钮按下否。我们可以很容易重载OnMouseDown和OnMouseUp事件:
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e ) { bPushed = true; this.Invalidate(); }
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e) { bPushed = false; this.Invalidate(); } | 五、用法
可以很容易的新建一个ImageButton控件。把下面的少量代码添加到Form 结构中:
public Form1() { InitializeComponent();
imageButton1 = new ImageButton(); imageButton1.Image = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("ImageButtonProject.doorin2.gif")); imageButton1.Location = new Point(30, 50); imageButton1.Size = new Size(44, 34); //Hook up into click event imageButton1.Click+=new EventHandler(imageButton1_Click); this.Controls.Add(imageButton1); } | 我们添加doorin2.gif文件,同样可以在很容易用GetManifestResourceStream方法当前集合在项目的资源中。
图一(程序界面上三个按钮)
图二(第一个控件按下时)
六、结论
.NET Compact Framework并不像.NET Framework一样提供很多可用的图形和绘图函数。这是由于平台资源限制的缘故。不过,我们可以用一些自画技巧,在用户程序中建立大量丰富和可响应的图形。
|
|
|
|
|
|
|
|