浏览代码

Added checkbox

lainz 9 年之前
父节点
当前提交
747a295e24
共有 3 个文件被更改,包括 147 次插入8 次删除
  1. 119 3
      bgracustomdrawn.pas
  2. 17 0
      test/test_bgracustomdrawn/umain.lfm
  3. 11 5
      test/test_bgracustomdrawn/umain.pas

+ 119 - 3
bgracustomdrawn.pas

@@ -5,7 +5,7 @@ unit BGRACustomDrawn;
 interface
 
 uses
-  Classes, SysUtils, Types, FPCanvas, Graphics, Controls, Math, LazUTF8,
+  Classes, Types, FPCanvas, Graphics, Controls, Math, LazUTF8, Forms,
   { CustomDrawn }
   CustomDrawnControls, CustomDrawnDrawers, CustomDrawn_Common,
   { BGRABitmap }
@@ -35,6 +35,10 @@ type
 
   end;
 
+  TBCDCheckBox = class(TCDCheckBox)
+
+  end;
+
   { TBGRADrawer }
 
   TBGRADrawer = class(TCDDrawerCommon)
@@ -44,6 +48,9 @@ type
     procedure AssignFont(Bitmap: TBGRABitmap; Font: TFont);
   public
     constructor Create; override;
+    { General }
+    procedure DrawTickmark(ADest: TFPCustomCanvas; ADestPos: TPoint;
+      AState: TCDControlState); override;
     { Button }
     procedure DrawButton(ADest: TFPCustomCanvas; ADestPos: TPoint;
       ASize: TSize; AState: TCDControlState; AStateEx: TCDButtonStateEx); override;
@@ -65,6 +72,11 @@ type
     { Progress Bar }
     procedure DrawProgressBar(ADest: TCanvas; ASize: TSize;
       AState: TCDControlState; AStateEx: TCDProgressBarStateEx); override;
+    { CheckBox }
+    procedure DrawCheckBoxSquare(ADest: TCanvas; ADestPos: TPoint;
+      ASize: TSize; AState: TCDControlState; AStateEx: TCDControlStateEx); override;
+    procedure DrawCheckBox(ADest: TCanvas; ASize: TSize;
+      AState: TCDControlState; AStateEx: TCDControlStateEx); override;
   end;
 
 procedure Register;
@@ -74,7 +86,7 @@ implementation
 procedure Register;
 begin
   RegisterComponents('BGRA Custom Drawn', [TBCDButton, TBCDEdit,
-    TBCDStaticText, TBCDProgressBar, TBCDSpinEdit]);
+    TBCDStaticText, TBCDProgressBar, TBCDSpinEdit, TBCDCheckBox]);
 end;
 
 { TBCDProgressBar }
@@ -102,6 +114,42 @@ begin
   FRandSeed := randseed;
 end;
 
+procedure TBGRADrawer.DrawTickmark(ADest: TFPCustomCanvas; ADestPos: TPoint;
+  AState: TCDControlState);
+var
+  i: integer;
+  lSpacing5, lFirstLinesEnd, lSecondLinesEnd: integer;
+begin
+  if csfPartiallyOn in AState then
+    ADest.Pen.FPColor := TColorToFPColor($00AAAAAA)
+  else
+    ADest.Pen.FPColor := TColorToFPColor($00E5E5E5);
+  ADest.Pen.Style := psSolid;
+
+  if Screen.PixelsPerInch <= 125 then
+  begin
+    // 4 lines going down and to the right
+    for i := 0 to 3 do
+      ADest.Line(ADestPos.X + 1 + i, ADestPos.Y + 2 + i, ADestPos.X + 1 + i, ADestPos.Y + 5 + i);
+    // Now 5 lines going up and to the right
+    for i := 4 to 8 do
+      ADest.Line(ADestPos.X + 1 + i, ADestPos.Y + 2 + 6 - i, ADestPos.X + 1 + i, ADestPos.Y + 5 + 6 - i);
+    Exit;
+  end;
+
+  lSpacing5 := DPIAdjustment(5);
+  lFirstLinesEnd := DPIAdjustment(4) - 1;
+  lSecondLinesEnd := DPIAdjustment(9) - 1;
+
+  // 4 lines going down and to the right
+  for i := 0 to lFirstLinesEnd do
+    ADest.Line(ADestPos.X + 2 + i, ADestPos.Y + 2 + i, ADestPos.X + 2 + i, ADestPos.Y + lSpacing5 + i);
+  // Now 5 lines going up and to the right
+  for i := lFirstLinesEnd + 1 to lSecondLinesEnd do
+    ADest.Line(ADestPos.X + 2 + i, ADestPos.Y + 2 + lFirstLinesEnd * 2 - i,
+      ADestPos.X + 2 + i, ADestPos.Y + 2 + lFirstLinesEnd * 2 + lSpacing5 - i);
+end;
+
 procedure TBGRADrawer.DrawButton(ADest: TFPCustomCanvas; ADestPos: TPoint;
   ASize: TSize; AState: TCDControlState; AStateEx: TCDButtonStateEx);
 var
