如果表单没有焦点,则不会调用 OnPaint [英] OnPaint not getting called if form doesn't have focus
问题描述
我有一个自定义绘画的用户控件.据我所知,构造函数正确设置了样式.基本代码:
I have a user control with custom painting. Constructor sets styles correctly, from what I can tell. Basic code:
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 上的面板中.
The control is contained within a Panel on a standard WinForm.
无效和刷新我都试过了.
I've tried both Invalidate and Refresh.
当使用 Invalidate() 时,只要控件包含的表单具有焦点,控件就会正确重绘.绘图很流畅.当我将焦点切换到另一种形式时,即使事件仍在触发,绘图也会停止,并且 this.Invalidate() 仍在被调用.该表单仍然在屏幕上完全可见.
When using Invalidate(), the control will redraw properly as long as the form it is contained in has focus. Drawing is smooth. When I switch focus to another form, drawing ceases even though the events are still firing, and this.Invalidate() is still being called. The form is still fully visible on screen.
使用Refresh()时,无论窗体是否有焦点,控件都会重绘,但绘图不断闪烁,好像绕过了双缓冲机制.
When using Refresh(), the control will redraw regardless of whether the form has focus, but the drawing constantly flickers, as if bypassing the double-buffering mechanism.
那么如何获取 Invalidate 消息以正确调用 OnPaint 方法而不管焦点如何?
So how do I get the Invalidate message to properly invoke the OnPaint method regardless of focus?
推荐答案
文档 说:
调用 Invalidate 方法不会强制同步绘制;强迫一个同步绘制,调用更新调用 Invalidate 后的方法方法.
Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method.
您是否尝试过在 Invalidate
之后调用 Update
?
Have you tried calling Update
after Invalidate
?
这篇关于如果表单没有焦点,则不会调用 OnPaint的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!