簡體   English   中英

如何在Delphi中更改TCalendar組件中的單元格顏色?

[英]How to change cell color in a TCalendar Component in Delphi?

我需要在適用於Android和iOS的應用程序上的TCalendar組件中更改某些單元格的顏色。 我正在使用Delphi Seattle 10.有沒有辦法做到這一點?

這適用於Delphi XE5。 不幸的是,我沒有Delphi 10檢查代碼。

type
  TMyCalendar = class(TCalendar)
  private
    FSelectedDays: set of byte;
    procedure ApplyStyle; override;
  end;

...

{ TMyCalendar }

procedure TMyCalendar.ApplyStyle;
var
  i: word;
  LB: TListBox;
begin
  inherited;
  if FSelectedDays <> [] then
  begin
    LB := TListBox(TStyleObject(Children.Items[0]).Children.Items
      [TStyleObject(Children.Items[0]).Children.Count - 1]);
    for i := 0 to LB.Count - 1 do
      if (Assigned(LB.ItemByIndex(i))) and
        (StrToInt(LB.ItemByIndex(i).Text) in FSelectedDays) then
      begin
        LB.ItemByIndex(i).StyledSettings := LB.ItemByIndex(i).StyledSettings -
          [TStyledSetting.ssStyle];
        LB.ItemByIndex(i).Font.Style := LB.ItemByIndex(i).Font.Style +
          [TFontStyle.fsBold];
        With TRectangle.Create(LB.ItemByIndex(i)) do
        begin
          Parent := LB.ItemByIndex(i);
          Align := TAlignLayout.alClient;
          Fill.Color := TAlphaColorRec.Red;
          Opacity := 0.5;
        end;
      end;
  end;
end;

然后創建一個TMyCalendar類的實例:

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    MyCalendar: TMyCalendar;
  end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MyCalendar := TMyCalendar.Create(Self);
  MyCalendar.Parent := Self;
  MyCalendar.Position.X := 1;
  MyCalendar.Position.Y := 1;
  MyCalendar.FSelectedDays := [9, 11]; // <-set other days here and check the month
end;

附加

還有另一種方法可以訪問私有變量FDays,它可以重復出現該月的日期列表。 您聲明一個class helper在屬性Days公開它:

  TMyCalendarHelper = class helper for TCalendar
    function GetDays: TListBox;
    procedure SetDays(const Value: TListBox);
    property Days: TListBox read GetDays write SetDays;
  end;

...

{ TMyCalendarHelper }

function TMyCalendarHelper.GetDays: TListBox;
begin
  result := Self.FDays;
end;

procedure TMyCalendarHelper.SetDays(const Value: TListBox);
begin
  Self.FDays := Value;
end;

然后在calss后代中,您可以使用Days屬性控制此ListBox及其項目。

procedure TMyCalendar.ApplyStyle;
var
  i: word;
//  LB: TListBox;//<-you do not need it any more
begin
  inherited;
  if FSelectedDays <> [] then
  begin
//    LB := TListBox(TStyleObject(Children.Items[0]).Children.Items//<-you do not need it
//      [TStyleObject(Children.Items[0]).Children.Count - 1]);//<-you do not need it
    for i := 0 to Days.Count - 1 do
      if (Assigned(Days.ItemByIndex(i))) and
        (StrToInt(Days.ItemByIndex(i).Text) in FSelectedDays) then
      begin
        Days.ItemByIndex(i).StyledSettings := Days.ItemByIndex(i).StyledSettings -
          [TStyledSetting.ssStyle];
        Days.ItemByIndex(i).Font.Style := Days.ItemByIndex(i).Font.Style +
          [TFontStyle.fsBold];
        //Do other things you want with Days.ItemByIndex(i)

附錄2有可能糾正日期的繪制方式。

  TMyCalendar = class(TCalendar)
  private
    FSelectedDays: set of byte;
    procedure PaintChildren; override;
  end;
procedure TMyCalendar.PaintChildren;
var
  i: word;
  TMPC: TAlphaColor;
  R: TRectF;
begin
  inherited;
  if FSelectedDays <> [] then
  begin
    for i := 0 to Days.Count - 1 do
      if (Assigned(Days.ItemByIndex(i))) and
        (StrToInt(Days.ItemByIndex(i).Text) in FSelectedDays) then
      begin
        TMPC := Days.ItemByIndex(i).Canvas.Fill.Color;
        R := Days.ItemByIndex(i).AbsoluteRect;
        R.Inflate(Position.X, Position.Y, -Position.X, -Position.Y);
        Days.ItemByIndex(i).Canvas.BeginScene;
        Days.ItemByIndex(i).Canvas.Fill.Color := TAlphaColorRec.Red;
        Days.ItemByIndex(i).Canvas.FillRect(R, 0, 0, [], 0.5);
        Days.ItemByIndex(i).Canvas.EndScene;
        Days.ItemByIndex(i).Canvas.Fill.Color := TMPC;
      end;
  end;
end; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM