【问题标题】:Custom control rendering weird in TapcontrolTapcontrol中的自定义控件渲染很奇怪
【发布时间】:2014-11-27 13:54:57
【问题描述】:

这是一个奇怪的问题,描述起来会有点长,所以请耐心等待。

我有一个为 c# 4 客户端配置文件制作的控件,名为 Toggleswitch (tsw)。这个拨动开关的设计就像地铁开关一样,这确实很棒......直到它被添加到一个点击控制/点击页面然后:

  • 渲染行为很奇怪,没有正确绘制背景,并且开/关标签不显示。这也会影响其他控件,就像整个表单不会绘制一样。

  • VS 2010 和 2013 设计编辑器的属性字段也会冻结,因此我无法访问其他控件

  • 如果保持正常,程序将无法构建...基本上崩溃了 VS..

但是,如果我将点击控件从正常更改为平面按钮或按钮,那么它会像往常一样停止并平稳运行,这很好,但后来我开始想知道为什么它不能正常渲染

这就是我尝试过的方法

  • tsw 扩展了Buttonbase 以获得可点击性。所以我改变了它来控制然后OnPaint在无限循环中被调用所以没有帮助,点击也不起作用,但现在不那么重要了

  • 我扩展了按钮,没有帮助仍然很奇怪

  • 覆盖按钮和按钮库中的每一个方法,看看是否仍然没有任何帮助。

  • 查看表单设计器后,我发现唯一更改的属性是 UseVisualStyleBackColor,当它为 false 时,它​​在 true 时不起作用,所以我做了一个覆盖,强制它为 false,仍然不起作用:/

  • 我什至尝试删除部分关键字。

  • 我花了半天时间阅读关于点击和按钮控件的解除好友方法

  • 甚至发现一个自定义控件测试扩展按钮工作正常,但没有理由这样做,因为与我看到的唯一不同的是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);
        }
    }

并不是我不能忍受一个扁平的 tapcontrol 而是它为什么不能工作让我烦恼......

任何帮助将不胜感激:)

最后是一些问题的可视化文档:

这是它的工作原理:

这是在正常情况下点击

【问题讨论】:

  • 感谢标记很好地清理了它:)

标签: c# rendering custom-controls


【解决方案1】:

已将其修复以使其现在可以正常工作,但事实证明存在一些问题。

第一个基类错了,应该是控件

   public class ToggleSwith : Control

onpaint 事件变成了这个

    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);
        }

得到了我正在寻找的图形结果。

【讨论】:

    猜你喜欢
    • 2017-02-04
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多