【问题标题】:Extra rectangle gets drawn after label标签后绘制额外的矩形
【发布时间】:2019-10-06 06:20:45
【问题描述】:

我想将下面的彩色面板包含在我的表单中:

为此,我创建了自定义面板,它将根据单选按钮选择更改边框颜色。我的面板代码是

InfoPanel.cs

 class InfoPanel : Panel
 {
     private Color colorBorder = Color.Transparent;

     public InfoPanel()
         : base()
     {
         this.SetStyle(ControlStyles.UserPaint, true);
     }

     protected override void OnPaint(PaintEventArgs e)
     {
         base.OnPaint(e);
         e.Graphics.DrawRectangle(
             new Pen(
                 new SolidBrush(colorBorder), 2),
                 e.ClipRectangle);            
         e.Graphics.DrawLine(new Pen(new SolidBrush(colorBorder), 0), 50, 0, 50, 50); //drawing a line to split the child & parent info panel
     }

     public Color BorderColor
     {
         get
         {
             return colorBorder;
         }
         set
         {
             colorBorder = value;
         }
     }
 }

以我的形式, 1.创建了一个父信息面板 2.创建一个带有图片框的子面板 3. 父信息面板中的一个标签来显示信息

现在,对于父面板,我正在根据用户选择更改颜色 [后退、边框] 和文本,而对于子面板,我不会更改边框颜色,而是根据用户选择更新背景颜色。

下面是更改面板颜色、图片、文字更新的代码:

private void rbIPAddress_CheckedChanged(object sender, EventArgs e)
{
    if (rbIPAddress.Checked)
    {                
        ParentInfoPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFEE");
        ParentInfoPanel.BorderColor = System.Drawing.ColorTranslator.FromHtml("#DADA85");
        ChildInfoPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#F6F6D8");
        InfoPanelPictureBox.Image = Template.InfoPanelInfoImage;
        Infolabel.Text = "IP Address is already configured. You can switch to Forward Lookup Zone by choosing other configuration. *IP Address \ncan be either LB IP Address.";
        txtBoxIPAddress.Enabled = true;
        textBoxPort.Enabled = true;   
    }
    else
    {
        Infolabel.Text = "";
        txtBoxIPAddress.Text = "";
        txtBoxIPAddress.Enabled = false;
        textBoxPort.Enabled = false;
    }
}

private void rbForwardLookupZone_CheckedChanged(object sender, EventArgs e)
{
    if (rbForwardLookupZone.Checked)
    {
        ParentInfoPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#FFFFEE");
        ParentInfoPanel.BorderColor = System.Drawing.ColorTranslator.FromHtml("#DADA85");
        ChildInfoPanel.BackColor = System.Drawing.ColorTranslator.FromHtml("#F6F6D8");
        InfoPanelPictureBox.Image = Template.InfoPanelInfoImage;
        Infolabel.Text = "Forward Lookup Zone is already configured. You can switch to IP Address by choosing other configuration and \nchanging port number will affect Firewall rules.";
        textBoxControlPlane.Enabled = true;

        if (string.IsNullOrEmpty(textBoxControlPlane.Text))
        {
            textBoxControlPlane.Text = Constants.DefaultControlPlaneDomain;
        }
    }
    else
    {
        Infolabel.Text = "";
        textBoxControlPlane.Text = "";
        textBoxControlPlane.Enabled = false;
    }
}

注意:使用下一行字符多行显示标签文本

输出: 一切正常,但在标签文本末尾我得到另一个矩形框。我想知道为什么会这样显示?我做错了吗?请帮助我。

【问题讨论】:

标签: c# .net winforms user-controls panel


【解决方案1】:

问题是您使用的是e.ClipRectangle。它会通知您需要重绘控件的哪个部分。这有时只是控件的一小部分,而不是整个控件(在您的情况下是额外矩形的区域)。始终改为绘制控件的整个矩形。

此外,您必须同时处理 PenSolidBrush。不这样做会导致内存泄漏。使用using statement

using(SolidBrush brush = new SolidBrush(colorBorder))
using(Pen pen = new Pen(brush, 2))
{
    e.Graphics.DrawRectangle(pen, new Rectangle(0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1));
    e.Graphics.DrawLine(pen, 50, 0, 50, 50);
}

【讨论】:

  • @saravana :很高兴我能帮上忙。祝你的项目好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多