@@ -409,7 +457,7 @@ procedure TBGRADrawer.DrawProgressBar(ADest: TCanvas; ASize: TSize;
       ApplyLightness(lCol, 26000), ApplyLightness(lCol, 18000),
       gdVertical, gdVertical, gdVertical, 0.53);
 
-    InflateRect(bounds, -1, -1);
+    //InflateRect(bounds, -1, -1);
 
     DoubleGradientAlphaFill(Bitmap, bounds,
       ApplyLightness(lCol, 28000), ApplyLightness(lCol, 22000),
@@ -469,6 +517,74 @@ begin
   Bitmap.Free;
 end;
 
+procedure TBGRADrawer.DrawCheckBoxSquare(ADest: TCanvas; ADestPos: TPoint;
+  ASize: TSize; AState: TCDControlState; AStateEx: TCDControlStateEx);
+var
+  lHalf, lSquareHalf, lSquareHeight: integer;
+  Bitmap: TBGRABitmap;
+  r: TRect;
+begin
+  Bitmap := TBGRABitmap.Create(ASize.cx, ASize.cy);
+  lHalf := ASize.cy div 2;
+  lSquareHalf := GetMeasures(TCDCHECKBOX_SQUARE_HALF_HEIGHT);
+  lSquareHeight := GetMeasures(TCDCHECKBOX_SQUARE_HEIGHT);
+  r := Bounds(1, lHalf - lSquareHalf, lSquareHeight, lSquareHeight);
+  Bitmap.Rectangle(r.Left, r.Top, r.Right, r.Bottom, BGRA(48, 48, 48),
+    BGRA(84, 84, 84), dmSet);
+  Bitmap.SetHorizLine(r.Left + 1, r.Top + 1, r.Right - 2, BGRA(130, 130, 130));
+  Bitmap.Draw(TCanvas(ADest), ADestPos.x, ADestPos.y, False);
+  Bitmap.Free;
+end;
+
+procedure TBGRADrawer.DrawCheckBox(ADest: TCanvas; ASize: TSize;
+  AState: TCDControlState; AStateEx: TCDControlStateEx);
+var
+  lColor: TColor;
+  lSquareHeight, lValue3: integer;
+  lTextHeight, lTextY: integer;
+begin
+  lSquareHeight := GetMeasures(TCDCHECKBOX_SQUARE_HEIGHT);
+  lValue3 := DPIAdjustment(3);
+
+  // Background
+  lColor := $00535353; //AStateEx.ParentRGBColor;
+  ADest.Brush.Color := lColor;
+  ADest.Brush.Style := bsSolid;
+  ADest.Pen.Style := psClear;
+  ADest.FillRect(0, 0, ASize.cx, ASize.cy);
+
+  // The checkbox item itself
+  DrawCheckBoxSquare(ADest, Point(0, 0), ASize, AState, AStateEx);
+
+  // The Tickmark
+  if (csfOn in AState) or (csfPartiallyOn in AState) then
+    DrawTickmark(ADest, Point(lValue3, ASize.cy div 2 -
+      GetMeasures(TCDCHECKBOX_SQUARE_HALF_HEIGHT) + lValue3), AState);
+
+  // The text selection
+  //if csfHasFocus in AState then
+  //  DrawFocusRect(ADest, Point(lSquareHeight+4, 0),
+  //    Size(ASize.cx-lSquareHeight-4, ASize.cy));
+
+  // Now the text
+  ADest.Brush.Style := bsClear;
+  ADest.Pen.Style := psClear;
+  ADest.Font.Assign(AStateEx.Font);
+  if csfEnabled in AState then
+    ADest.Font.Color := $00E5E5E5
+  else
+    ADest.Font.Color := $00AAAAAA;
+  lTextHeight := ADest.TextHeight(cddTestStr);
+  // put the text in the center
+  if lSquareHeight > lTextHeight then
+    lTextY := (lSquareHeight - ADest.TextHeight(cddTestStr)) div 2
+  else
+    lTextY := 0;
+  lTextY := Max(0, lTextY - 1);
+
+  ADest.TextOut(lSquareHeight + 5, lTextY, AStateEx.Caption);
+end;
+
 initialization
   RegisterDrawer(TBGRADrawer.Create, dsCommon);
 

+ 17 - 0
test/test_bgracustomdrawn/umain.lfm

@@ -157,6 +157,23 @@ object frmMain: TfrmMain
       MaxValue = 100
       Value = 1
     end
+    object BCDCheckBox1: TBCDCheckBox
+      Left = 8
+      Height = 20
+      Top = 152
+      Width = 122
+      DrawStyle = dsDefault
+      Caption = 'BCDCheckBox1'
+    end
+    object BCDCheckBox2: TBCDCheckBox
+      Left = 8
+      Height = 20
+      Top = 176
+      Width = 122
+      AllowGrayed = True
+      DrawStyle = dsDefault
+      Caption = 'BCDCheckBox2'
+    end
   end
   object BCToolBar1: TBCToolBar
     Left = 0

+ 11 - 5
test/test_bgracustomdrawn/umain.pas

@@ -5,9 +5,11 @@ unit umain;
 interface
 
 uses
-  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
-  ComCtrls, BGRACustomDrawn, BCPanel, BCToolBar, BGRABitmap, BGRABitmapTypes, BCTypes,
-  Types;
+  Forms, Controls, Graphics, ExtCtrls, ComCtrls, Types,
+  { BGRABitmap }
+  BGRABitmap, BGRABitmapTypes,
+  { BGRAControls }
+  BGRACustomDrawn, BCPanel, BCToolBar;
 
 type
 
@@ -24,6 +26,8 @@ type
     BCDStaticText2: TBCDStaticText;
     BCPanel1: TBCPanel;
     BCToolBar1: TBCToolBar;
+    BCDCheckBox1: TBCDCheckBox;
+    BCDCheckBox2: TBCDCheckBox;
     Timer1: TTimer;
     ToolButton1: TToolButton;
     ToolButton2: TToolButton;
@@ -91,8 +95,10 @@ begin
         begin
           { Button Hovered }
           Bitmap.GradientFill(0, 0, Sender.Width, Sender.Height, BGRA(132, 132, 132),
-            BGRA(109, 109, 109), gtLinear, PointF(0, 0), PointF(0, Sender.Height), dmSet);
-          Bitmap.Rectangle(0, 0, Sender.Width, Sender.Height - 1, BGRA(48, 48, 48), dmSet);
+            BGRA(109, 109, 109), gtLinear, PointF(0, 0),
+            PointF(0, Sender.Height), dmSet);
+          Bitmap.Rectangle(0, 0, Sender.Width, Sender.Height - 1,
+            BGRA(48, 48, 48), dmSet);
           Bitmap.SetHorizLine(1, 1, Sender.Width - 2, BGRA(160, 160, 160));
           Bitmap.SetHorizLine(0, Sender.Height - 1, Sender.Width - 1, BGRA(83, 83, 83));
         end