본문 바로가기

카테고리 없음

[일반/컴포넌트] StringGrid의 중간에 제목 Cell 두기

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
      var CanSelect: Boolean);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

// StringGrid1의 프로퍼티 setting
//    DefaultDrawing := True
//    Options에 goEditing 추가

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: Integer;
begin
  StringGrid1.RowCount := 6;
  StringGrid1.ColCount := 5;

  // Column의 title을 만든다
  for i := 1 to StringGrid1.ColCount - 1 do
  begin
    StringGrid1.Cells[i, 0] := Char(Ord('A')+i-1); // 제목
    StringGrid1.Cells[i, 3] := Char(Ord('E')+i-1); // 제목

    StringGrid1.Cells[i, 1] := Format('%.0n', [i * 1 * 10000.0]); // 값
    StringGrid1.Cells[i, 2] := Format('%.0n', [i * 2 * 10000.0]); // 값
    StringGrid1.Cells[i, 4] := Format('%.0n', [i * 3 * 10000.0]); // 값
    StringGrid1.Cells[i, 5] := Format('%.0n', [i * 4 * 10000.0]); // 값
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if Row = 3 then
  begin
    with (Sender as TstringGrid).Canvas do
    begin
      // 버튼 모양을 그린다
      DrawFrameControl(Handle, Rect, DFC_BUTTON,
                       DFCS_BUTTONPUSH {or DFCS_FLAT} or DFCS_ADJUSTRECT);
      Brush.Style := bsClear;
      Font.Color  := clBlack;
      TextRect(rect, rect.left+2, rect.top+2,
               TStringGrid(Sender).Cells[Col, Row]);
    end;
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
  var CanSelect: Boolean);
begin
  if Row = 3 then
    StringGrid1.Options := StringGrid1.Options - [goEditing]
  else
    StringGrid1.Options := StringGrid1.Options + [goEditing];
end;

end.