自定義組件之屬性(Property)的性質(Attribute)說明(4)
發表時間:2024-06-16 來源:明輝站整理相關軟件相關文章人氣:
[摘要]二:UI屬性編輯器(UITypeEditor)這里的屬性編輯器的意思是能夠實現上面提到的彈出對話框和下拉UI的形式。廢話不說下面我們一一介紹。1、 彈出對話框的形式在本例中我使用了string類型的屬性來顯示版本的信息,大家可以隨便的寫各類的屬性,這里只需要指定改屬性的編輯器就可以了。首先我們要...
二: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這個參數?其實這個參數就是已經裝箱的屬性值,在我們自定義的處理完這個值的時候同樣可以返回一個裝箱的值來確定已經修改的屬性。在上面的兩個例子中我們只是簡單的使用了這個值,大家理解這個內容之后就可以做出更加個性化的編輯器了。