unit1.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, CheckLst, Types,
  6. StdCtrls, BGRAThemeCheckBox, BGRABitmap, BGRABitmapTypes, BGRATheme;
  7. type
  8. { TForm1 }
  9. TForm1 = class(TForm)
  10. BGRAThemeCheckBox1: TBGRAThemeCheckBox;
  11. CheckListBox1: TCheckListBox;
  12. procedure CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
  13. ARect: TRect; State: TOwnerDrawState);
  14. procedure FormCreate(Sender: TObject);
  15. private
  16. procedure DrawCheckBox(aCaption: string; State: TBGRAThemeButtonState;
  17. aFocused: boolean; Checked: boolean; ARect: TRect;
  18. ASurface: TBGRAThemeSurface);
  19. public
  20. end;
  21. var
  22. Form1: TForm1;
  23. implementation
  24. {$R *.lfm}
  25. { TForm1 }
  26. procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
  27. ARect: TRect; State: TOwnerDrawState);
  28. var
  29. surface: TBGRAThemeSurface;
  30. parentForm: TCustomForm;
  31. lclDPI: Integer;
  32. begin
  33. parentForm := GetParentForm(Control, False);
  34. if Assigned(parentForm) then
  35. lclDPI := parentForm.PixelsPerInch
  36. else lclDPI := Screen.PixelsPerInch;
  37. surface := TBGRAThemeSurface.Create(ARect, TCheckListBox(Control).Canvas, Control.GetCanvasScaleFactor, lclDPI);
  38. try
  39. DrawCheckBox(TCheckListBox(Control).Items[Index], btbsNormal, False, TCheckListBox(Control).Checked[Index], ARect, surface);
  40. finally
  41. surface.Free;
  42. end;
  43. end;
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46. CheckListBox1.AddItem('Red', nil);
  47. CheckListBox1.AddItem('Green', nil);
  48. CheckListBox1.AddItem('Blue', nil);
  49. CheckListBox1.AddItem('Alpha', nil);
  50. end;
  51. procedure TForm1.DrawCheckBox(aCaption: string; State: TBGRAThemeButtonState;
  52. aFocused: boolean; Checked: boolean; ARect: TRect; ASurface: TBGRAThemeSurface
  53. );
  54. var
  55. Style: TTextStyle;
  56. aColor: TBGRAPixel;
  57. aleft, atop, aright, abottom: integer;
  58. penWidth: single;
  59. begin
  60. with ASurface do
  61. begin
  62. DestCanvas.Font.Color := clBlack;
  63. case State of
  64. btbsHover: aColor := BGRA(0, 120, 215);
  65. btbsActive: aColor := BGRA(0, 84, 153);
  66. btbsDisabled:
  67. begin
  68. DestCanvas.Font.Color := clGray;
  69. aColor := BGRA(204, 204, 204);
  70. end;
  71. else {btbsNormal}
  72. aColor := BGRABlack;
  73. end;
  74. Bitmap.Fill(BGRAWhite);
  75. BitmapRect := ARect;
  76. penWidth := ASurface.ScaleForBitmap(10) / 10;
  77. aleft := round(penWidth);
  78. aright := Bitmap.Height-round(penWidth);
  79. atop := round(penWidth);
  80. abottom := Bitmap.Height-round(penWidth);
  81. Bitmap.RectangleAntialias(aleft-0.5+penWidth/2, atop-0.5+penWidth/2,
  82. aright-0.5-penWidth/2, abottom-0.5-penWidth/2,
  83. aColor, penWidth);
  84. aleft := round(penWidth*2);
  85. aright := Bitmap.Height-round(penWidth*2);
  86. atop := round(penWidth*2);
  87. abottom := Bitmap.Height-round(penWidth*2);
  88. if Checked then
  89. Bitmap.DrawPolyLineAntialias(Bitmap.ComputeBezierSpline(
  90. [BezierCurve(pointF(aleft + 2, atop + 3), PointF((aleft + aright - 1) / 2, abottom - 3)),
  91. BezierCurve(PointF((aleft + aright - 1) / 2, abottom - 3), PointF(
  92. (aleft + aright - 1) / 2, (atop * 2 + abottom - 1) / 3), PointF(aright - 2, atop))]),
  93. Color, penWidth*1.5);
  94. DrawBitmap;
  95. if aCaption <> '' then
  96. begin
  97. fillchar(Style, sizeof(Style), 0);
  98. Style.Alignment := taLeftJustify;
  99. Style.Layout := tlCenter;
  100. Style.Wordbreak := True;
  101. DestCanvas.TextRect(ARect,
  102. ARect.Height, 0, aCaption, Style);
  103. end;
  104. end;
  105. end;
  106. end.