代码如下:将以下代码用类库包装成DLL,然后加载到工具箱中,拖出来用即可。
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
{
// 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
{
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);
}
}
}
提示包错信息:“ControlDesignerExample.ExampleControl”并不包含“OutlineColor”的定义
不是很理解,这是MSDN上面的代码。MSDN连接路径:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref18/html/T_System_Windows_Forms_Design_ControlDesigner.htm无意中看到的例子,还是不太理解ControlDesigner类的作用,熟悉的人请讲一下这个类的作用吧,怎么用的。先谢谢。
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
{
// 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
{
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);
}
}
}
提示包错信息:“ControlDesignerExample.ExampleControl”并不包含“OutlineColor”的定义
不是很理解,这是MSDN上面的代码。MSDN连接路径:ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/CPref18/html/T_System_Windows_Forms_Design_ControlDesigner.htm无意中看到的例子,还是不太理解ControlDesigner类的作用,熟悉的人请讲一下这个类的作用吧,怎么用的。先谢谢。
这个怎么回事,上面代码是从MSDN上考下来的,MSDN上说这样就可以了,但还是不行,我就不明白了。
步骤如下:
1.新建“Windows窗体控件库”项目,默认项目名称
2.删除UserControl1.Desinger.cs中的所有代码
3.将UserControl1.cs中的代码全部替换成MSDN中复制来的代码
4.添加对System.Design.dll的引用,这一步很重要,否则无法编译
5.Build
6.新建Winform项目
7.将此控件加入工具箱
8.将此控件拖放到窗体中,在设计时当鼠标进入控件范围,控件的四周会画上框
9.可设置控件的OutlineColor,改变画的边框的颜色。