2012-10-10 65 views
1

我們有一個簡單的用戶控件,在我們的表單中使用它時可以正常工作,但是當我們嘗試通過此方法將其託管到StatusStrip中時,從不會調用OnPaint事件。 MSDN文檔指出這「應該」的工作,但我們什麼也看不到,確認OnPaint事件不會被調用:託管UserControl的StatusStrip無法調用UserControls 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); 
} 

回答

2

的ToolStripControlHost有一個奇怪的怪癖它需要設置的minimumSize它承載控制:

嘗試添加此:

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

得分。只是我喜歡SO的很多原因之一。我必須等待檢查,因爲現在有一段時間才能接受...... – Gio

1

我有他同樣的問題。我的解決辦法是從ToolStripControlHost作出新classinherited和覆蓋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(); 
     } 
    } 
} 

在公共Form1中()

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