【问题标题】:VisualStudio: How to add the dotted border to a UserControl at design time?Visual Studio:如何在设计时将虚线边框添加到用户控件?
【发布时间】:2009-02-06 20:55:20
【问题描述】:

我有一个继承自 UserControl 的用户控件。

当用户控件拖放到窗体上时,它是不可见的,因为它没有任何类型的边框来显示自己。

PictureBox 和 Panel 控件在设计时绘制一个 1px 虚线边框以使其可见。

这样做的正确方法是什么?有没有一个属性可以让 VS 添加它?

【问题讨论】:

    标签: visual-studio user-controls


    【解决方案1】:

    没有属性会自动执行此操作。但是,您可以通过覆盖控件中的 OnPaint 并手动绘制矩形来归档它。

    在被覆盖的事件中,您可以调用 base.OnPaint(e) 来绘制控件内容,然后使用图形对象在边缘周围绘制虚线。

     protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
    
            if (this.DesignMode)
                 ControlPaint.DrawBorder( e.Graphics,this.ClientRectangle,Color.Gray,ButtonBorderStyle.Dashed);   
        }
    

    如您所见,您需要将此额外代码包装在查询控件 DesignMode 属性的 if 语句中,以便它仅在您的 IDE 中绘制。

    【讨论】:

      【解决方案2】:

      Panel 执行此操作的方式(这表明这是执行此操作的实际正确方式)使用DesignerAttribute。此属性可用于设计时添加到 Control-components 以及非控制组件(如 Timer)。

      使用DesignerAttribute 时,您需要指定一个派生自IDesigner 的类。对于Control 设计师,您应该派生自ControlDesigner

      ControlDesigner 的特定实现中,您希望覆盖OnPaintAdornment。此方法的目的是专门在控件顶部绘制设计器提示,例如边框。

      下面是Panel 使用的实现。您可以复制它并将其用于您的控制,但您显然需要调整专门引用 Panel 类的部分。

      internal class PanelDesigner : ScrollableControlDesigner
      {
          protected Pen BorderPen
          {
              get
              {
                  Color color = ((double)this.Control.BackColor.GetBrightness() < 0.5) ? ControlPaint.Light(this.Control.BackColor) : ControlPaint.Dark(this.Control.BackColor);
                  return new Pen(color)
                  {
                      DashStyle = DashStyle.Dash
                  };
              }
          }
          public PanelDesigner()
          {
              base.AutoResizeHandles = true;
          }
          protected virtual void DrawBorder(Graphics graphics)
          {
              Panel panel = (Panel)base.Component;
              if (panel == null || !panel.Visible)
              {
                  return;
              }
              Pen borderPen = this.BorderPen;
              Rectangle clientRectangle = this.Control.ClientRectangle;
              int num = clientRectangle.Width;
              clientRectangle.Width = num - 1;
              num = clientRectangle.Height;
              clientRectangle.Height = num - 1;
              graphics.DrawRectangle(borderPen, clientRectangle);
              borderPen.Dispose();
          }
          protected override void OnPaintAdornments(PaintEventArgs pe)
          {
              Panel panel = (Panel)base.Component;
              if (panel.BorderStyle == BorderStyle.None)
              {
                  this.DrawBorder(pe.Graphics);
              }
              base.OnPaintAdornments(pe);
          }
      }
      

      ScrollableControlDesigner 是一个公共类,您可能希望也可能不希望将其用作特定设计器实现的基础。

      【讨论】:

      • +1。这是绘制设计时装饰的正确方法。与其检查 DesignMode 属性,不如创建一个自定义设计器。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2015-07-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多