微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图从第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.

原文地址:https://www.jb51.cc/csharp/94222.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