home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / Shapes / Rectangle.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  2.2 KB  |  71 lines

  1. unit Rectangle;
  2. {*******************************************************************************
  3.   ShapesDemo
  4.   Written by David Clegg, davidclegg@optusnet.com.au.
  5.  
  6.   This unit contains the Rectangle class used to render a rectangle onto a GDI+
  7.   drawing surface.
  8. *******************************************************************************}
  9.  
  10. interface
  11.  
  12. uses
  13.   Shape, System.Drawing, System.Drawing.Drawing2D;
  14.  
  15. type
  16.  
  17.     /// <summary>
  18.     /// Class to draw a rectangle.
  19.     /// </summary>
  20.   TRectangle = class(TShape)
  21.   protected
  22.     function GetShape(pStartPoint, pEndPoint: Point): GraphicsPath; override;
  23.   public
  24.     procedure DrawRectangle(pBrush: Brush; pStartPoint, pEndPoint: Point);
  25.     procedure OutlineRectangle(pPen: Pen; pStartPoint, pEndPoint: Point);
  26.   end;
  27.  
  28. implementation
  29.  
  30. /// <summary>
  31. /// Alternative method to draw a solid rectangle. Not as good as the
  32. /// inherited Draw method as it doesn't cater for if an endPoint X or Y
  33. /// value is less than its corresponding startPoint value.
  34. /// </summary>
  35. procedure TRectangle.DrawRectangle(pBrush: Brush; pStartPoint, pEndPoint: Point);
  36. begin
  37.   Canvas.FillRectangle(pBrush, DoGetRectangle(pStartPoint, pEndPoint));
  38. end;
  39.  
  40. /// <summary>
  41. /// Alternative method to draw a rectangle outline. Not as good as the
  42. /// inherited Draw method as it doesn't cater for if an endPoint X or Y
  43. /// value is less than its corresponding startPoint value.
  44. /// </summary>
  45. procedure TRectangle.OutlineRectangle(pPen: Pen; pStartPoint, pEndPoint: Point);
  46. begin
  47.   Canvas.DrawRectangle(pPen, DoGetRectangle(pStartPoint, pEndPoint));
  48. end;
  49.  
  50. /// <summary>
  51. /// Return a GraphicsPath representing the bounds of the Rectangle based on
  52. /// pStartPoint and pEndPoint.
  53. /// </summary>
  54. function TRectangle.GetShape(pStartPoint, pEndPoint: Point): GraphicsPath;
  55. var
  56.   lTopRight: Point;
  57.   lBottomLeft: Point;
  58. begin
  59.   Result := GraphicsPath.Create;
  60.  
  61.   lTopRight := Point.Create(pStartPoint.X, pEndPoint.Y);
  62.   lBottomLeft := Point.Create(pEndPoint.X, pStartPoint.Y);
  63.  
  64.   Result.AddLine(pStartPoint, lTopRight);
  65.   Result.AddLine(lTopRight, pEndPoint);
  66.   Result.AddLine(pEndPoint, lBottomLeft);
  67.   Result.AddLine(lBottomLeft, pStartPoint);
  68. end;
  69.  
  70. end.
  71.