没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
翻译|其它|编辑:郝浩|2004-03-16 09:41:00.000|阅读 1839 次
概述:
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
二: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这个参数?其实这个参数就是已经装箱的属性值,在我们自定义的处理完这个值的时候同样可以返回一个装箱的值来确定已经修改的属性。在上面的两个例子中我们只是简单的使用了这个值,大家理解这个内容之后就可以做出更加个性化的编辑器了。
尾声:
在.Net FrameWork中还有许多的Attribute,由于小弟知道的有限,所以只能给大家介绍这些,而且在这期间也避免不了有错误的地方希望大家能够原谅。小弟在此仅希望能够起到一个抛砖引玉的作用我就能够满足了,小弟非常感谢各位能够看完我写的这些东西。谢谢大家。
希望大家常联系:
E-mail:
QQ:20954664
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@evget.com
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
慧都科技 版权所有 Copyright 2003-
2025 渝ICP备12000582号-13 渝公网安备
50010702500608号