Problem with custom Panel class

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi,

I'm extending the Panel control to allow selection of controls on the Panel
(comparable to selecting icons on the desktop). The problem is that the
selection rectangle is drawn behind the controls on the Panel. What should I
do to have the rectangle drawn in front of the controls on the Panel?

<code>
public class SelectionPanel : Panel
{
private bool isMouseDown = false;
private Point dragFromLocation = new Point( );
private Point dragToLocation = new Point( );

public SelectionPanel( )
{
InitializeComponent( );
}

protected override void OnPaint( PaintEventArgs pe )
{
base.OnPaint( pe );

if( isMouseDown )
{
int x = Math.Min( dragFromLocation.X, dragToLocation.X );
int y = Math.Min( dragFromLocation.Y, dragToLocation.Y );
int width = Math.Abs( dragFromLocation.X - dragToLocation.X );
int height = Math.Abs( dragFromLocation.Y - dragToLocation.Y );

Rectangle rectangle = new Rectangle( x, y, width, height );

ControlPaint.DrawFocusRectangle( pe.Graphics, rectangle );
}
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
dragFromLocation = new Point( e.X, e.Y );
dragToLocation = dragFromLocation;
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
Invalidate( );
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
if( isMouseDown )
{
dragToLocation = new Point( e.X, e.Y );
Invalidate( );
}
}

private void InitializeComponent( )
{
this.MouseUp += new System.Windows.Forms.MouseEventHandler(
this.OnMouseUp );
this.MouseMove += new System.Windows.Forms.MouseEventHandler(
this.OnMouseMove );
this.MouseDown += new System.Windows.Forms.MouseEventHandler(
this.OnMouseDown );
}
}
</code>

Kind regards,
Tom Tempelaere.



.



Relevant Pages

  • RE: GDI+ Clip Region allows 0.0f width pen to draw through
    ... Just to make this more outrageous if you draw the vertical lines from the ... I took the previous code I posted and made the panel anchor to all four ... private void panel1_SizeChanged ... In other words create a rectangle that is a wider than the panel. ...
    (microsoft.public.win32.programmer.gdi)
  • Re: Which pattern for Settings/Preferences/Options scenario?
    ... Adds the given option's alternatives to the given panel. ... private void addOption{ ... presented as a check-button selection. ... @return namespace text display choices ...
    (comp.object)
  • RE: GDI+ Clip Region allows 0.0f width pen to draw through
    ... private void panel1_Paint ... I took the previous code I posted and made the panel anchor to all four ... Using a zero width pen I can draw into areas that are not part of the clip ... The first rectangle does not fit in the panel in this very specific manner: ...
    (microsoft.public.win32.programmer.gdi)
  • Problem with custom Panel class
    ... I'm extending the Panel control to allow selection of controls on the Panel ... selection rectangle is drawn behind the controls on the Panel. ... private void OnMouseDown ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: My custom control has issues
    ... Ultimately I have written a new control using pieces of zoompicbox as a skeleton and it's working pretty well, but there are some problems. ... This control is called PixelViewer and that's its purpose: to view an image and let you zoom in and see the pixels as pixels, not a smoothed image. ... Since GDI+ doesn't allow for XOR drawing, I've resorted to using GDI to drawing my rubber-band selection. ... That part works fine, but when I draw the final selection rectangle I get a black box, not inverted colors. ...
    (microsoft.public.dotnet.framework.drawing)