[英]OnPaint override is never called
我已經在這幾天了,這讓我很生氣。 我有一個繼承自System.Windows.Forms.Panel的控件,我試圖覆蓋OnPaint。 它很簡單,直截了當地發出它。
public class CollapsiblePanel : System.Windows.Forms.Panel
{
public CollapsiblePanel()
{
//
// Required for the Windows Form Designer
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
SetStyle
(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint | ControlStyles.DoubleBuffer |
ControlStyles.ResizeRedraw | ControlStyles.Selectable ,
true
);
}
protected override void OnPaint(PaintEventArgs e)
{
// This never runs no matter what I try!
base.OnPaint(e);
}
}
當我嘗試覆蓋OnPaint時,我對ProgressBar也有同樣的問題。它從未被調用過。
我在這里找到了解決方案: http : //web.archive.org/web/20140214234801/http : //osix.net/modules/article/?id = 826
您必須創建一個構造函數並啟用用戶繪制,如下所示:
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
默認值可能因框架版本和操作系統而異。
如果要調用OnPaint
,請調用this.Invalidate()
我有一個問題,就是沒有看到它有時會運行。 但這個工程就在Windows 7 64位系統,.NET框架4.0。 剛剛將它添加到表單中。
class Class1 : Panel
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Console.WriteLine("hello world, I'm painting myself");
// Process.Start("notepad.exe");
}
}
如果您看不到控制台輸出,請嘗試Process.Start(...)
行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.