C# Tapcontrol中的自定义控件渲染
这是一个奇怪的例子,描述起来会有点长,所以请耐心听我说 我有一个为C4客户端配置文件制作的控件,名为ToggleSwitchTSW。这个开关的设计就像地铁开关一样,这是做得很出色。。。直到它被添加到点击控制/点击页面,然后: 渲染的行为很奇怪,没有正确绘制背景,并且开/关标签没有显示。这也会影响其他控件,就像整个窗体不会绘制一样 此外,VS 2010和2013 design Editor属性字段都冻结,因此我无法访问其他控件 如果保持正常,程序将无法生成。。。基本上崩溃了 但是,如果我将点击控制从“法线”更改为“flatbutton”或“button”,则它会停止并像往常一样平滑运行,这很好,但是我开始想为什么它不会以法线渲染 这就是我尝试过的 tsw扩展按钮库以实现可点击性。所以我将其更改为控制,然后在一个无限循环中调用OnPaint,这样做没有帮助,单击也不起作用,但这现在不那么重要了 我打开了按钮,没有任何帮助,仍然很奇怪 重写button和buttonbase中的每个方法,看看这是否仍然没有任何帮助 查看表单设计器后,我发现唯一更改的属性是UseVisualStyleBackColor当为false时有效,当为true时无效,因此我做了一个覆盖,强制它为false,但仍然无效:/ 我甚至试图删除partial关键字 我花了半天的时间阅读关于点击和按钮控制的解除朋友关系的方法 甚至找到了一个自定义控件测试按钮的扩展,它工作得很好,但没有理由这样做,因为它与我看到的唯一不同之处是OnPaint中关于某些渲染的if语句 。。。。什么都不管用 以下是切换开关的整体:C# Tapcontrol中的自定义控件渲染,c#,rendering,custom-controls,C#,Rendering,Custom Controls,这是一个奇怪的例子,描述起来会有点长,所以请耐心听我说 我有一个为C4客户端配置文件制作的控件,名为ToggleSwitchTSW。这个开关的设计就像地铁开关一样,这是做得很出色。。。直到它被添加到点击控制/点击页面,然后: 渲染的行为很奇怪,没有正确绘制背景,并且开/关标签没有显示。这也会影响其他控件,就像整个窗体不会绘制一样 此外,VS 2010和2013 design Editor属性字段都冻结,因此我无法访问其他控件 如果保持正常,程序将无法生成。。。基本上崩溃了 但是,如果我将点击控制
public partial class ToggleSwith : ButtonBase
{
object _Lock = new object();
private Color _OffColor = SystemColors.ControlDarkDark;
private Color _OnColor = Color.LimeGreen;
private bool _ColorText = false;
// private bool _BeenDrawn = false;
private bool _Checked = false;
[Browsable(false)]
public override string Text { get { return null; } }
[Browsable(false)]
public override bool AutoSize{get{return false;}}
[Browsable(false)]
public override ContentAlignment TextAlign { get { return ContentAlignment.MiddleLeft; } }
[Category("Appearance")]
public virtual Color OffColor { get { return _OffColor; } set { _OffColor = value; Invalidate(); } }
[Category("Appearance")]
public virtual Color OnColor { get { return _OnColor; } set { _OnColor = value; Invalidate(); } }
[Category("Appearance")]
public virtual bool ColorText { get { return _ColorText; } set { _ColorText = value; Invalidate(); } }
[Category("Appearance")]
public virtual bool Checked { get { return _Checked; } set { _Checked = value; Invalidate(); } }
new public bool UseVisualStyleBackColor { get { return false; } set{} }
protected override Size DefaultSize { get { return new Size(80, 18); } }
protected override Size DefaultMinimumSize { get { return new Size(40, 13); } }
public event EventHandler ToggleChanged;
public ToggleSwith()
{
this.Size = new Size(80, 18);
InitializeComponent();
this.Click += ToggleSwith_Click;
}
void ToggleSwith_Click(object sender, EventArgs e)
{
lock (_Lock)
{
if (_Checked) _Checked = false;
else _Checked = true;
}
OnToggleChanged();
}
private void OnToggleChanged()
{
if (ToggleChanged != null)
{
EventArgs ea = new EventArgs();
ToggleChanged(this, ea);
}
}
protected override void OnPaint(PaintEventArgs e)
{
this.Controls.Clear();
int toggleblocsize=(int)this.Size.Width/8;
int indent = 23;
int labelx = 2;
// Declare and instantiate a new pen.
SolidBrush OffPen = new SolidBrush(this.OffColor);
SolidBrush OnPen = new SolidBrush(this.OnColor);
SolidBrush BackgroundPen = new SolidBrush(this.BackColor);
SolidBrush BlackPen = new SolidBrush(SystemColors.ControlText);
Pen ControlDarkPen = new Pen(SystemColors.ControlDark);
Pen ControlPen = new Pen(SystemColors.Control);
e.Graphics.FillRectangle(BackgroundPen,0, 0, this.Size.Width,this.Size.Height);
e.Graphics.DrawRectangle(ControlDarkPen, indent, -0, this.Size.Width - (indent+1), this.Size.Height-1);
Label l = new Label();
l.Font = this.Font;
l.Size = new Size((indent-labelx),this.Size.Height);
// if(this.Size.Height <13)l.Location= new Point(labelx,-1);
// else l.Location = new Point(labelx, 0);
l.TextAlign = TextAlign;
l.ForeColor = this.ForeColor;
if (this._Checked)
{
//ligth
e.Graphics.FillRectangle(OnPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, this.Size.Width - (toggleblocsize+1), -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(BlackPen, this.Size.Width - (toggleblocsize), -1, this.Size.Width, this.Size.Height + 1);
if (ColorText) l.ForeColor = OnColor;
l.Text = "On";
}
else
{
//ligth
e.Graphics.FillRectangle(OffPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, indent , -0, (toggleblocsize + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(BlackPen, (indent+1) , -1,(toggleblocsize), this.Size.Height + 1);
if (ColorText) l.ForeColor = this.ForeColor;
l.Text = "Off";
}
this.Controls.Add(l);
}
public override Size GetPreferredSize(Size proposedSize)
{
return new Size(80, 18);
}
}
这并不是说我不能生活在一个平面的tapcontrol它只是为什么它不工作,困扰我
如有任何帮助,将不胜感激:
最后,一些问题的可视化文档:
以下是它应该如何工作:
这是一个正常的水龙头
把它修好了,现在可以用了,结果是出了点问题 第一个基类是错误的,它应该是一个控件
public class ToggleSwith : Control
油漆上的事件改成了这个
protected override void OnPaint(PaintEventArgs e)
{
//this.Controls.Clear();
int toggleblocsize = (int)this.Size.Width / 6;
int indent = 23;
int labelx = 2;
// Declare and instantiate a new pen.
SolidBrush OffPen = new SolidBrush(this.OffColor);
SolidBrush OnPen = new SolidBrush(this.OnColor);
Rectangle texttangle = new Rectangle(new Point(labelx, 0), new Size(indent - labelx, this.Size.Height));
SolidBrush BackgroundBrush = new SolidBrush(this.Parent.BackColor);
SolidBrush ToggleBrush = new SolidBrush(SystemColors.ControlText);
SolidBrush TextBrush = new SolidBrush(SystemColors.ControlText);
Pen ControlDarkPen = new Pen(SystemColors.ControlDark);
Pen ControlPen = new Pen(SystemColors.Control);
e.Graphics.FillRectangle(BackgroundBrush, 0, 0, this.Size.Width, this.Size.Height);
e.Graphics.FillRectangle(new SolidBrush(BackColor), indent, 0, this.Size.Width - indent, this.Size.Height);
e.Graphics.DrawRectangle(ControlDarkPen, indent, -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
if (!this.Enabled)
{
OffPen = new SolidBrush(SystemColors.Control);
OnPen = new SolidBrush(SystemColors.Control);
ToggleBrush = new SolidBrush(SystemColors.ControlDark);
TextBrush = new SolidBrush(SystemColors.Control);
}
if (this._Checked)
{
//ligth
e.Graphics.FillRectangle(OnPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, this.Size.Width - (toggleblocsize + 1), -0, this.Size.Width - (indent + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(ToggleBrush, this.Size.Width - (toggleblocsize), -1, this.Size.Width, this.Size.Height + 1);
if (ColorText && this.Enabled) TextBrush = new SolidBrush(OnColor);//_label.ForeColor = OnColor;
// _label.Text = "On";
// Draw string to screen.
TextRenderer.DrawText(e.Graphics, OnText, this.Font, texttangle, TextBrush.Color, this.Parent.BackColor,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.GlyphOverhangPadding);
}
else
{
//ligth
e.Graphics.FillRectangle(OffPen, (indent + 2), 2, this.Size.Width - (indent + 4), this.Size.Height - 4);
//Toggle
e.Graphics.DrawRectangle(ControlPen, indent, -0, (toggleblocsize + 1), this.Size.Height - 1);
e.Graphics.FillRectangle(ToggleBrush, (indent + 1), -1, (toggleblocsize), this.Size.Height + 1);
if (ColorText && this.Enabled) TextBrush = new SolidBrush(SystemColors.ControlText);
// Draw string to screen.
TextRenderer.DrawText(e.Graphics, OffText, this.Font, texttangle, TextBrush.Color, this.Parent.BackColor,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter |
TextFormatFlags.GlyphOverhangPadding);
}
这就得到了我想要的图形效果。感谢mark将其清理干净: