PaintUnit.pas
上传用户:hylc_2004
上传日期:2014-01-23
资源大小:46800k
文件大小:4k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. unit PaintUnit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5.   ComCtrls, ExtCtrls, fcClearPanel, fcOutlookBar, StdCtrls, fcCombo,
  6.   fcColorCombo, fcOutlookList, fcLabel,
  7.   {$ifndef VER100}
  8.   ImgList,
  9.   {$endif}
  10.   fcButtonGroup, fcButton, fcImgBtn, fcShapeBtn;
  11. type
  12.   TDrawingTool = (dtLine, dtRectangle, dtEllipse, dtRoundRect);
  13.   TPaintForm = class(TForm)
  14.     PaintBox: TImage;
  15.     BrushImages: TImageList;
  16.     PenImages: TImageList;
  17.     ToolImages: TImageList;
  18.     fcControlBar1: TfcOutlookBar;
  19.     fcControlBar1fcShapeBtn1: TfcShapeBtn;
  20.     fcControlBar1fcShapeBtn2: TfcShapeBtn;
  21.     fcControlBar1fcShapeBtn3: TfcShapeBtn;
  22.     fcControlBar1fcShapeBtn4: TfcShapeBtn;
  23.     ToolsOutlookList: TfcOutlookList;
  24.     BrushesOutlookList: TfcOutlookList;
  25.     PensOutlookList: TfcOutlookList;
  26.     BrushColorCombo: TfcColorCombo;
  27.     fcLabel1: TfcLabel;
  28.     fcLabel2: TfcLabel;
  29.     PenColorCombo: TfcColorCombo;
  30.     procedure PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
  31.       Shift: TShiftState; X, Y: Integer);
  32.     procedure PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
  33.       Y: Integer);
  34.     procedure PaintBoxMouseUp(Sender: TObject; Button: TMouseButton;
  35.       Shift: TShiftState; X, Y: Integer);
  36.     procedure FormShow(Sender: TObject);
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure FormActivate(Sender: TObject);
  39.   private
  40.     { Private declarations }
  41.     Drawing: Boolean;
  42.     Origin, MovePt: TPoint;
  43.     InfoShown: Boolean;
  44.     procedure DrawShape(TopLeft, BottomRight: TPoint; AMode: TPenMode);
  45.   public
  46.     { Public declarations }
  47.   end;
  48. var
  49.   PaintForm: TPaintForm;
  50. implementation
  51. uses InfoUnit;
  52. {$R *.DFM}
  53. procedure TPaintForm.PaintBoxMouseDown(Sender: TObject; Button: TMouseButton;
  54.   Shift: TShiftState; X, Y: Integer);
  55. begin
  56.   Drawing := True;
  57.   PaintBox.Canvas.MoveTo(X, Y);
  58.   Origin := Point(X, Y);
  59.   MovePt := Origin;
  60. end;
  61. procedure TPaintForm.PaintBoxMouseMove(Sender: TObject; Shift: TShiftState; X,
  62.   Y: Integer);
  63. begin
  64.   if Drawing then
  65.   begin
  66.     DrawShape(Origin, MovePt, pmNotXor);
  67.     MovePt := Point(X, Y);
  68.     DrawShape(Origin, MovePt, pmNotXor);
  69.   end;
  70. end;
  71. procedure TPaintForm.PaintBoxMouseUp(Sender: TObject; Button: TMouseButton;
  72.   Shift: TShiftState; X, Y: Integer);
  73. begin
  74.   if Drawing then
  75.   begin
  76.     DrawShape(Origin, Point(X, Y), pmCopy);
  77.     Drawing := False;
  78.   end;
  79. end;
  80. procedure TPaintForm.DrawShape(TopLeft, BottomRight: TPoint; AMode: TPenMode);
  81. begin
  82.   with PaintBox.Canvas do
  83.   begin
  84.     Brush.Color := BrushColorCombo.SelectedColor;
  85.     Pen.Color := PenColorCombo.SelectedColor;
  86.     Pen.Mode := AMode;
  87.     if BrushesOutlookList.Selected <> nil then Brush.Style := TBrushStyle(BrushesOutlookList.Selected.Index);
  88.     if PensOutlookList.Selected <> nil then Pen.Style := TPenStyle(PensOutlookList.Selected.Index);
  89.     if ToolsOutlookList.Selected <> nil then
  90.       case TDrawingTool(ToolsOutlookList.Selected.Index) of
  91.         dtLine: begin
  92.           MoveTo(TopLeft.X, TopLeft.Y);
  93.           LineTo(BottomRight.X, BottomRight.Y);
  94.         end;
  95.         dtRectangle: Rectangle(TopLeft.X, TopLeft.Y, BottomRight.X,
  96.           BottomRight.Y);
  97.         dtEllipse: Ellipse(Topleft.X, TopLeft.Y, BottomRight.X,
  98.           BottomRight.Y);
  99.         dtRoundRect: RoundRect(TopLeft.X, TopLeft.Y, BottomRight.X,
  100.           BottomRight.Y, (TopLeft.X - BottomRight.X) div 2,
  101.           (TopLeft.Y - BottomRight.Y) div 2);
  102.       end;
  103.   end;
  104. end;
  105. procedure TPaintForm.FormShow(Sender: TObject);
  106. begin
  107.   PaintBox.Canvas.Brush.Color := clWhite;
  108.   PaintBox.Canvas.FillRect(PaintBox.ClientRect);
  109. end;
  110. procedure TPaintForm.FormCreate(Sender: TObject);
  111. begin
  112.   InfoForm := TInfoForm.Create(self);
  113. end;
  114. procedure TPaintForm.FormActivate(Sender: TObject);
  115. begin
  116.   if not InfoShown then begin
  117.     InfoForm.Show;
  118.     InfoShown := True;
  119.   end;  
  120. end;
  121. end.