gpt4 book ai didi

C# Invalidate 不调用 paint 方法

转载 作者:太空狗 更新时间:2023-10-29 21:05:54 31 4
gpt4 key购买 nike

我重写了 OnPaint 方法以在屏幕上绘制一个椭圆。

    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();
}

滚动事件(在 Richtextbox 上)得到了正确处理,但即使我使表单无效,它也没有调用 OnPaint 函数(消息框未显示)。

这可能是什么原因?

编辑:我忘了提到在子窗体的初始化函数(使用 MDI 属性作为主窗体的控件添加)中,我设置了以下样式:

 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 添加为主窗体的控件。

最佳答案

Invalidate 之后调用 UpdateInvalidate 仅在表单具有焦点时才重新绘制表单,它可能没有获得焦点,因为它被添加为 TabControl 子项。

来自 MSDN documentation :

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 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11389850/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com