加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Invalidate()和Refresh()都调用OnPaint()

发布时间:2020-12-15 06:55:02 所属栏目:百科 来源:网络整理
导读:我试图从第1行到第2行到下面的代码中: using System; using System.Windows.Forms; namespace MyNameSpace { internal class MyTextBox : System.Windows.Forms.TextBox { protected override void OnEnabledChanged(EventArgs e) { base.OnEnabledChanged(
我试图从第1行到第2行到下面的代码中:
using System;  
using System.Windows.Forms;  

namespace MyNameSpace  
{  
    internal class MyTextBox : System.Windows.Forms.TextBox  
    {  
        protected override void OnEnabledChanged(EventArgs e)  
        {  
            base.OnEnabledChanged(e);  
            Invalidate(); // Line #1 - can get here  
            Refresh();  
        }

       protected override void OnPaint(PaintEventArgs e)  
       {
            base.OnPaint(e);   
            System.Diagnostics.Debugger.Break(); // Line #2 - can't get here  
       }  
    }  
}

然而,似乎neiter Invalidate()和Refresh()会导致OnPaint(PaintEventArgs e)被调用.两个问题:

>为什么不起作用?
>如果它不能被修复:我只想调用OnPaint(PaintEventArgs e)为了访问e.Graphics对象 – 有没有其他的方法来做到这一点?

解决方法

要覆盖控件的绘图,您必须将样式设置为UserPaint,如下所示:
this.SetStyle(ControlStyles.UserPaint,true);

有关详细信息,请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx

UserPaint If true,the control paints itself rather than the operating system doing so. If false,the Paint event is not raised. This style only applies to classes derived from Control.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读