We have a simple user control which works fine when we use it in our form, however when we try to host it into our StatusStrip via this method the OnPaint event is never called. MSDN Documentation states this 'should' work, but we see nothing and confirmed that the OnPaint event never gets called.:

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);
}
有帮助吗?

解决方案

The ToolStripControlHost has an odd quirk where it needs the MinimumSize set for the control it is hosting:

Try adding this:

suc.Size = new Size(30, 20);
suc.MinimumSize = suc.Size;

其他提示

I had he same problem. My solution was to make a new classinherited from ToolStripControlHost and override ToolStripItem.OnPaint Method, like this:

[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();
        }
    }
}

In public form1()

public Form1( )
{
     InitializeComponent( );

     ToolStripSimpleUserControl suc = new ToolStripSimpleUserControl( );
     suc.StripSimpleUserControl.Size = new Size( 30, 20 );

     statusStrip1.Items.Add(suc);
}

For more rules visit http://msdn.microsoft.com/en-us/library/9k5etstz.aspx

and visit http://tonyfear.netau.net/index.php?option=com_content&view=category&layout=blog&id=3&limitstart=5

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top