TStringGrid multi-select 時的繪製 問題 |
尚未結案
|
tidal
初階會員 發表:93 回覆:32 積分:25 註冊:2003-07-15 發送簡訊給我 |
這是從 Chance36 大大 抄錄下來的 code, 每一次click 一下cell時便會改變顏色. 有沒有辨法 multi-select (按下shift鍵, 再click) 時, 把select 的cell 也改變顏色?
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} // 在FormShow的地方給予StringGrid 的顯示內容 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.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Obj : TstringGrid; Begin // 將Sender 轉型成TStringGrid 備用 Obj := TStringgrid(Sender); // 要變換顏色的判斷 // 每個儲存格除了可以儲存畫面看得到的文字之外,StringGrid 還為每個儲存格提供了一個Object屬性可以你儲存TObject, // 然而每個物件(TObject)其實就是個指標,也可看成一個整數值,所以只要檢查Object的屬性值(當然要轉型啦)如果為1代表改了顏色 // ,若為0則未改變顏色 If Integer(Obj.Objects[ACol,ARow]) = 1 Then Begin Obj.Canvas.Brush.Color := clRed; // 設定背景顏色 End; Obj.Canvas.FillRect(Rect); // 用Brush.color 填滿該儲存格 Obj.Canvas.TextOut(Rect.Left 2,Rect.Top 2,Obj.Cells[ACol,ARow]); // 畫出顯示的文字 End; // 每當在StringGrid DoubleClick時觸發, procedure TForm1.StringGrid1Click(Sender: TObject); begin With StringGrid1 Do Begin If Integer(Objects[Col,Row]) = 1 Then Begin // 如果該儲存格的Object 的值為1 Objects[Col,Row] := TObject(0); // 則把它改為 0 End Else Begin Objects[Col,Row] :=TObject(1); // 否則把它設為 1 End; End; end; end. |
Fishman
尊榮會員 發表:120 回覆:1949 積分:2163 註冊:2006-10-28 發送簡訊給我 |
Hi tidal, 參考 http://delphi.ktop.com.tw/topic.php?topic_id=62957,並將 OnDblClick 更改如下:
procedure TForm1.StringGrid1DblClick(Sender: TObject); var C,R : Integer; begin for C := 0 to StringGrid1.ColCount - 1 do begin for R := 0 to StringGrid1.RowCount - 1 do begin If ((C >= StringGrid1.Selection.Left) and (C <= StringGrid1.Selection.Right) and (R >= StringGrid1.Selection.Top) and (R <= StringGrid1.Selection.Bottom)) then begin If Integer(StringGrid1.Objects[C,R]) = 1 then StringGrid1.Objects[C,R] := TObject(0) else StringGrid1.Objects[C,R] :=TObject(1); end; end; end; end;---------------------------------- 小弟才疏學淺,若有謬誤尚請不吝指教 ----------------------------------
------
Fishman |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |