Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#15

AW: [StringGrid] berechnen d. Zellenbreite nach resize

  Alt 18. Aug 2012, 12:33
Hallo Buddelflink,
wie hast du das gemacht dass einen Kalender in einem Stringgrid angezeigt wird?
Kann du mal bitte ein Beispiel zeigen!
danke.
Nur mal so für Spaß, wie man in ein StringGrid - mit wiederverwendbarem Code - einen Kalender reinschmeißt
Form:
Delphi-Quellcode:
unit MainView;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, uStringGridCalendar,
  Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class( TForm )
    StringGrid1 : TStringGrid;
    Button1 : TButton;
    Button2 : TButton;
    Panel1: TPanel;
    procedure FormCreate( Sender : TObject );
    procedure FormDestroy( Sender : TObject );
    procedure Button1Click( Sender : TObject );
    procedure Button2Click( Sender : TObject );
  private
    { Private-Deklarationen }
    FCal : TStringGridCalendar;
  public
    { Public-Deklarationen }
  end;

var
  Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click( Sender : TObject );
begin
  FCal.DoIncYear( 1 );
end;

procedure TForm1.Button2Click( Sender : TObject );
begin
  FCal.DoIncYear( -1 );
end;

procedure TForm1.FormCreate( Sender : TObject );
begin
  FCal := TStringGridCalendar.Create( StringGrid1 );
end;

procedure TForm1.FormDestroy( Sender : TObject );
begin
  FCal.Free;
end;

end.
und die Klasse, die das gesamte Handling übernimmt:
Delphi-Quellcode:
unit uStringGridCalendar;

interface

uses
  Vcl.Grids;

type
  TStringGridCalendar = class
  private
    FGrid : TStringGrid;
    FSelDate : TDate;
    procedure SetSelDate( const Value : TDate );
  protected
    procedure GridSelectCell( Sender : TObject; ACol, ARow : Integer; var CanSelect : Boolean );
    procedure DoGridDataFill;
    function TryGetDateFromGrid( ACol, ARow : Integer; out aDate : TDate ) : Boolean;
    function GetDateFromGrid( ACol, ARow : Integer ) : TDate;
  public
    constructor Create( aStringGrid : TStringGrid );
    property SelDate : TDate read FSelDate write SetSelDate;
    procedure DoIncYear( aValue : Integer );
  end;

implementation

uses
  System.SysUtils, System.DateUtils;

{ TStringGridCalendar }

constructor TStringGridCalendar.Create( aStringGrid : TStringGrid );
begin
  inherited Create;
  FGrid := aStringGrid;
  FGrid.Options := FGrid.Options - [goRangeSelect];
  FGrid.OnSelectCell := GridSelectCell;
  FSelDate := Date;

  DoGridDataFill;
end;

procedure TStringGridCalendar.DoGridDataFill;
var
  idx : Integer;
  DateIdx : TDate;
  lRow, lCol : Integer;
begin
  FGrid.FixedCols := 1;
  FGrid.FixedRows := 1;

  FGrid.ColCount := 38;
  FGrid.RowCount := MonthsPerYear + FGrid.FixedRows;

  // Jahreszahl in die erste Zelle
  FGrid.Cells[0, 0] := IntToStr( YearOf( SelDate ) );

  // Tagesnamen in die erste Zeile
  for idx := FGrid.FixedCols to Pred( FGrid.ColCount ) do
    begin
      FGrid.Cells[idx, 0] := ShortDayNames[( idx - FGrid.FixedCols + 1 ) mod DaysPerWeek + 1];
    end;

  // Monatsnamen in die erste Spalte
  for idx := FGrid.FixedRows to Pred( FGrid.RowCount ) do
    begin
      FGrid.Cells[0, idx] := LongMonthNames[idx - FGrid.FixedRows + 1];
    end;

  for lRow := FGrid.FixedRows to Pred( FGrid.RowCount ) do
    begin
      for lCol := FGrid.FixedCols to Pred( FGrid.ColCount ) do
        begin
          if TryGetDateFromGrid( lCol, lRow, DateIdx )
          then
            FGrid.Cells[lCol, lRow] := IntToStr( DayOf( DateIdx ) )
          else
            FGrid.Cells[lCol, lRow] := '';
        end;
    end;

end;

function TStringGridCalendar.GetDateFromGrid( ACol, ARow : Integer ) : TDate;
begin
  if not TryGetDateFromGrid( ACol, ARow, Result )
  then
    raise Exception.Create( 'Fehlermeldung' );
end;

procedure TStringGridCalendar.GridSelectCell( Sender : TObject; ACol, ARow : Integer; var CanSelect : Boolean );
var
  lDate : TDate;
begin
  if TryGetDateFromGrid( ACol, ARow, lDate )
  then
    begin
      CanSelect := True;
      SelDate := lDate;
    end
  else
    CanSelect := False;
end;

procedure TStringGridCalendar.DoIncYear( aValue : Integer );
begin
  SelDate := IncYear( SelDate, aValue )
end;

function TStringGridCalendar.TryGetDateFromGrid( ACol, ARow : Integer; out aDate : TDate ) : Boolean;
var
  lMonth, lDay, lYear : Word;
  lMinCol, lMaxCol : Integer;
begin
  Result := False;
  if ( ACol >= FGrid.FixedCols ) and ( ARow >= FGrid.FixedRows )
  then
    begin
      lYear := YearOf( SelDate );
      lMonth := ARow - FGrid.FixedRows + 1;
      aDate := EncodeDate( lYear, lMonth, 1 );
      lMinCol := DayOfTheWeek( aDate ) + FGrid.FixedCols - 1;
      lMaxCol := lMinCol + DaysInMonth( aDate ) - 1;
      if ( ACol >= lMinCol ) and ( ACol <= lMaxCol )
      then
        begin
          Result := True;
          lDay := ACol - lMinCol + 1;
          aDate := EncodeDate( lYear, lMonth, lDay );
        end;
    end;
end;

procedure TStringGridCalendar.SetSelDate( const Value : TDate );
var
  lYearChange : Boolean;
begin
  if ( Value <> SelDate )
  then
    begin
      lYearChange := YearOf( Value ) <> YearOf( SelDate );
      FSelDate := Value;
      if lYearChange
      then
        DoGridDataFill;
    end;
end;

end.
Diese Klasse kann man nun noch um beliebige Features erweitern um die Anzeige aufzuhübschen oder weitere Funktionalitäten anzupassen.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat