C#Invalidate不调用paint方法 [英] C# Invalidate is not calling paint method
问题描述
我有OnPaint方法overrided到在屏幕上绘制一个椭圆。
I have OnPaint method overrided to draw an Ellipse on the screen.
protected override void OnPaint(PaintEventArgs e)
{
MessageBox.Show("Paint");
if (debugStarted)
{
int y = rtbLogicCode.PlaceToPoint(new Place(0, debugLine)).Y;
if (rtbLogicCode.GetVisibleState(debugLine).ToString() == "Visible")
{
e.Graphics.FillEllipse(new LinearGradientBrush(new Rectangle(0, y, 15, 15), Color.LightPink, Color.Red, 45), 0, y, 15, 15);
}
base.OnPaint(e);
}
}
private void rtbLogicCode_Scroll(object sender, ScrollEventArgs e)
{
this.Invalidate();
}
scroll事件(在RichTextBox中)得到妥善处理,但即使我无效,这不是调用的OnPaint函数(MessageBox中未显示)的形式。
The scroll event (On the Richtextbox) is handled properly but even though I am invalidating the form, it isn't calling the OnPaint function (The messagebox isn't showing).
可能是什么的这个可能的原因?
What could be the possible cause of this?
编辑:我忘了提,在子窗体(添加为使用MDI财产的主要形式的控制),我的初始化函数,我设置以下样式:
I have forgot to mention that on my initialization function of the child form (added as a control of the main form using MDI property), I set the following styles:
private void LogicCodeInit()
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
}
EDIT2:我也忘了提到子窗体添加为的TabControl的控制。在的TabControl然后加入为主要形式的控制。
I've also forgot to mention that the child form is added as a control of TabControl. The TabControl then is added as a control of the main form.
推荐答案
呼叫更新
在的Invalidate
。 的Invalidate
重绘,只有当它具有焦点,它可能没有得到焦点,因为它被添加为的TabControl
子的形式。
Call Update
after Invalidate
. Invalidate
repaints the form only if it has focus, it's probably not getting focus since it's being added as a TabControl
child.
文档:
调用Invalidate方法不会强制同步漆;强制同步油漆,拨打调用Invalidate方法后Update方法。当这个方法被调用不带参数,整个客户区被添加到更新区域。
Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method. When this method is called with no parameters, the entire client area is added to the update region.
这篇关于C#Invalidate不调用paint方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!