Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I do custom drawing on my control designer's surface during design-time?

Override OnPaintAdornments in your Control Designer. This will be called after the Control has painted.

A good example is when you have a Control that has its border style set to None, and you want to draw a dummy border in the designer.

protected override void OnPaintAdornments( PaintEventArgs pe ) 
{ 
  Panel panel = (Panel) Component; 
  if ( panel.BorderStyle == BorderStyle.None ) 
    DrawDesignTimeBorder( pe.Graphics, panel ); 
  base.OnPaintAdornments( pe ); 
}

public void DrawDesignTimeBorder( Graphics g, Control control ) 
{ 
  Rectangle clientRectangle = control.ClientRectangle; 
  Color bgColor = control.BackColor; 
  Color adjustedBgColor = ( (double) bgColor.GetBrightness() ) >= 0.5 
    ? ControlPaint.Dark( control.BackColor )
    : ControlPaint.Light( control.BackColor ); 
            
  using ( Pen pen = new Pen( adjustedBgColor ) )
  {
    pen.DashStyle = DashStyle.Dash; 
    clientRectangle.Width -= 1; 
    clientRectangle.Height -= 1; 
    g.DrawRectangle( pen, clientRectangle ); 
  }
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter