首先声明下面代码只能用于桌面_NET,不适用于NETCF。
另外,内容很菜鸟,因为本人04年出道,自认仍然是菜鸟,老鸟略过。
控件写了不少,底层的上层的都有,但是一直都是只关注运行时效果,对新版设计时技术关注不多,习惯还停留于VS03的[CategoryAttribute("Behavior")]时代,今天花点时间写下,主要是给我同样的菜鸟们做做笔记。
不少源码都抄自MSDN,先坦白。。。
功能说明:只是一个相应鼠标操作的控件,设计时鼠标经过的时候,在控件上绘制一个带颜色的空心框。不同的只是设计时语法(VS05支持)。
Code
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
namespace ControlDesignerExample
{
// ExampleControlDesigner is an example control designer that
// demonstrates basic functions of a ControlDesigner.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public class ExampleControlDesigner : System.Windows.Forms.Design.ControlDesigner
{
Code
// This boolean state reflects whether the mouse is over the control.
private bool mouseover = false;
// This color is a private field for the OutlineColor property.
private Color lineColor = Color.White;
// This color is used to outline the control when the mouse is
// over the control.
public Color OutlineColor
{
get
{
return lineColor;
}
set
{
lineColor = value;
}
}
public ExampleControlDesigner()
{
}
// Sets a value and refreshes the control's display when the
// mouse position enters the area of the control.
protected override void OnMouseEnter()
{
this.mouseover = true;
this.Control.Refresh();
}
// Sets a value and refreshes the control's display when the
// mouse position enters the area of the control.
protected override void OnMouseLeave()
{
this.mouseover = false;
this.Control.Refresh();
}
// Draws an outline around the control when the mouse is
// over the control.
protected override void OnPaintAdornments(System.Windows.Forms.PaintEventArgs pe)
{
if(this.mouseover)
pe.Graphics.DrawRectangle(new Pen(new SolidBrush(this.lineColor), 6), 0, 0, this.Control.Size.Width, this.Control.Size.Height);
}
// Adds a property to this designer's control at design time
// that indicates the outline color to use.
protected override void PreFilterProperties(System.Collections.IDictionary properties)
{
properties.Add("OutlineColor", TypeDescriptor.CreateProperty(typeof(ExampleControlDesigner), "OutlineColor", typeof(System.Drawing.Color), null));
}
}
// This example control demonstrates the ExampleControlDesigner.
[DesignerAttribute(typeof(ExampleControlDesigner))]
public class ExampleControl : System.Windows.Forms.UserControl
{
Code
private System.ComponentModel.Container components = null;
public ExampleControl()
{
components = new System.ComponentModel.Container();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
}
}
以上源码在VS08中以_NET2.0实现。