1
我創建了一個從Panel派生的自定義控件。我用它來顯示使用BackgroundImage屬性的圖像。我重寫OnClick方法並將isSelected設置爲true,然後調用Invalidate方法,並在OverPover的OnPaint中繪製一個矩形。 一切都很好,直到我將DoubleBuffered設置爲true。矩形被繪製,然後它被刪除,我無法得到爲什麼會發生這種情況。DoubleBuffered設置爲true時覆蓋OnPaint時出現問題
public CustomControl()
: base()
{
base.DoubleBuffered = true;
base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw, true);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
PaintSelection();
}
private void PaintSelection()
{
if (isSelected)
{
Graphics graphics = CreateGraphics();
graphics.DrawRectangle(SelectionPen, DisplayRectangle.Left, DisplayRectangle.Top, DisplayRectangle.Width - 1, DisplayRectangle.Height - 1);
}
}
我知道這是容易的,謝謝! – 2010-07-15 12:15:03