我有一個用戶控件和自定義繪畫。從我所知道的構造函數中正確設置樣式。基本代碼:如果窗體沒有焦點,OnPaint沒有被調用
public partial class LineChart2 : UserControl
{
public LineChart2()
{
InitializeComponent();
//Set control styles to eliminate flicker on redraw and to redraw on resize
this.SetStyle(
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer,
true);
SetDefaultValues();
}
protected override void OnPaint(PaintEventArgs e)
{
// breakpoint set here for verification
Paint~misc stuff(e.Graphics);
base.OnPaint(e);
}
private void UpdateGraph()
{
// this is called when the data that the control depends on changes
~update stuff();
this.Invalidate();
//this.Refresh();
}
}
控制被包含在標準的WinForm一個面板內。
我試過Invalidate和Refresh。
使用Invalidate()時,只要包含的窗體具有焦點,控件就會正確重繪。繪圖很流暢。當我將焦點切換到另一個表單時,即使事件仍在觸發,繪圖也會停止,並且this.Invalidate()仍然被調用。該表格在屏幕上仍然完全可見。
使用Refresh()時,無論表單是否具有焦點,控件都會重繪,但繪圖不斷閃爍,就好像繞過雙緩衝機制一樣。
那麼,無論焦點如何,我如何獲取Invalidate消息以正確調用OnPaint方法?
調用更新後無效使其行爲就像調用刷新 - 常量閃爍。 – dsmith 2011-05-27 22:00:44
使用這也意味着我不能捕捉事件像鼠標事件,因爲(我認爲)同步調用搶佔它們(圖形每秒更新10-20次,並且控件的鼠標操作剛剛完成如果使用更新,則不起作用)。 – dsmith 2011-05-27 22:16:14
已經發現閃爍的問題。這是因爲每次刷新都有兩次調用Invalidate,一次是清除舊數據,另一次是增加新數據.Update()顯然會迫使這兩次繪製發生,而不是像正常事件隊列那樣將它們摺疊爲一次調用。調整,以便只有一個Invalidate調用修復了事件,並且鼠標事件是可管理的(儘管不像我想的那樣流暢)。我對事件隊列的非處理仍然不滿意,但現在它運行良好。標記已被接受。 – dsmith 2011-06-07 21:17:40