colortypeeditor.cs

来自「ASP.NET服务器控件高级编程电子书」· CS 代码 · 共 70 行

CS
70
字号
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Web.UI.WebControls;
using System.Web.UI.Design.WebControls;
using System.Windows.Forms.Design;

namespace WroxDesign.Design
{
	public class ColorTypeEditor : UITypeEditor
	{
		public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
		{
			return UITypeEditorEditStyle.DropDown;
		}


		public override bool GetPaintValueSupported(ITypeDescriptorContext context)
		{
			return true;
		}

		/// <summary>
		/// Paint your custom drawing in the output Graphics object recieved.
		/// </summary>
		public override void PaintValue(PaintValueEventArgs e)
		{
			WebControl control = e.Context.Instance as WebControl;
			
			//Fills the left rectangle with a color.
			e.Graphics.FillRegion(new SolidBrush(control.BackColor), new Region(e.Bounds));
			//Draw a couple letters with the selected colorss
			e.Graphics.DrawString("ab", new Font("Tahoma", 8, 
				FontStyle.Regular |	(control.Font.Bold ? FontStyle.Bold : FontStyle.Regular) |
                (control.Font.Italic ? FontStyle.Italic : FontStyle.Regular) |
				(control.Font.Underline ? FontStyle.Underline : FontStyle.Regular)),
				new SolidBrush(control.ForeColor), e.Bounds);
		}

		/// <summary>
		/// Called upon edition of the property.
		/// </summary>
		public override object EditValue(ITypeDescriptorContext context, 
			IServiceProvider provider, object value)
		{
			object retvalue = value;
			IWindowsFormsEditorService srv = null;
			//IToolboxService tool;
			//System.ComponentModel.Design.ISelectionService sel;
			
			//Get the editor service from the provider, to perform the dropdown.
			if (provider != null)
				srv = (IWindowsFormsEditorService)
					provider.GetService(typeof(IWindowsFormsEditorService));

			if (srv != null)
			{
				ColorTypeEditorControl editor = 
					new ColorTypeEditorControl((System.Drawing.Color)value, 
					context.Instance as WebControl);
				srv.DropDownControl(editor);
				return editor.SelectedColor;
			}

			return retvalue;
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?