graphwin.cpp
上传用户:lulishicai
上传日期:2010-03-01
资源大小:13202k
文件大小:10k
源码类别:

Delphi/CppBuilder

开发平台:

C++ Builder

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