ASP.net控件开发系列(六)
点击次数:26 次 发布日期:2008-12-31 14:42:15 作者:源代码网
|
源代码网推荐
UITypeEdit
“我要红桃”
假如,你现在在做一个“扑克”控件,扑克牌有个属性--花色,你想在用户选择花色这个属性后,属性窗口呈现的不仅仅是文字,还有一个小小的花色图标来表示花色,“红桃”就有个小“红桃”图标在前面显示,“黑桃”就有个“黑桃”图标在前面显示,就像你选择其它控件的BackColor时,颜色前还有个小方色块来表示选定的颜色,多体贴人的设计啊。
现在,我们就来做这件事:
public class Squeezer { . public CardTypes CardType { } } [Editor(typeof(CardTypesEditor), typeof(System.Drawing.Design.UITypeEditor))] public class CardTypes { .. } public class CardTypesEditor : UITypeEditor { public override bool GetPaintValueSupported(ITypeDescriptorContext context) { return true;//支持画小图 } public override void PaintValue(PaintValueEventArgs pe) //定义根据值画小图的逻辑 { string bmpName = null; CardTypes C = (CardTyes)pe.Value; switch(C.Value) { case CarderTypes.HongTao: bmpName = "红桃.bmp";//图片必须是嵌入的资源,大小为16*16,类型为BMP break; } Bitmap b = new Bitmap(typeof(GradeEditor), bmpName); pe.Graphics.DrawImage(b, pe.Bounds); b.Dispose(); } }
在上面的代码中,我们通过EditorAttribute来使花色类和一个Editor关联,再通过这个Editor来实现画示意小图的功能
本文作者:未知
源代码网供稿. |