StringGrid 左上方的cell 無故被select |
尚未結案
|
tidal
初階會員 發表:93 回覆:32 積分:25 註冊:2003-07-15 發送簡訊給我 |
再次煩到各位大大了. 這是執行的結果, 左上方的cell 無故被select (當我在StringGrid 中按shift 鍵時, 按其他鍵的話 cell[1,1] 被select 或 unselect). 當我加了OnMouseUpEvent 才有此問題. code 如下:
unit timeZonepas; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls; type TtimeZone = class(TForm) StringGrid1: TStringGrid; btnSave: TButton; procedure FormCreate(Sender: TObject); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure StringGrid1Click(Sender: TObject); procedure btnSaveClick(Sender: TObject); procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } public { Public declarations } end; var timeZone: TtimeZone; implementation {$R *.dfm} var timeArray:array of array of String; procedure TtimeZone.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 := clNavy; // 設定背景顏色 End; Obj.Canvas.FillRect(Rect); // 用Brush.color 填滿該儲存格 Obj.Canvas.TextOut(Rect.Left+2,Rect.Top+2,Obj.Cells[ACol,ARow]); // 畫出顯示的文字 End; procedure TtimeZone.StringGrid1Click(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; procedure TtimeZone.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var myRect: TGridRect; begin myRect.Left := -1; myRect.Top := -1; myRect.Right := -1; myRect.Bottom := -1; StringGrid1.Selection := myRect; end; end.如果問題解決不了, 如何使按下任何鍵時沒有反應, thx you. |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
你的問題可能無法解決,因為受限於TStringGrid的實作碼,
請參考TStringGrid的KeyDown()原始碼,它實作在TCustomGrid中.
Shift鍵不是被考慮到的按鍵(Ctrl則有).
procedure TCustomGrid.KeyDown(var Key: Word; Shift: TShiftState); begin ... if ssCtrl in Shift then case Key of ... end; else case Key of ... end; MaxTopLeft.X := ColCount - 1; MaxTopLeft.Y := RowCount - 1; MaxTopLeft := CalcMaxTopLeft(MaxTopLeft, DrawInfo); Restrict(NewTopLeft, FixedCols, FixedRows, MaxTopLeft.X, MaxTopLeft.Y); if (NewTopLeft.X <> LeftCol) or (NewTopLeft.Y <> TopRow) then MoveTopLeft(NewTopLeft.X, NewTopLeft.Y); Restrict(NewCurrent, FixedCols, FixedRows, ColCount - 1, RowCount - 1); if (NewCurrent.X <> Col) or (NewCurrent.Y <> Row) then FocusCell(NewCurrent.X, NewCurrent.Y, not (ssShift in Shift)); if NeedsInvalidating then Invalidate; end;解決的方法是你要把Shift給過濾掉. TMyStringGrid = class(TStringGrid) protected procedure KeyDown(var Key: Word; Shift: TShiftState); override; end; implementation procedure TMyStringGrid.KeyDown(var Key: Word; Shift: TShiftState); begin if ssShift in Shift then Exit else inherited; end;以下是修改過的測試碼 procedure TForm1.FormCreate(Sender: TObject); begin //動態產生一個Grid with TMyStringGrid.Create(Self) do begin ColCount := 10; RowCount := 20; Width := DefaultColWidth * ColCount 10; Align := alLeft; Parent := Self; OnClick := Self.StringGrid1Click; OnDrawCell := Self.StringGrid1DrawCell; OnMouseUp := StringGrid1MouseUp; end; end; procedure TForm1.StringGrid1Click(Sender: TObject); var C,R : Integer; begin with TStringGrid(Sender) do for C := 0 to ColCount - 1 do begin for R := 0 to RowCount - 1 do begin If ((C >= Selection.Left) and (C <= Selection.Right) and (R >= Selection.Top) and (R <= Selection.Bottom)) then begin If Integer(Objects[C,R]) = 1 then Objects[C,R] := TObject(0) else Objects[C,R] :=TObject(1); end; end; end; end; procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var myRect: TGridRect; begin with TStringGrid(Sender) do begin myRect.Left := -1; myRect.Top := -1; myRect.Right := -1; myRect.Bottom := -1; Selection := myRect; end; end; |
tidal
初階會員 發表:93 回覆:32 積分:25 註冊:2003-07-15 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
老實說,我看了很久才知道您欲為何?
事實上,我搞不清楚為何如此複雜。
將OnMouseUpEvent 及onClick 移除
在OnSelectcell 中處理不能嗎!還是有別的用意?
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); begin If Integer(StringGrid1.Objects[ACOl,ARow]) = 1 then StringGrid1.Objects[ACOl,ARow] := TObject(0) else StringGrid1.Objects[ACOl,ARow] :=TObject(1); end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |