我们有一个简单的用户控件,当我们在表单中使用它时工作正常,但是当我们尝试通过此方法将其托管到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);
}
答案 0 :(得分:2)
ToolStripControlHost有一个奇怪的怪癖,它需要为它所托管的控件设置MinimumSize:
尝试添加此内容:
suc.Size = new Size(30, 20);
suc.MinimumSize = suc.Size;
答案 1 :(得分:1)
我有同样的问题。我的解决方案是从ToolStripControlHost创建一个新类,并覆盖ToolStripItem.OnPaint方法,如下所示:
[System.ComponentModel.DesignerCategory("code")]
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.All]
public class ToolStripSimpleUserControl : ToolStripControlHost
{
public ToolStripSimpleUserControl ()
: base(new SimpleUserControl())
{
}
public SimpleUserControl StripSimpleUserControl
{
get { return Control as SimpleUserControl; }
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// not thread safe!
if (e != null)
{
this.StripSimpleUserControl.Invalidate();
}
}
}
在公共表单1()
public Form1( )
{
InitializeComponent( );
ToolStripSimpleUserControl suc = new ToolStripSimpleUserControl( );
suc.StripSimpleUserControl.Size = new Size( 30, 20 );
statusStrip1.Items.Add(suc);
}
了解更多规则 访问http://msdn.microsoft.com/en-us/library/9k5etstz.aspx
和 访问http://tonyfear.netau.net/index.php?option=com_content&view=category&layout=blog&id=3&limitstart=5