CnAAFontDialog.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2018 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnAAFontDialog;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:CnPack 控件包
  24. * 单元名称:平滑特效字体对话框控件 TCnAAFontDialog 单元
  25. * 单元作者:CnPack 开发组 周劲羽 (zjy@cnpack.org)
  26. * 移植:e-
  27. * 开发平台:PWin2000Pro + Delphi 5.01
  28. * 兼容测试:PWin9X/2000/XP + Delphi 5/6/7 + C++Build 5/6
  29. * 备  注:
  30. * 单元标识:$Id$
  31. * 最后更新:2003.03.22
  32. * 移植日期:2006.08.18
  33. ================================================================================
  34. |</PRE>}
  35. interface
  36. {$I CnPack.inc}
  37. uses
  38. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  39. CnAAFont, CnAACtrls, ExtCtrls, StdCtrls, ExtDlgs, CnSpin;
  40. type
  41. { TCnAAFontDlg }
  42. TCnAAFontDlg = class(TForm)
  43. Panel1: TPanel;
  44. gbShadow: TGroupBox;
  45. cbShadow: TCheckBox;
  46. Label1: TLabel;
  47. seShadowBlur: TCnSpinEdit;
  48. Label2: TLabel;
  49. seShadowAlpha: TCnSpinEdit;
  50. Label3: TLabel;
  51. seOffsetX: TCnSpinEdit;
  52. Label4: TLabel;
  53. seOffsetY: TCnSpinEdit;
  54. spShadow: TShape;
  55. Label5: TLabel;
  56. gbGradual: TGroupBox;
  57. spStartColor: TShape;
  58. Label10: TLabel;
  59. cbGradual: TCheckBox;
  60. spEndColor: TShape;
  61. Label6: TLabel;
  62. rbLeftToRight: TRadioButton;
  63. rbRightToLeft: TRadioButton;
  64. rbTopToBottom: TRadioButton;
  65. rbCenterToLR: TRadioButton;
  66. gbTexture: TGroupBox;
  67. cbTexture: TCheckBox;
  68. rbTile: TRadioButton;
  69. rbStretched: TRadioButton;
  70. rbCenter: TRadioButton;
  71. rbNormal: TRadioButton;
  72. btnOpenPic: TButton;
  73. gbOther: TGroupBox;
  74. cbOutline: TCheckBox;
  75. Label7: TLabel;
  76. seBlur: TCnSpinEdit;
  77. Label8: TLabel;
  78. seAlpha: TCnSpinEdit;
  79. Label9: TLabel;
  80. seNoise: TCnSpinEdit;
  81. btnFont: TButton;
  82. btnOK: TButton;
  83. btnCancel: TButton;
  84. OpenPictureDialog: TOpenPictureDialog;
  85. FontDialog: TFontDialog;
  86. ColorDialog: TColorDialog;
  87. btnClearPic: TButton;
  88. Label11: TLabel;
  89. seSpray: TCnSpinEdit;
  90. rbBottomToTop: TRadioButton;
  91. rbCenterToTB: TRadioButton;
  92. Label12: TLabel;
  93. seAngle: TCnSpinEdit;
  94. cbHorzMirror: TCheckBox;
  95. cbVertMirror: TCheckBox;
  96. procedure btnFontClick(Sender: TObject);
  97. procedure cbShadowClick(Sender: TObject);
  98. procedure seShadowBlurClick(Sender: TObject);
  99. procedure spShadowMouseDown(Sender: TObject; Button: TMouseButton;
  100. Shift: TShiftState; X, Y: Integer);
  101. procedure btnOpenPicClick(Sender: TObject);
  102. procedure btnClearPicClick(Sender: TObject);
  103. procedure FormCreate(Sender: TObject);
  104. private
  105. { Private declarations }
  106. FUpdating: Boolean;
  107. AALabel: TCnAALabel;
  108. procedure SetEffect(const Value: TCnAAEffect);
  109. function GetTestFont: TFont;
  110. procedure SetTestFont(const Value: TFont);
  111. function GetEffect: TCnAAEffect;
  112. procedure SetEffectToControls;
  113. procedure GetEffectFromControls;
  114. procedure UpdateEffect;
  115. public
  116. { Public declarations }
  117. property Effect: TCnAAEffect read GetEffect write SetEffect;
  118. property TestFont: TFont read GetTestFont write SetTestFont;
  119. end;
  120. { TCnAAFontDialog }
  121. TCnAAFontDialog = class(TComponent)
  122. {* 平滑特效字体对话框组件,使用方法同普通对话框}
  123. private
  124. FOnClose: TNotifyEvent;
  125. FOnShow: TNotifyEvent;
  126. FEffect: TCnAAEffect;
  127. FFont: TFont;
  128. FAllowChangeFont: Boolean;
  129. procedure SetEffect(const Value: TCnAAEffect);
  130. procedure SetTestFont(const Value: TFont);
  131. protected
  132. procedure DoShow; virtual;
  133. procedure DoClose; virtual;
  134. public
  135. constructor Create(AOwner: TComponent); override;
  136. {* 类构造器}
  137. destructor Destroy; override;
  138. {* 类析构器}
  139. procedure Assign(Source: TPersistent); override;
  140. {* 对象赋值方法}
  141. function Execute: Boolean;
  142. {* 打开对话框,返回执行结果}
  143. published
  144. property Effect: TCnAAEffect read FEffect write SetEffect;
  145. {* 平滑特效参数属性}
  146. property Font: TFont read FFont write SetTestFont;
  147. {* 字体属性}
  148. property AllowChangeFont: Boolean read FAllowChangeFont
  149. write FAllowChangeFont default False;
  150. {* 是否允许更改字体,如果为True,将允许用户更改Font属性。如果为False,则不
  151. 显示“字体”按钮。}
  152. property OnClose: TNotifyEvent read FOnClose write FOnClose;
  153. {* 对话框关闭事件}
  154. property OnShow: TNotifyEvent read FOnShow write FOnShow;
  155. {* 对话框显示事件}
  156. end;
  157. implementation
  158. {$R *.DFM}
  159. { TCnAAFontDlg }
  160. procedure TCnAAFontDlg.FormCreate(Sender: TObject);
  161. begin
  162. FUpdating := False;
  163. Panel1.DoubleBuffered := True;
  164. AALabel := TCnAALabel.Create(Self);
  165. with AALabel do
  166. begin
  167. Parent := Panel1;
  168. AutoSize := False;
  169. Align := alClient;
  170. Font.Charset := GB2312_CHARSET;
  171. Font.Color := clWindowText;
  172. Font.Height := -19;
  173. Font.Name := '楷体_GB2312';
  174. Font.Style := [];
  175. Effect.Layout := tlCenter;
  176. Effect.Alignment := taCenter;
  177. Effect.BackColor := clWhite;
  178. Caption := '平滑特效字体 AAFont';
  179. end;
  180. end;
  181. function TCnAAFontDlg.GetEffect: TCnAAEffect;
  182. begin
  183. Result := AALabel.Effect.FontEffect;
  184. end;
  185. function TCnAAFontDlg.GetTestFont: TFont;
  186. begin
  187. Result := AALabel.Font;
  188. end;
  189. procedure TCnAAFontDlg.SetEffect(const Value: TCnAAEffect);
  190. begin
  191. AALabel.Effect.FontEffect := Value;
  192. SetEffectToControls;
  193. end;
  194. procedure TCnAAFontDlg.SetTestFont(const Value: TFont);
  195. begin
  196. AALabel.Font := Value;
  197. end;
  198. procedure TCnAAFontDlg.GetEffectFromControls;
  199. begin
  200. with AALabel.Effect.FontEffect do
  201. begin
  202. Shadow.Enabled := cbShadow.Checked;
  203. Shadow.Blur := seShadowBlur.Value;
  204. Shadow.Color := spShadow.Brush.Color;
  205. Shadow.Alpha := seShadowAlpha.Value;
  206. Shadow.OffsetX := seOffsetX.Value;
  207. Shadow.OffsetY := seOffsetY.Value;
  208. Gradual.Enabled := cbGradual.Checked;
  209. Gradual.StartColor := spStartColor.Brush.Color;
  210. Gradual.EndColor := spEndColor.Brush.Color;
  211. if rbLeftToRight.Checked then
  212. Gradual.Style := gsLeftToRight
  213. else if rbRightToLeft.Checked then
  214. Gradual.Style := gsRightToLeft
  215. else if rbTopToBottom.Checked then
  216. Gradual.Style := gsTopToBottom
  217. else if rbBottomToTop.Checked then
  218. Gradual.Style := gsBottomToTop
  219. else if rbCenterToLR.Checked then
  220. Gradual.Style := gsCenterToLR
  221. else
  222. Gradual.Style := gsCenterToTB;
  223. Texture.Enabled := cbTexture.Checked;
  224. if rbTile.Checked then
  225. Texture.Mode := tmTiled
  226. else if rbStretched.Checked then
  227. Texture.Mode := tmStretched
  228. else if rbCenter.Checked then
  229. Texture.Mode := tmCenter
  230. else
  231. Texture.Mode := tmNormal;
  232. Blur := seBlur.Value;
  233. Alpha := seAlpha.Value;
  234. Angle := seAngle.Value;
  235. Noise := seNoise.Value;
  236. Spray := seSpray.Value;
  237. Outline := cbOutline.Checked;
  238. HorzMirror := cbHorzMirror.Checked;
  239. VertMirror := cbVertMirror.Checked;
  240. end;
  241. end;
  242. procedure TCnAAFontDlg.SetEffectToControls;
  243. begin
  244. FUpdating := True;
  245. try
  246. with AALabel.Effect.FontEffect do
  247. begin
  248. cbShadow.Checked := Shadow.Enabled;
  249. seShadowBlur.Value := Shadow.Blur;
  250. spShadow.Brush.Color := Shadow.Color;
  251. seShadowAlpha.Value := Shadow.Alpha;
  252. seOffsetX.Value := Shadow.OffsetX;
  253. seOffsetY.Value := Shadow.OffsetY;
  254. cbGradual.Checked := Gradual.Enabled;
  255. spStartColor.Brush.Color := Gradual.StartColor;
  256. spEndColor.Brush.Color := Gradual.EndColor;
  257. rbLeftToRight.Checked := Gradual.Style = gsLeftToRight;
  258. rbRightToLeft.Checked := Gradual.Style = gsRightToLeft;
  259. rbTopToBottom.Checked := Gradual.Style = gsTopToBottom;
  260. rbBottomToTop.Checked := Gradual.Style = gsBottomToTop;
  261. rbCenterToLR.Checked := Gradual.Style = gsCenterToLR;
  262. rbCenterToTB.Checked := Gradual.Style = gsCenterToTB;
  263. cbTexture.Checked := Texture.Enabled;
  264. rbTile.Checked := Texture.Mode = tmTiled;
  265. rbStretched.Checked := Texture.Mode = tmStretched;
  266. rbCenter.Checked := Texture.Mode = tmCenter;
  267. rbNormal.Checked := Texture.Mode = tmNormal;
  268. seBlur.Value := Blur;
  269. seAlpha.Value := Alpha;
  270. seAngle.Value := Angle;
  271. seNoise.Value := Noise;
  272. seSpray.Value := Spray;
  273. cbOutline.Checked := Outline;
  274. cbHorzMirror.Checked := HorzMirror;
  275. cbVertMirror.Checked := VertMirror;
  276. cbShadowClick(cbShadow);
  277. cbShadowClick(cbGradual);
  278. cbShadowClick(cbTexture);
  279. end;
  280. finally
  281. FUpdating := False;
  282. end;
  283. end;
  284. procedure TCnAAFontDlg.btnFontClick(Sender: TObject);
  285. begin
  286. FontDialog.Font.Assign(TestFont);
  287. if FontDialog.Execute then
  288. TestFont := FontDialog.Font;
  289. end;
  290. procedure TCnAAFontDlg.cbShadowClick(Sender: TObject);
  291. var
  292. GroupBox: TGroupBox;
  293. i: Integer;
  294. begin
  295. if (Sender is TCheckBox) and (TCheckBox(Sender).Parent is TGroupBox) then
  296. begin
  297. GroupBox := TGroupBox(TCheckBox(Sender).Parent);
  298. for i := 0 to GroupBox.ControlCount - 1 do
  299. if GroupBox.Controls[i] <> Sender then
  300. GroupBox.Controls[i].Enabled := TCheckBox(Sender).Checked;
  301. end;
  302. UpdateEffect;
  303. end;
  304. procedure TCnAAFontDlg.UpdateEffect;
  305. begin
  306. if FUpdating then Exit;
  307. FUpdating := True;
  308. AALabel.BeginUpdate;
  309. try
  310. GetEffectFromControls;
  311. finally
  312. AALabel.EndUpdate;
  313. AALabel.Changed;
  314. FUpdating := False;
  315. end;
  316. end;
  317. procedure TCnAAFontDlg.seShadowBlurClick(Sender: TObject);
  318. begin
  319. UpdateEffect;
  320. end;
  321. procedure TCnAAFontDlg.spShadowMouseDown(Sender: TObject;
  322. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  323. begin
  324. if (Sender is TShape) and (Button = mbLeft) then
  325. begin
  326. ColorDialog.Color := TShape(Sender).Brush.Color;
  327. if ColorDialog.Execute then
  328. begin
  329. TShape(Sender).Brush.Color := ColorDialog.Color;
  330. UpdateEffect;
  331. end;
  332. end;
  333. end;
  334. procedure TCnAAFontDlg.btnOpenPicClick(Sender: TObject);
  335. begin
  336. if OpenPictureDialog.Execute then
  337. begin
  338. AALabel.Effect.FontEffect.Texture.Picture.LoadFromFile(OpenPictureDialog.FileName);
  339. UpdateEffect;
  340. end;
  341. end;
  342. procedure TCnAAFontDlg.btnClearPicClick(Sender: TObject);
  343. begin
  344. AALabel.Effect.FontEffect.Texture.Picture := nil;
  345. UpdateEffect;
  346. end;
  347. { TCnAAFontDialog }
  348. procedure TCnAAFontDialog.Assign(Source: TPersistent);
  349. begin
  350. if Source is TCnAAFontDialog then
  351. begin
  352. FFont.Assign(TCnAAFontDialog(Source).FFont);
  353. FEffect.Assign(TCnAAFontDialog(Source).FEffect);
  354. end
  355. else
  356. inherited;
  357. end;
  358. constructor TCnAAFontDialog.Create(AOwner: TComponent);
  359. begin
  360. inherited;
  361. FAllowChangeFont := False;
  362. FFont := TFont.Create;
  363. FEffect := TCnAAEffect.Create(nil);
  364. end;
  365. destructor TCnAAFontDialog.Destroy;
  366. begin
  367. FEffect.Free;
  368. FFont.Free;
  369. inherited;
  370. end;
  371. procedure TCnAAFontDialog.DoClose;
  372. begin
  373. if Assigned(FOnClose) then
  374. FOnClose(Self);
  375. end;
  376. procedure TCnAAFontDialog.DoShow;
  377. begin
  378. if Assigned(FOnShow) then
  379. FOnShow(Self);
  380. end;
  381. function TCnAAFontDialog.Execute: Boolean;
  382. begin
  383. with TCnAAFontDlg.Create(Self) do
  384. try
  385. Effect := Self.FEffect;
  386. btnFont.Visible := AllowChangeFont;
  387. if AllowChangeFont then
  388. TestFont := Self.FFont;
  389. Self.DoShow;
  390. Result := ShowModal = mrOk;
  391. if Result then
  392. begin
  393. Self.FEffect.Assign(Effect);
  394. if AllowChangeFont then
  395. Self.FFont.Assign(TestFont);
  396. end;
  397. Self.DoClose;
  398. finally
  399. Free;
  400. end;
  401. end;
  402. procedure TCnAAFontDialog.SetEffect(const Value: TCnAAEffect);
  403. begin
  404. FEffect.Assign(Value);
  405. end;
  406. procedure TCnAAFontDialog.SetTestFont(const Value: TFont);
  407. begin
  408. FFont.Assign(Value);
  409. end;
  410. end.