自定義組件之屬性(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這個參數?其實這個參數就是已經裝箱的屬性值,在我們自定義的處理完這個值的時候同樣可以返回一個裝箱的值來確定已經修改的屬性。在上面的兩個例子中我們只是簡單的使用了這個值,大家理解這個內容之后就可以做出更加個性化的編輯器了。