home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WDOCDEMO.ZIP / PEN.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  5.0 KB  |  207 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Demo                           }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Pen;
  9.  
  10. {$R PEN.RES}
  11.  
  12. interface
  13.  
  14. uses WinTypes, Objects, OWindows, ODialogs;
  15.  
  16. type
  17.   TPenData = record
  18.     XWidth: array[0..6] of Char;
  19.     ColorArray: array[0..7] of Word;
  20.     StyleArray: array[0..5] of Word;
  21.   end;
  22.  
  23.   PPenDialog = ^TPenDialog;
  24.   TPenDialog = object(TDialog)
  25.     constructor Init(AParent: PWindowsObject; AName: PChar);
  26.   end;
  27.  
  28.   PPen = ^TPen;
  29.   TPen = object(TObject)
  30.     Width, Style: Integer;
  31.     Color: Longint;
  32.     constructor Init(AStyle, AWidth:Integer; AColor: Longint);
  33.     constructor InitLike(APen: PPen);
  34.     destructor Done; virtual;
  35.     constructor Load(var S: TStream);
  36.     procedure ChangePen;
  37.     procedure Delete;
  38.     procedure Select(ADC: HDC);
  39.     procedure SetAttributes(AStyle, AWidth: Integer; AColor: Longint);
  40.     procedure Store(var S: TStream);
  41.   private
  42.     PenHandle, OldPen: HPen;
  43.     TheDC: HDC;
  44.     PenData: TPenData;
  45.   end;
  46.  
  47. const
  48.   RPen: TStreamRec = (
  49.     ObjType: 202;
  50.     VmtLink: Ofs(TypeOf(TPen)^);
  51.     Load: @TPen.Load;
  52.     Store: @TPen.Store);
  53.  
  54. implementation
  55.  
  56. uses Strings, WinProcs;
  57.  
  58. const
  59.   ColorAttr: array[0..7] of Longint =
  60.     (0, $FF0000, $FF00, $FFFF00, $0000FF, $FF00FF, $00FFFF, $FFFFFF);
  61.  
  62. function GetColorAttr(ARec: TPenData): Longint;
  63. var
  64.   i: Integer;
  65. begin
  66.   for i := 0 to 7 do
  67.     if ARec.ColorArray[i] = bf_Checked then GetColorAttr := ColorAttr[i];
  68. end;
  69.  
  70. procedure SetColorAttr(var ARec: TPenData; AColor: Longint);
  71. var
  72.   i: Integer;
  73. begin
  74.   for i := 0 to 7 do
  75.     if ColorAttr[i] = AColor then
  76.       ARec.ColorArray[i] := bf_Checked
  77.     else ARec.ColorArray[i] := bf_Unchecked;
  78. end;
  79.  
  80. function GetStyle(ARec: TPenData): Longint;
  81. var
  82.   i: Integer;
  83. begin
  84.   for i := 0 to 5 do
  85.     if ARec.StyleArray[i] = bf_Checked then GetStyle := i;
  86. end;
  87.  
  88. procedure SetStyle(var ARec: TPenData; AStyle: Integer);
  89. var
  90.   i: Integer;
  91. begin
  92.   for i := 0 to 5 do
  93.     if i = AStyle then ARec.StyleArray[i] := bf_Checked
  94.   else ARec.StyleArray[i] := bf_Unchecked;
  95. end;
  96.  
  97. {--------------------------------------------------}
  98. { TPenDialog's method implementations:             }
  99. {--------------------------------------------------}
  100.  
  101. constructor TPenDialog.Init(AParent: PWindowsObject; AName: PChar);
  102. var
  103.   AControl: PControl;
  104.   i: Integer;
  105. begin
  106.   inherited Init(AParent, AName);
  107.   AControl := New(PEdit, InitResource(@Self, 1099, 7));
  108.   for i := 0 to 7 do
  109.     AControl := New(PRadioButton, InitResource(@Self, 1100 + i));
  110.   for i := 0 to 5 do
  111.     AControl := New(PRadioButton, InitResource(@Self, 1200 + i));
  112. end;
  113.  
  114.  
  115. {--------------------------------------------------}
  116. { TPen's method implementations:                   }
  117. {--------------------------------------------------}
  118.  
  119. constructor TPen.Init(AStyle, AWidth: Integer; AColor: Longint);
  120. begin
  121.   inherited Init;
  122.   PenHandle := 0;
  123.   SetAttributes(AStyle, AWidth, AColor);
  124.   FillChar(PenData, SizeOf(PenData), #0);
  125. end;
  126.  
  127. constructor TPen.InitLike(APen: PPen);
  128. begin
  129.   inherited Init;
  130.   PenHandle := 0;
  131.   SetAttributes(APen^.Style, APen^.Width, APen^.Color);
  132.   FillChar(PenData, SizeOf(PenData), #0);
  133. end;
  134.  
  135. destructor TPen.Done;
  136. begin
  137.   Delete;
  138.   inherited Done;
  139. end;
  140.  
  141. constructor TPen.Load(var S: TStream);
  142. begin
  143.   S.Read(Style, SizeOf(Style));
  144.   S.Read(Width, SizeOf(Width));
  145.   S.Read(Color, SizeOf(Color));
  146.   PenHandle := 0;
  147.   FillChar(PenData, SizeOf(PenData), 0);
  148. end;
  149.  
  150. procedure TPen.ChangePen;
  151. var
  152.   PenDlg: PPenDialog;
  153.   TempWidth, ErrorPos: Integer;
  154.   PenDlgName: PChar;
  155. begin
  156.   if BWCCClassNames then PenDlgName := StrNew('PenDlgB')
  157.   else PenDlgName := StrNew('PenDlg');
  158.   SetColorAttr(PenData, Color);
  159.   SetStyle(PenData, Style);
  160.   wvsprintf(PenData.XWidth, '%d', Width);
  161.   PenDlg := New(PPenDialog, Init(Application^.MainWindow, PenDlgName));
  162.   PenDlg^.TransferBuffer := @PenData;
  163.   if Application^.ExecDialog(PenDlg) <> idCancel then
  164.   begin
  165.     Val(PenData.XWidth, TempWidth, ErrorPos);
  166.     if ErrorPos = 0 then
  167.       SetAttributes(GetStyle(PenData), TempWidth, GetColorAttr(PenData));
  168.   end;
  169.   StrDispose(PenDlgName);
  170. end;
  171.  
  172. procedure TPen.Delete;
  173. begin
  174.   if PenHandle <> 0 then
  175.   begin
  176.     SelectObject(TheDC, OldPen);
  177.     DeleteObject(PenHandle);
  178.   end;
  179.   PenHandle := 0;
  180. end;
  181.  
  182. procedure TPen.Select(ADC: HDC);
  183. begin
  184.   if PenHandle <> 0 then Delete;
  185.   TheDC := ADC;
  186.   PenHandle := CreatePen(Style, Width, Color);
  187.   OldPen := SelectObject(TheDC, PenHandle);
  188. end;
  189.  
  190. procedure TPen.SetAttributes(AStyle, AWidth: Integer; AColor: Longint);
  191. begin
  192.   Style := AStyle;
  193.   Width := AWidth;
  194.   Color := AColor;
  195. end;
  196.  
  197. procedure TPen.Store(var S: TStream);
  198. begin
  199.   S.Write(Style, SizeOf(Style));
  200.   S.Write(Width, SizeOf(Width));
  201.   S.Write(Color, SizeOf(Color));
  202. end;
  203.  
  204. begin
  205.   RegisterType(RPen);
  206. end.
  207.