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