Re: Change border color for a control
- From: "RichS" <richard.somerfield@xxxxxxxxxxxxxxx>
- Date: 29 Aug 2006 00:47:12 -0700
Hi all,
I've tried all sorts of solutions to try to get this to work. I tried
the WM_NCPAINT one, this almost worked, but it was v. complicated and
had strange missed bits when running through RemoteDesktop instances.
The best and simplest solution is this ( this is for the Panel only, I
usually use Panels for layout and then dock my controls in them,
alternatively you may be able to use this idea for your custom controls
). The trick is to override "DisplayRectangle" to fake the actual
client area - so when control is added to the panel and the Dock set to
fill, it uses the DisplayRectangle size and not the actual control
size. Note: I've modified slightly the simplify so this wont compile
directly, but I'm sure you can work it out:
public class CustomPanel : Panel
{
...
/// <summary>
/// By providing the display rectangle we can effectively fake
the client area. This fools child "Dock"s into only consuming the
DisplayRectangle area.
/// </summary>
public override Rectangle DisplayRectangle
{
get
{
Rectangle rect = base.DisplayRectangle;
if ( true == this.bPadding )
{
rect = new Rectangle ( iSideMargin, iTopMargin,
base.Width - ( 2 * iSideMargin ), base.Height - ( 2 * iTopMargin ) );
}
return rect;
}
}
...
/// <summary>
/// Handles the painting of the custom border - if required
/// </summary>
/// <param name="pe"></param>
protected override void OnPaint( PaintEventArgs pe )
{
if ( 0 != this.Width && 0 != this.Height )
{
if ( this.bPadding )
{
// -1 is required to be just inside the controls
area
Rectangle rect = new Rectangle( 0, 0, this.Width-1,
this.Height-1 );
// We just paint a border outline with the max
border size. Ideally we should paint each section of the
// border individually ( 4 rectangles, and 4 corner
squares ), but this is a cheat to save performance.
// If there are issues with anchoring ( i.e. child
controls are much narrower and dodgy lines are being drawn
// then this will have to be changed ).
pe.Graphics.DrawRectangle( this.BorderPen, rect );
// RS: Uncomment the next line of code to do a
Border test - all Panels should have a red border
//pe.Graphics.DrawRectangle( new Pen( Color.Red, 1
), rect );
}
}
// Calling the base class OnPaint
base.OnPaint( pe );
}
...
}
regards,
Rich.
.
- References:
- Change border color for a control
- From: MuZZy
- Re: Change border color for a control
- From: Claes Bergefall
- Change border color for a control
- Prev by Date: Re: PLS HELP! Problem with CheckedListBox changing Parent
- Next by Date: Re: Wizard interface ?
- Previous by thread: Re: Change border color for a control
- Next by thread: Re: Change border color for a control
- Index(es):
Relevant Pages
|
|