お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"StringGrid:各セル毎に文字列表示書式を"






■説明
 StringGrid の各セル毎に文字色、 文字位置を指定するサンプルです。
 SpeedButton を押すことでセル毎の文字列表示書式を変更できます。

 Form1に StringGrid1, SpeedButton1 〜 SpeedButton7, ColorDialog1
を適当に配置して下さい。そして、各 SpeedButton は、

  SpeedButton1.Caption    ・・・ DT_LEFT
       〃     .GroupIndex ・・・ 1      
       〃     .Down       ・・・ True   
 SpeedButton2.Caption    ・・・ DT_CENTER
       〃     .GroupIndex ・・・ 1      
  SpeedButton3.Caption    ・・・ DT_RIGHT
       〃     .GroupIndex ・・・ 1      

 SpeedButton4.Caption    ・・・ DT_SINGLELINE or DT_TOP
       〃     .GroupIndex ・・・ 2      
       〃     .Down       ・・・ True   
 SpeedButton5.Caption    ・・・ DT_SINGLELINE or DT_VCENTER
       〃     .GroupIndex ・・・ 2      
 SpeedButton6.Caption    ・・・ DT_SINGLELINE or DT_BOTTOM
       〃     .GroupIndex ・・・ 2      

  SpeedButton7.Caption    ・・・ Font Color
       〃     .GroupIndex ・・・ 0      

 のように設定して下さい。( SpeedButton1Click を SpeedButton1 〜
SpeedButton6 で共有させて下さい。)

■参考
 MES 15 #160, #228 等

■サンプルコード
//=====================================================================
{...略...}
type
  TCellsStatus = packed record          //各セルの書式を格納
                   hAlgnmt : UINT;
                   vAlgnmt : UINT;
                   FontColor : TColor;
  end;
  PCellsStArray = ^TCStArray;
  TCStArray = array[ 0..0 ] of TCellsStatus;

  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
{...略...}
  private
    { Private 宣言 }
    procedure SetStatus( ACol, ARow : Integer; CellsStatus : TCellsStatus );
    function GetStatus( ACol, ARow : Integer ): TCellsStatus;
{...略...}
var
  Form1: TForm1;
  pCStArray : PCellsStArray;

implementation

{$R *.DFM}

//セルの書式設定手続
procedure TForm1.SetStatus( ACol, ARow : Integer; CellsStatus : TCellsStatus );
begin
  with StringGrid1 do
    pCStArray^[ ACol + ARow * ColCount ] := CellsStatus;
end;

//セルの書式読み出し関数
function TForm1.GetStatus( ACol, ARow : Integer ): TCellsStatus;
begin
  with StringGrid1 do
    Result := pCStArray^[ ACol + ARow * ColCount ];
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j,
  W, H : Integer;
  TempSt : TCellsStatus;
begin
  with StringGrid1 do
    begin
      W := GridLineWidth * ColCount;
      H := GridLineWidth * RowCount;
      pCStArray := AllocMem(ColCount * RowCount * SizeOf(TCellsStatus));
      with TempSt do
        begin
          hAlgnmt := DT_LEFT;
          vAlgnmt := DT_SINGLELINE or DT_TOP;
          FontColor := clBlack;
        end;
      for i := 0 to ColCount - 1 do
        begin
          W := W + ColWidths[i];
          for j := 0 to RowCount - 1 do
            begin
              SetStatus( i, j, TempSt );
              Cells[ i, j ] := 'C:' + IntToStr(i) + '/R:' + IntToStr(j);
              if i = 0 then H := H + RowHeights[j];
            end;
        end;
      ClientWidth  := W + 100;   //ScrollBar 対策(#42参照)
      ClientHeight := H + 100;   //ScrollBar 対策(#42参照)
      ClientWidth  := W;
      ClientHeight := H;
    end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  with StringGrid1 do
    FreeMem( pCStArray, ColCount * RowCount * SizeOf( TCellsStatus ) );
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
var
  dtRect : TRect;
  DTFLG : UINT;
begin
  with dtRect do
    begin
      Left   := Rect.Left   + 2;  //文字列位置補正
      Right  := Rect.Right  - 2;  //   〃   
      Top    := Rect.Top    + 2;  //   〃   
      Bottom := Rect.Bottom - 2;  //   〃   
    end;
  with StringGrid1.Canvas, GetStatus( Col, Row ) do
    begin
      DTFLG :=  hAlgnmt or vAlgnmt;
      Font.Color := FontColor;
      FillRect( Rect );
      DrawText( Handle, PChar( StringGrid1.Cells[Col, Row] ), -1,
                                                   dtRect, DTFLG );
      Font.Color := clBlack;  //DrawFocusRect() の色化け対策
    end;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  TempSt : TCellsStatus;
begin
  with StringGrid1 do
    begin
      TempSt := GetStatus( Col, Row );
      with TempSt do
        case StrToInt( TSpeedButton( Sender ).Name[12] ) of
         {Horizontal}   //  Nameに依存。要注意。/~~~~
          1: hAlgnmt := DT_LEFT;
          2: hAlgnmt := DT_CENTER;
          3: hAlgnmt := DT_RIGHT;
         {Vertical}
          4: vAlgnmt := DT_SINGLELINE or DT_TOP;
          5: vAlgnmt := DT_SINGLELINE or DT_VCENTER;
          6: vAlgnmt := DT_SINGLELINE or DT_BOTTOM;
        end;
      SetStatus( Col, Row, TempSt );
      Refresh;
    end;
end;

procedure TForm1.StringGrid1Click(Sender: TObject);
begin
  with StringGrid1, GetStatus( Col, Row ) do
    begin
      case hAlgnmt of
        DT_LEFT  : SpeedButton1.Down := True;
        DT_CENTER: SpeedButton2.Down := True;
        DT_RIGHT : SpeedButton3.Down := True;
      end;
      case vAlgnmt of
        DT_SINGLELINE or DT_TOP     : SpeedButton4.Down := True;
        DT_SINGLELINE or DT_VCENTER : SpeedButton5.Down := True;
        DT_SINGLELINE or DT_BOTTOM  : SpeedButton6.Down := True;
      end;
    end;
end;

procedure TForm1.SpeedButton7Click(Sender: TObject);
var
  TempSt : TCellsStatus;
begin
  with StringGrid1, ColorDialog1 do
    if Execute then
      begin
        TempSt := GetStatus( Col, Row );
        TempSt.FontColor := Color;
        SetStatus( Col, Row, TempSt );
        Refresh;
      end;
end;
{...略...}
//=====================================================================

                                     97/10/27(Mon) 02:00 Fermion [KHF03264]

Original document by Fermion         氏 ID:(KHF03264)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum