線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1171
推到 Plurk!
推到 Facebook!

TStringGrid 繪製問題

尚未結案
tidal
初階會員


發表:93
回覆:32
積分:25
註冊:2003-07-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-01-07 11:06:43 IP:202.82.xxx.xxx 未訂閱
我想用TStringGrid 做一個元件, 當按下cell時會轉藍色, multiSelect 時也會轉藍色, 按delete 鍵時會消除顏色(也可以multiselect時按delete鍵一次過清除顏色). 還有另一個問題時, 我如何判決某一個cell轉了顏色?
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-01-07 18:47:46 IP:211.20.xxx.xxx 未訂閱
試試這樣如何??    unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Grids;    type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure FormShow(Sender: TObject);
    procedure StringGrid1DblClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Obj : TstringGrid;
Begin
  Obj := TStringgrid(Sender);
  // 要變換顏色的判斷
  If Integer(TStringGrid(Sender).Objects[ACol,ARow]) = 1 Then Begin
    Obj.Canvas.Brush.Color := clRed;
    If gdSelected IN State Then Obj.Canvas.Brush.Color := clRed;
    If gdFocused IN State Then Obj.Canvas.Brush.Color := clRed;
  End;
  Obj.Canvas.FillRect(Rect);
  Obj.Canvas.TextOut(Rect.Left 2,Rect.Top 2,Obj.Cells[ACol,ARow]);
End;    procedure TForm1.FormShow(Sender: TObject);
begin
  StringGrid1.Rows[0].CommaText := 'C00,C01,C02,C03,C04' ;
  StringGrid1.Rows[1].CommaText := 'C10,C11,C12,C13,C14' ;
  StringGrid1.Rows[2].CommaText := 'C20,C21,C22,C23,C24' ;
  StringGrid1.Rows[3].CommaText := 'C30,C31,C32,C33,C34' ;
  StringGrid1.Rows[4].CommaText := 'C40,C41,C42,C43,C44' ;    end;    procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
  With StringGrid1 Do Begin
    If Integer(Objects[Col,Row]) = 1 Then Begin  // 要換顏色
      Objects[Col,Row] := TObject(0);
    End Else Begin
      Objects[Col,Row] :=TObject(1);
    End;
  End;
end;    end. 
_______________________________________ 深藍的魚,祝您好運..........連連
tidal
初階會員


發表:93
回覆:32
積分:25
註冊:2003-07-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-01-07 23:32:28 IP:221.124.xxx.xxx 未訂閱
Chance36 大大, 我比較笨>_<, 看不懂程式的意思, 可以解釋一下嗎? 還有, 怎樣寫一個for loop 去check 所有cells, 看那個cells 轉了顏色. 再把那個cell的row 和 col 紀錄在一個array 上? 感激不儘.
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-01-08 02:45:44 IP:203.204.xxx.xxx 未訂閱
引言: 還有, 怎樣寫一個for loop 去check 所有cells, 看那個cells 轉了顏色. 再把那個cell的row 和 col 紀錄在一個array 上?
解說的部份,我已加上去了
Var
  ArLog : Array Of Array[0..1] Of Integer ;
  i, j, c : Integer ;
Begin
  c := 0 ;
  With StringGrid Do Begin
    For i:= FixedRows To RowCount - 1 Do Begin  // 逐列掃描
      For j:= FixedCols To ColCount - 1 Do Begin // 逐行掃描
        If Integer(Objects[j,i]) = 1 Then Begin  // 
          SetLength(ArLog,c 1);  // 將陣列元素加大
          Arlog[c][0] := j ;  // 儲存[行]
          Arlog[c][1] := i ;  // 儲存[列]
          Inc(c);
        End;
      End;
    End;
  End;
End;
_______________________________________ 深藍的魚,祝您好運..........連連
tidal
初階會員


發表:93
回覆:32
積分:25
註冊:2003-07-15

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-01-08 10:00:27 IP:221.124.xxx.xxx 未訂閱
真是感謝Chance36大大無私的分享.
系統時間:2024-07-03 5:13:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!