自定义组件之属性(Property)的性质(Attribute)介绍(四)
2024-07-21 02:24:24
供稿:网友
 
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。二:ui属性编辑器(uitypeeditor)
这里的属性编辑器的意思是能够实现上面提到的弹出对话框和下拉ui的形式。废话不说下面我们一一介绍。
1、 弹出对话框的形式
在本例中我使用了string类型的属性来显示版本的信息,大家可以随便的写各类的属性,这里只需要指定改属性的编辑器就可以了。
首先我们要建立一个string类型的属性,代码如下:
 private string _appver="1.0";
 
 [categoryattribute("自定义编辑器"),
 defaultvalueattribute("1.0"),
 descriptionattribute("版本信息"),
 readonlyattribute(true),
 editorattribute(typeof(appverconverter),typeof(system.drawing.design.uitypeeditor))]
 public string appver
 {
 get {return this._appver;}
 set {this._appver=value;}
 }
大家可能已经注意到了在这个属性之多出了一个性质editorattribute(typeof(appverconverter),typeof(system.drawing.design.uitypeeditor)),具体的意思大家可以参考msdn我在这里就不用多说了,那么我们看看appverconverter这个类是怎么实现的就可以了。具体代码如下:
/// <summary>
 /// 自定义ui的属性编辑器(弹出消息)
 /// </summary>
 public class appverconverter:system.drawing.design.uitypeeditor
 {
 /// <summary>
 /// 覆盖此方法以返回编辑器的类型。
 /// </summary>
 public override system.drawing.design.uitypeeditoreditstyle geteditstyle(system.componentmodel.itypedescriptorcontext context)
 {
 return system.drawing.design.uitypeeditoreditstyle.modal;
 }
 
 /// <summary>
 /// 覆盖此方法以显示版本信息
 /// </summary>
 public override object editvalue(system.componentmodel.itypedescriptorcontext context, system.iserviceprovider provider, object value)
 {
 system.windows.forms.messagebox.show("版本:1.0/n作者:张翔","版本信息");
 return value;
 }
 }
这里需要说明的是我们的属性编辑器必须从system.drawing.design.uitypeeditor继承,要不然就不能显示ui了。uitypeeditoreditstyle方法的返回值决定了改属性编辑器的类型大家可以参考msdn我在这里就不多说了。编译之后就可以看到如下的画面了:
2、 下拉ui的类型
下拉ui类型主要是提供给用户一个简单的界面来选择所要确定的属性,这种方式提供给用户非常友好的界面。下面的例子我们首先定义里一个point类型的属性,在默认的情况下这种类型的属性是会以展开的形式来让用户编辑的。在这里我们扩展了他的功能,不仅仅能通过直接输入的方式来改变值,而且还可以下拉出来一个控件,用户可以在这个控件上根据鼠标的位置来确定具体的值。下面具体的代码:
private system.drawing.point _dropui;
 
 [categoryattribute("自定义编辑器"),
 defaultvalueattribute("1"),
 descriptionattribute("下拉可视控件"),
 readonlyattribute(false),
 editorattribute(typeof(dropeditor),typeof(system.drawing.design.uitypeeditor))]
 public system.drawing.point dropui
 {
 get { return this._dropui;}
 set { this._dropui=value; }
 }
 
 
 
 public class dropeditor:system.drawing.design.uitypeeditor
 {
 
 public override system.drawing.design.uitypeeditoreditstyle geteditstyle(system.componentmodel.itypedescriptorcontext context)
 {
 return system.drawing.design.uitypeeditoreditstyle.dropdown;
 }
 
 public override object editvalue(system.componentmodel.itypedescriptorcontext context, system.iserviceprovider provider, object value)
 {
 system.windows.forms.design.iwindowsformseditorservice iws=(system.windows.forms.design.iwindowsformseditorservice)provider.getservice(typeof(system.windows.forms.design.iwindowsformseditorservice));
 if (iws!=null)
 {
 propertygridapp.dropuicontrol uicontrol=new propertygridapp.dropuicontrol((system.drawing.point)value,iws);
 iws.dropdowncontrol(uicontrol);
 return uicontrol.value;
 }
 return value;
 }
 }
 
 internal class dropuicontrol:system.windows.forms.usercontrol
 {
 public dropuicontrol(system.drawing.point avalue,system.windows.forms.design.iwindowsformseditorservice iws)
 {
 this.value=avalue;
 this._tmpvalue=avalue;
 this._iws=iws;
 this.setstyle(system.windows.forms.controlstyles.doublebuffer|system.windows.forms.controlstyles.userpaint|system.windows.forms.controlstyles.allpaintinginwmpaint,true);
 this.backcolor=system.drawing.systemcolors.control;
 }
 
 private system.drawing.point _value;
 public system.drawing.point value
 {
 get { return this._value;}
 set { this._value=value; }
 }
 
 private system.drawing.point _tmpvalue;
 private system.windows.forms.design.iwindowsformseditorservice _iws;
 
 protected override void onpaint(system.windows.forms.painteventargs e)
 {
 string str="x:"+this._tmpvalue.x.tostring()+" ;y:"+this._tmpvalue.y.tostring();
 system.drawing.graphics g=e.graphics;
 system.drawing.sizef sizef= g.measurestring(str,this.font);
 g.drawstring(str,
 this.font,
 new system.drawing.solidbrush(system.drawing.color.black),
 (int)((this.width-(int)sizef.width)/2),
 this.height-(int)sizef.height);
 g.pageunit=system.drawing.graphicsunit.pixel;
 g.fillellipse(new system.drawing.solidbrush(system.drawing.color.red),
 this.value.x-2,
 this.value.y-2,
 4,
 4);
 }
 
 protected override void onmousemove(system.windows.forms.mouseeventargs e)
 {
 base.onmousemove(e);
 this._tmpvalue=new system.drawing.point(e.x,e.y);
 this.invalidate();
 }
 
 protected override void onmouseup(system.windows.forms.mouseeventargs e)
 {
 base.onmouseup(e);
 this.value=this._tmpvalue;
 this.invalidate();
 if (e.button==system.windows.forms.mousebuttons.left)
 this._iws.closedropdown();
 
 }
 }
 
以上的代码度非常的简单,相信大家一定能够看懂,如果有不明白的地方看看帮助,那里面解释的非常的清楚。
在编写属性编辑器中我们都需要覆盖其中的editvalue这个方法,大家是否注意到了其中的object value这个参数?其实这个参数就是已经装箱的属性值,在我们自定义的处理完这个值的时候同样可以返回一个装箱的值来确定已经修改的属性。在上面的两个例子中我们只是简单的使用了这个值,大家理解这个内容之后就可以做出更加个性化的编辑器了。