상세 컨텐츠

본문 제목

[일반/컴포넌트] StringGrid의 선택된 Cell을 버튼으로 보이기

카테고리 없음

by [롯벨] 2023. 9. 7. 08:28

본문

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
var
  i, j: Integer;
begin
  // Column의 title을 만든다
  for i := 1 to StringGrid1.ColCount - 1 do
   StringGrid1.Cells[i, 0] := Char(Ord('A')+i-1);

  // Row의 title을 만든다
  for i := 1 to StringGrid1.RowCount - 1 do
   StringGrid1.Cells[0, i] := IntToStr(i);;

  // 임의의 금액을 만들어서 각 cell에 입력합니다
  // format함수의 %n 은 금액(3자리수마다 콤마문자)표시 서식입니다
  for i := 1 to StringGrid1.ColCount - 1 do
    for j := 1 to StringGrid1.RowCount - 1 do
      StringGrid1.Cells[i, j] := Format('%.0n', [i * j * 10000.0]);
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; Col, Row: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  if gdSelected in State then
  begin
    with (Sender as TstringGrid).Canvas do
    begin
      // 아래 네번째 파라미터에 or DFCS_FLAT 를 추가하면 들어간 형태의 버튼을
      // 만들 수 있습니다
      DrawFrameControl(Handle, Rect, DFC_BUTTON,
                       DFCS_BUTTONPUSH 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.StringGrid1DblClick(Sender: TObject);
begin
  ShowMessage(StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row]);
end;

end.