我试图从以下代码中的第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
}
}
}
要覆盖控件的绘图,必须将样式设置为UserPaint,如下所示:
this.SetStyle(ControlStyles.UserPaint, true);
有关更多信息,请参阅此
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx
UserPaint如果为true,则控件绘制
本身而不是经营
系统这样做。如果为false,则为Paint
事件没有提出。这种风格而已
适用于派生自的类
控制。
要覆盖控件的绘图,必须将样式设置为UserPaint,如下所示:
this.SetStyle(ControlStyles.UserPaint, true);
有关更多信息,请参阅此
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.setstyle.aspx
UserPaint如果为true,则控件绘制
本身而不是经营
系统这样做。如果为false,则为Paint
事件没有提出。这种风格而已
适用于派生自的类
控制。
编辑:在阅读克里斯的评论后,我同意你可能不应该使用这个。
要回答问题的其他部分,您可以使用以下命令获取任意控件的图形对象:
Graphics g = panel1.CreateGraphics();
但是,当你这样做时,你也有责任清理它,所以正确的形式是:
using (Graphics g = this.CreateGraphics())
{
// all your drawing here
}
internal class MyTextBox : System.Windows.Forms.TextBox
{
public MyTextBox()
{
this.SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}