Re: Change border color for a control



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.

.



Relevant Pages

  • Re: Draw over controls
    ... want to draw a rectangle over everything. ... The problem is because I have to pass the rect ... of the panel, it draws on that, and under the other controls. ... You shouldn't really draw outside Paint events, because other controls ...
    (microsoft.public.dotnet.languages.csharp)
  • Draw over controls
    ... I have a panel that contains a label and a picturebox. ... want to draw a rectangle over everything. ... The problem is because I have to pass the rect ... of the panel, it draws on that, and under the other controls. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Postback bevor Buttonereignis
    ... // Controls im Panel Kategorie setzen ... Auf dieser Ansicht hat der Benutzer eben diesen Button, wo er den Modus ... dass beim Click auf den Button zuerst der Postback ...
    (microsoft.public.de.german.entwickler.dotnet.asp)
  • RE: Repost: Controls are unreachable after scroll and form resize.
    ... form are visible and there is no scroll bars on the panel. ... some of the controls on the panel are placed ... Then if I resize the child form to the previous size ... the same as the locations after I drag the scroll bar just now. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • RE: Repost: Controls are unreachable after scroll and form resize.
    ... form are visible and there is no scroll bars on the panel. ... some of the controls on the panel are placed ... Then if I resize the child form to the previous size ... the same as the locations after I drag the scroll bar just now. ...
    (microsoft.public.dotnet.framework.windowsforms)