我们有一个简单的用户控件,当我们在表单中使用它时可以正常工作,但是当我们尝试通过此方法将它托管到我们的 StatusStrip 中时,永远不会调用 OnPaint 事件。MSDN 文档声明这“应该”工作,但我们什么也没看到,并确认 OnPaint 事件永远不会被调用。:
public partial class SimpleUserControl : UserControl
{
public SimpleUserControl( )
{
InitializeComponent( );
// Set the styles for drawing
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer |
ControlStyles.SupportsTransparentBackColor,
true);
}
[System.ComponentModel.EditorBrowsableAttribute( )]
protected override void OnPaint( PaintEventArgs e )
{
Rectangle _rc = new Rectangle( 0, 0, this.Width, this.Height );
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawArc( new Pen( Color.Red ), _rc, 180, 180 );
}
}
public Form1( )
{
InitializeComponent( );
SimpleUserControl suc = new SimpleUserControl( );
suc.Size = new Size( 30, 20 );
ToolStripControlHost tsch = new ToolStripControlHost( suc );
statusStrip1.Items.Add(tsch);
}