In Depth Banner
Skip Navigation Links

Select your preferred language

The FilledGroupBox control and designer

using System;

using System.ComponentModel;

using System.Drawing;

using System.Drawing.Drawing2D;

using System.Windows.Forms;

using System.Windows.Forms.Design;

 

namespace WellFormed

{

  /// <summary>

  /// Summary description for FilledGroupBox.

  /// </summary>

  [Designer(typeof(FilledGroupBoxDesigner))]

  public class FilledGroupBox : GroupBox

  {

    Color _innerColor=Color.CadetBlue;

    public Color InnerColor

    {

      get{return _innerColor;}

      set{

        _innerColor=value;

        Invalidate();

      }

    }

 

    Color _outerColor=Color.Wheat;

    public Color OuterColor

    {

      get{return _outerColor;}

      set{

        _outerColor=value;

        Invalidate();

      }

    }

 

    Point _focusPoint=new Point(50,50);

    public Point FocusPoint

    {

      get{return _focusPoint;}

      set{

        _focusPoint=value;

        Invalidate();

      }

    }

 

    public FilledGroupBox()

    {

    }

 

    protected override void OnPaintBackground(PaintEventArgs pevent)

    {

      GraphicsPath pth=new GraphicsPath();

      pth.AddRectangle(this.ClientRectangle);

      PathGradientBrush pgb=new PathGradientBrush(pth);

      pgb.CenterColor=this.InnerColor;

      pgb.SurroundColors=new Color[]{this.OuterColor};

      pgb.CenterPoint=this.FocusPoint;

      pevent.Graphics.FillRectangle(pgb,this.ClientRectangle);

      pgb.Dispose();

      pth.Dispose();

    }

 

  }

 

 

  public class FilledGroupBoxDesigner : ControlDesigner

  {

    //True when the mouse is down

    bool _mouseDown;

    //True when the mouse is in the grab-handle.

    bool _inGrabber;

 

    //Called when the designer is initialized

    public override void Initialize(IComponent component)

    {

      base.Initialize (component);

      //adds handlers to the mouse events of the control

      this.Control.MouseDown+=new MouseEventHandler(Control_MouseDown);

      this.Control.MouseUp+=new MouseEventHandler(Control_MouseUp);

      this.Control.MouseMove+=new MouseEventHandler(Control_MouseMove);

    }

 

    protected override void Dispose(bool disposing)

    {

      //removes handlers from the mouse events of the control

      this.Control.MouseDown-=new MouseEventHandler(Control_MouseDown);

      this.Control.MouseUp-=new MouseEventHandler(Control_MouseUp);

      this.Control.MouseMove-=new MouseEventHandler(Control_MouseMove);

      base.Dispose (disposing);

    }

 

 

    protected override void OnPaintAdornments(PaintEventArgs pe)

    {

      //paints the grab-handle over the focus point.

      base.OnPaintAdornments (pe);

      FilledGroupBox fgb=(FilledGroupBox)this.Control;

      pe.Graphics.FillRectangle(Brushes.White,fgb.FocusPoint.X-4,fgb.FocusPoint.Y-4,8,8);

      pe.Graphics.DrawRectangle(Pens.Black,fgb.FocusPoint.X-4,fgb.FocusPoint.Y-4,8,8);

    }

 

    protected override bool GetHitTest(Point point)

    {

      //checks to see if the point is in the grab-handle

      FilledGroupBox fgb=(FilledGroupBox)this.Control;

      Point pnt=fgb.PointToClient(point);

      Rectangle rc=new Rectangle(fgb.FocusPoint.X-4,fgb.FocusPoint.Y-4,8,8);

      if(rc.Contains(pnt))

      {

        _inGrabber=true;

        return true;

      }

      _inGrabber=false;

      return base.GetHitTest (point);

    }

 

    protected override void OnSetCursor()

    {

      if(_inGrabber)

        Control.Cursor=Cursors.Hand;

      else

        base.OnSetCursor ();

    }

 

    private void Control_MouseDown(object sender, MouseEventArgs e)

    {

      _mouseDown=true;

    }

 

    private void Control_MouseUp(object sender, MouseEventArgs e)

    {

      _mouseDown=false;

    }

 

    private void Control_MouseMove(object sender, MouseEventArgs e)

    {

      if(_mouseDown)

      {

        //modifies the value in the control

        FilledGroupBox fgb=(FilledGroupBox)this.Control;

        MemberDescriptor md=TypeDescriptor.GetProperties(fgb,null)["FocusPoint"];

        PropertyDescriptor pd=md as PropertyDescriptor;

        Point oldPoint=(Point)pd.GetValue(fgb);

        if(pd!=null)

          pd.SetValue(fgb,new Point(e.X,e.Y));

        //update the designer host and the property grid

        this.RaiseComponentChanging(md);

        this.RaiseComponentChanged(md,oldPoint,new Point(e.X,e.Y));

        fgb.Invalidate();

      }

    }

  }

}

 

Return to the article

Copyright © Bob Powell 2003-2009. All rights reserved