自定义控件
不让控件的public属性显示在属性窗口,public属性自动显示
[System.ComponentModel.Browsable(false)]
public int StockNumber
{
//.....
}
继承控件
public class myButton:System.Windows.Forms.Button
{
//....
}
添加新功能,例
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (char.IsNumber(e.KeyChar) == false)
{
e.Handled = true;
}
}
改变外观
protected override void OnPaint(PaintEventArgs pe)
{
System.Drawing.Drawing2D.GraphicsPath myPath = new System.Drawing.Drawing2D.GraphicsPath();
myPath.AddString("Wow",Font.FontFamily,(int)Font.Style,72,new PointF(0,0),StringFormat.GenericDefault);
Region myRegion = new Region(myPath);
this.Region = myRegion;
}
UserControl设计器
组合一个或多个控件
OnPaint事件 PaintEventArgs参数有属性:Graphics ClipRectangle
重新绘制控件,ClipRectangle仅表示需要重新绘制的区域
要使控件在每次调整大小时都重新绘制,标志:
SetStyle(ControlStyles.ResizeRedraw,true);
重绘控件 Refresh()