2016-01-03 5 views
5

Прозрачные изображения являются чистым злом в Windows Forms, поэтому я создал собственный класс управления для их обработки. Дизайнер не показывает мой контроль, когда он пуст. Я хотел бы добавить тонкую границу, но только в представлении дизайна (и когда граница не добавляется пользователем). Как мне это сделать?Как добавить границу к настраиваемому элементу управления в виде дизайна?

Мой класс:

class TransparentImage : Control 
{ 
    public Image Image { get; set; } 

    protected Graphics graphics; 

    public string FilePath { get; set; } 

    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT 

      return cp; 
     } 
    } 

    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 
     // Don't paint background 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     // Update the private member so we can use it in the OnDraw method 
     this.graphics = e.Graphics; 

     // Set the best settings possible (quality-wise) 
     this.graphics.TextRenderingHint = 
      System.Drawing.Text.TextRenderingHint.AntiAlias; 
     this.graphics.InterpolationMode = 
      System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; 
     this.graphics.PixelOffsetMode = 
      System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
     this.graphics.SmoothingMode = 
      System.Drawing.Drawing2D.SmoothingMode.HighQuality; 

     if (Image != null) 
     { 
      // Sets the images' sizes and positions 
      var width = Image.Size.Width; 
      var height = Image.Size.Height; 
      var size = new Rectangle(0, 0, width, height); 

      // Draws the two images 
      this.graphics.DrawImage(Image, size); 
     } 
    } 
} 

ответ

6

Проверить if (this.DesignMode) в вашем OnPaint и вызвать DrawRectangle.

Возможно, вы захотите использовать new Pen(SystemColors.ActiveBorder) { DashStyle = DashStyle.Dot }

Смежные вопросы