这是一个奇怪的问题,描述的时间有点长,所以请耐心等待。
我有一个名为Toggleswitch
(tsw)的c#4客户端配置文件的控件。这个toggleswitch的设计就像地铁开关一样,这很棒......直到它被添加到Tap控件/ Tap页面然后:
渲染行为很奇怪,没有正确地绘制背景,并且没有显示开/关标签。这也会影响其他控件,就像整个表单不会被绘制一样。
VS 2010和2013设计编辑器属性字段也冻结,因此我无法访问其他控件
如果保持正常,程序将无法构建......基本上已经崩溃了VS ..
但是,如果我将水龙头控制从普通按钮更改为平板按钮或按钮,那么它会一直停止并平稳运行,这很好,但后来我开始想知道为什么它不能正常渲染
所以这就是我试过的
tsw扩展Buttonbase
以获得可点击性。所以我把它改为控制然后OnPaint
被无限循环调用,这样就没有帮助,点击也不起作用,但现在不那么重要了
我延长了按钮,没有任何帮助仍然很奇怪
覆盖按钮和按钮库中的每一个方法,看看是否仍然没有帮助。
在查看表单设计器后,我发现改变的唯一属性是UseVisualStyleBackColor
,当它为false时它会正常工作,所以我做了一个覆盖,强制它是假的,仍然没有工作:/
我甚至尝试删除了部分关键字。
我花了半天时间阅读关于点按和按钮控制的defriend方法
甚至找到了一个自定义控件测试按钮的扩展,工作正常,但没有理由,因为唯一不同于我能看到的OnPaint
中有关某些渲染的if语句。
....什么都行不通......
这是完整的togglewitch:
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);
}
}
并不是说我不能忍受扁平的控制,这就是它不起作用的原因让我感到烦恼......
非常感谢任何帮助:)
最后一些问题的可视化文档:
以下是它应该如何运作:
点击正常
答案 0 :(得分:0)
修复它以便它现在可以工作,结果发现有一些错误。
基类的第一个错误,它应该是一个控件
public class ToggleSwith : Control
并且on paint事件已更改为此
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);
}
得到了我正在寻找的图形效果。