home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / GRAPHWIN.CPP < prev    next >
C/C++ Source or Header  |  1997-02-28  |  9KB  |  279 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1997 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. //---------------------------------------------------------------------------
  6. #include <vcl.h>
  7. #pragma hdrstop
  8. #include <vcl/printers.hpp>
  9.  
  10. #include "graphwin.h"
  11. #include "bmpdlg.h"
  12. #include <Clipbrd.hpp>
  13. //---------------------------------------------------------------------------
  14. #pragma resource "*.dfm"
  15. TForm1* Form1;
  16. //---------------------------------------------------------------------------
  17. __fastcall TForm1::TForm1(TComponent* Owner)
  18.   : TForm(Owner)
  19. {
  20.  Image->Canvas->MoveTo(0,0);
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::LineButtonClick(TObject *Sender)
  24. {
  25.   DrawingTool = dtLine;
  26. }
  27. //---------------------------------------------------------------------
  28. void __fastcall TForm1::RectangleButtonClick(TObject *Sender)
  29. {
  30.   DrawingTool = dtRectangle;
  31. }
  32. //---------------------------------------------------------------------
  33. void __fastcall TForm1::EllipseButtonClick(TObject *Sender)
  34. {
  35.   DrawingTool = dtEllipse;
  36. }
  37. //---------------------------------------------------------------------
  38. void __fastcall TForm1::RoundRectButtonClick(TObject *Sender)
  39. {
  40.   DrawingTool = dtRoundRect;
  41. }
  42. //---------------------------------------------------------------------
  43. void __fastcall TForm1::PenButtonClick(TObject *Sender)
  44. {
  45.   PenBar->Visible = PenButton->Down;
  46. }
  47. //---------------------------------------------------------------------
  48. void __fastcall TForm1::BrushButtonClick(TObject *Sender)
  49. {
  50.   BrushBar->Visible = BrushButton->Down;
  51. }
  52. //---------------------------------------------------------------------
  53. void __fastcall TForm1::SetPenStyle(TObject *Sender)
  54. {
  55.     if (Sender == SolidPen){
  56.        Image->Canvas->Pen->Style = psSolid;
  57.     }
  58.     else if (Sender == DashPen){
  59.             Image->Canvas->Pen->Style = psDash;
  60.     }
  61.     else if (Sender == DotPen){
  62.          Image->Canvas->Pen->Style = psDot;
  63.     }
  64.     else if (Sender == DashDotPen){
  65.          Image->Canvas->Pen->Style = psDashDot;
  66.     }
  67.     else if (Sender == DashDotDotPen){
  68.          Image->Canvas->Pen->Style = psDashDotDot;
  69.     }
  70.     else if (Sender == ClearPen){
  71.          Image->Canvas->Pen->Style = psClear;
  72.     }
  73. }
  74. //---------------------------------------------------------------------
  75. void __fastcall TForm1::PenColorClick(TObject *Sender)
  76. {
  77.   ColorDialog1->Color = Image->Canvas->Pen->Color;
  78.   if (ColorDialog1->Execute()){
  79.     Image->Canvas->Pen->Color = ColorDialog1->Color;
  80.   }
  81. }
  82. //---------------------------------------------------------------------
  83. void __fastcall TForm1::PenSizeChange(TObject *Sender)
  84. {
  85.   Image->Canvas->Pen->Width = PenWidth->Position;
  86. }
  87. //---------------------------------------------------------------------
  88. void __fastcall TForm1::SetBrushStyle(TObject *Sender)
  89. {
  90.     if (Sender == SolidBrush){
  91.        Image->Canvas->Brush->Style = bsSolid;
  92.     }
  93.     else if (Sender == ClearBrush){
  94.          Image->Canvas->Brush->Style = bsClear;
  95.     }
  96.     else if (Sender == HorizontalBrush){
  97.          Image->Canvas->Brush->Style = bsHorizontal;
  98.     }
  99.     else if (Sender == VerticalBrush){
  100.          Image->Canvas->Brush->Style = bsVertical;
  101.     }
  102.     else if (Sender == FDiagonalBrush){
  103.          Image->Canvas->Brush->Style = bsFDiagonal;
  104.     }
  105.     else if (Sender == BDiagonalBrush){
  106.          Image->Canvas->Brush->Style = bsBDiagonal;
  107.     }
  108.     else if (Sender == CrossBrush){
  109.          Image->Canvas->Brush->Style = bsCross;
  110.     }
  111.     else if (Sender == DiagCrossBrush){
  112.          Image->Canvas->Brush->Style = bsDiagCross;
  113.     }
  114. }
  115. //---------------------------------------------------------------------
  116. void __fastcall TForm1::BrushColorClick(TObject *Sender)
  117. {
  118.   ColorDialog1->Color = Image->Canvas->Brush->Color;
  119.   if (ColorDialog1->Execute()){
  120.     Image->Canvas->Brush->Color = ColorDialog1->Color;
  121.   }
  122. }
  123. //---------------------------------------------------------------------
  124. void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
  125.       TShiftState Shift, int X, int Y)
  126. {
  127.   Drawing = True;
  128.   Image->Canvas->MoveTo(X, Y);
  129.   Origin = Point(X, Y);
  130.   MovePt = Origin;
  131.  
  132.   TVarRec tempvar[2] = {X, Y};
  133.   StatusBar1->Panels->Items[0]->Text = Format("Origin: (%d, %d)", tempvar, 2);
  134. }
  135. //---------------------------------------------------------------------
  136. void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
  137.       int X, int Y)
  138. {
  139.   if (Drawing){
  140.     DrawShape(Origin, MovePt, pmNotXor);
  141.     MovePt = Point(X, Y);
  142.     DrawShape(Origin, MovePt, pmNotXor);
  143.   }
  144.  
  145.   TVarRec tempvar[2] = {X, Y};
  146.   StatusBar1->Panels->Items[1]->Text = Format("Current: (%d, %d)", tempvar, 2);
  147. }
  148. //---------------------------------------------------------------------
  149. void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
  150.       TShiftState Shift, int X, int Y)
  151. {
  152.   if (Drawing){
  153.     DrawShape(Origin, Point(X, Y), pmCopy);
  154.     Drawing = False;
  155.   }
  156. }
  157. //---------------------------------------------------------------------
  158. void __fastcall TForm1::New1Click(TObject *Sender)
  159. {
  160.      Graphics::TBitmap *Bitmap;
  161.  
  162.      NewBMPForm->ActiveControl = NewBMPForm->WidthEdit;
  163.      NewBMPForm->WidthEdit->Text = IntToStr(Image->Picture->Graphic->Width);
  164.      NewBMPForm->HeightEdit->Text = IntToStr(Image->Picture->Graphic->Height);
  165. //     if (ShowModal() != idCancel){
  166.      if (NewBMPForm->ShowModal() != IDCANCEL){
  167.         Bitmap = new Graphics::TBitmap();
  168. //        Bitmap->Create();
  169.         Bitmap->Width = StrToInt(NewBMPForm->WidthEdit->Text);
  170.         Bitmap->Height = StrToInt(NewBMPForm->HeightEdit->Text);
  171.         Image->Picture->Graphic = Bitmap;
  172.         CurrentFile = EmptyStr;
  173.      }
  174. }
  175. //---------------------------------------------------------------------
  176. void __fastcall TForm1::Open1Click(TObject *Sender)
  177. {
  178.   if (OpenDialog1->Execute()){
  179.     CurrentFile = OpenDialog1->FileName;
  180.     Image->Picture->LoadFromFile(CurrentFile);
  181.   }
  182. }
  183. //---------------------------------------------------------------------
  184. void __fastcall TForm1::Save1Click(TObject *Sender)
  185. {
  186.   if (CurrentFile != EmptyStr){
  187.     Image->Picture->SaveToFile(CurrentFile);
  188.   }
  189.   else{
  190.    SaveAs1Click(Sender);
  191.   }
  192. }
  193. //---------------------------------------------------------------------
  194. void __fastcall TForm1::SaveAs1Click(TObject *Sender)
  195. {
  196.   if (SaveDialog1->Execute()){
  197.     CurrentFile = SaveDialog1->FileName;
  198.     Save1Click(Sender);
  199.   }
  200. }
  201. //---------------------------------------------------------------------
  202. void __fastcall TForm1::Exit1Click(TObject *Sender)
  203. {
  204.   Close();
  205. }
  206. //---------------------------------------------------------------------
  207. void __fastcall TForm1::Cut1Click(TObject *Sender)
  208. {
  209.  TRect ARect;
  210.  
  211.  Copy1Click(Sender);
  212.  
  213.  Image->Canvas->CopyMode = cmWhiteness;
  214.  ARect = Rect(0, 0, Image->Width, Image->Height);
  215.  Image->Canvas->CopyRect(ARect, Image->Canvas, ARect);
  216.  Image->Canvas->CopyMode = cmSrcCopy;
  217. }
  218.  
  219. //---------------------------------------------------------------------
  220. void __fastcall TForm1::Copy1Click(TObject *Sender)
  221. {
  222.   Clipboard()->Assign(Image->Picture);
  223. }
  224.  
  225. //---------------------------------------------------------------------
  226. void __fastcall TForm1::Paste1Click(TObject *Sender)
  227. {
  228.  
  229.   Graphics::TBitmap *Bitmap;
  230.  
  231.   if (Clipboard()->HasFormat(CF_BITMAP)){
  232.     Bitmap = new Graphics::TBitmap();
  233.     try{
  234.       Bitmap->Assign(Clipboard());
  235.       Image->Canvas->Draw(0, 0, Bitmap);
  236.       delete Bitmap;
  237.     }
  238.     catch(...){
  239.       delete Bitmap;
  240.     }
  241.   }
  242. }
  243.  
  244. //---------------------------------------------------------------------
  245. void __fastcall TForm1::DrawShape(TPoint TopLeft, TPoint BottomRight, TPenMode AMode){
  246.     Image->Canvas->Pen->Mode = AMode;
  247.  
  248.     switch (DrawingTool){
  249.            case dtLine : {
  250.                 Image->Canvas->MoveTo(TopLeft.x, TopLeft.y);
  251.                 Image->Canvas->LineTo(BottomRight.x, BottomRight.y);
  252.                 break;
  253.            }
  254.  
  255.            case dtRectangle : {
  256.                 Image->Canvas->Rectangle(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
  257.                 break;
  258.            }
  259.  
  260.            case dtEllipse : {
  261.                 Image->Canvas->Ellipse(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y);
  262.                 break;
  263.            }
  264.  
  265.            case dtRoundRect : {
  266.                 Image->Canvas->RoundRect(TopLeft.x, TopLeft.y, BottomRight.x, BottomRight.y, (TopLeft.x - BottomRight.x)/2, (TopLeft.y - BottomRight.y)/2);
  267.                 break;
  268.            }
  269.     }
  270. }
  271. //---------------------------------------------------------------------
  272. void __fastcall TForm1::Print1Click(TObject *Sender)
  273. {
  274.     Printer()->BeginDoc();
  275.     Printer()->Canvas->CopyRect(Image->ClientRect, Image->Canvas, Image->ClientRect);
  276.     Printer()->EndDoc();
  277. }
  278. //---------------------------------------------------------------------
  279.