【发布时间】:2016-05-31 18:20:54
【问题描述】:
我创建了一个派生自 Panel 的自定义控件。 一切正常,除了在设计器中我的控件只有在失去焦点时才会重绘。 我错过了什么?
这里是CustomControl.cs的代码
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Picwing
{
public partial class xPanel : Panel
{
private SizeF textSize;
public xPanel() {
InitializeComponent();
}
[Browsable(true)]
public override string Text {
get { return base.Text; }
set { base.Text = value; }
}
protected override void OnPaint(PaintEventArgs pe) {
base.OnPaint(pe);
textSize = pe.Graphics.MeasureString(Text, Font);
pe.Graphics.DrawRectangle(new Pen(ForeColor, 1), pe.ClipRectangle.X, pe.ClipRectangle.Y + textSize.Height / 2, pe.ClipRectangle.Width - 1, pe.ClipRectangle.Height - textSize.Height / 2 - 1);
pe.Graphics.FillRectangle(new SolidBrush(BackColor), 5, 0, textSize.Width, textSize.Height);
pe.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), pe.ClipRectangle.X + 6, pe.ClipRectangle.Y);
}
}
}
【问题讨论】:
-
你能打印屏幕吗?
-
您忘记发布您的控件的代码。
-
您是否使用 '+=" 注册了 OnPaint 事件?
-
不要使用
pe.ClipRectangle。请根据您的要求使用DisplayRectangle或ClientRactangle。