dbGrid 要能選取一個 Block , 並填入一個字元 |
答題得分者是:wameng
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); var Cell: TGridCoord; SelectRect: TGridRect; begin Cell := DBGrid1.MouseCoord(X,Y); SelectRect := TStringGrid(DBGrid1).Selection; if ssLeft in Shift then if (Cell.X>-1) and (Cell.Y>-1) then begin SelectRect.BottomRight := Cell; TStringGrid(DBGrid1).Selection := SelectRect; end; end;參考,我簡單寫了一小段。 只能由左向右。 ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~ |
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
版主兄 : 果然功力深厚, 完全是我想像不到的程式碼
不過與我的問題, 似乎有點距離 Step 1 :
我想要在 dbGrid 上, 能夠像 StringGrid 一樣, 能夠用滑鼠左擊, 點在某個Cell上 -> 移動滑鼠 -> 放開滑鼠左鍵
如果是 stringGrid 會有一塊 selected Block , dbGrid 不行 Step 2 :
滑鼠放開的同時, 在 Block 中, 每個 Cell 填入 'A' 字元 p.s. 完全沒用到鍵盤 , 我的環境是 D5 , 應該沒差吧 ? < >< >
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
事實上,要實現這樣的功能是不難的。
若不用 Selection的功能,就可能要自己畫。
可定義一變數為BRect:TRect Selected Block 區域。
並在 OnMouseDown ..MouseMove及MouseUp 處理。 另外在定義 2 個BOOKMARK 為第一個位置紀錄及最後的紀錄位置。
現在我要下班... 嘿嘿!回家先 晚點在上來,若有其他大大願意代勞!那就.....
或者您可以自己寫寫看。... ~~~~~~~~~~~
難得聰明,常常糊塗。
~~~~~~~~~~~
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
type TDBGrid = Class(DBGrids.TDBGrid) Private fSelectedCells : TRect; Procedure SetSelectedCells(Value:TRect); Protected function HighlightCell(DataCol, DataRow: Integer; const Value: string; AState: TGridDrawState): Boolean; Override; procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; procedure MouseMove(Shift: TShiftState; X, Y: Integer); override; procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; Public ButtonDown :Boolean; Property SelectedCells:TRect read fSelectedCells write SetSelectedCells; constructor Create(AOwner: TComponent); override; destructor Destroy; override; end; TForm1 = class(TForm) DBGrid1: TDBGrid; .... procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private public end; var Form1: TForm1; implementation {$R *.DFM} Function SameRect(ARect,BRect:TRect):Boolean; begin With ARect do Result := (Left = BRect.Left) and (Right = BRect.Right) and (Top = BRect.Top) and (Bottom = BRect.Bottom); end; constructor TDBGrid.Create(AOwner: TComponent); begin inherited Create(AOwner); ButtonDown := False; fSelectedCells:= Rect(-1,-1,-1,-1); end; destructor TDBGrid.Destroy; begin inherited Destroy; end; Procedure TDBGrid.SetSelectedCells(Value:TRect); Function GetBlockRect(GridRect:TRect):TRect; var RBRect : TRect; begin With GridRect do begin Result := CellRect(Left,Top); RBRect := CellRect(Right,Bottom); Result.BottomRight := RBRect.BottomRight; end; end; var OldSltRect,TMPRect :TRect; begin if Not SameRect(fSelectedCells,Value) then begin OldSltRect := fSelectedCells; fSelectedCells := Value; if (OldSltRect.Left>-1) then begin TMPRect :=GetBlockRect(OldSltRect); Windows.InvalidateRect(Handle, @TMPRect, True); end; if (fSelectedCells.Left>-1) then begin TMPRect :=GetBlockRect(fSelectedCells); Windows.InvalidateRect(Handle, @TMPRect, True); end; end; end; procedure TDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); var Cell: TGridCoord; TmpRect : TRect; begin inherited MouseDown(Button, Shift, X, Y); if Not Assigned(DataSource.DataSet) then Exit; TmpRect := Rect(-1,-1,-1,-1); With DataSource.DataSet do if (Button=mbLeft) and Active then begin Cell := MouseCoord(X,Y); if (Cell.X>-1) and (Cell.Y>-1) then begin TmpRect.TopLeft := Point(Cell.X,Cell.Y); TmpRect.BottomRight := TmpRect.TopLeft; ButtonDown := True; end; end; SelectedCells := TmpRect; end; procedure TDBGrid.MouseMove(Shift: TShiftState; X, Y: Integer); var Cell: TGridCoord; TmpRect : TRect; begin inherited MouseMove(Shift, X, Y); With DataSource.DataSet do if ButtonDown and (ssLeft in Shift) then begin Cell := MouseCoord(X,Y); if (Cell.X>-1) and (Cell.Y>-1) then begin TmpRect.TopLeft := Point(COL,ROW); TmpRect.BottomRight := TmpRect.TopLeft; if Cell.X<=Col then TmpRect.Left:= Cell.X else TmpRect.Right:= Cell.X; if Cell.Y<=Row then TmpRect.Top:= Cell.Y else TmpRect.Bottom:= Cell.Y; SelectedCells := TmpRect; end; end; end; procedure TDBGrid.MouseUp(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); begin inherited MouseUp(Button, Shift, X, Y); ButtonDown := False; end; function TDBGrid.HighlightCell(DataCol, DataRow: Integer; const Value: string;AState: TGridDrawState): Boolean; begin With fSelectedCells do begin Result := inherited HighlightCell(DataCol,DataRow,Value,AState) or ((Left>-1) and PtinRect(Rect(Left,Top,Right 1,Bottom 1),Point(DataCol 1,DataRow 1))); end; end; procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); var I,Direction,MoveCount: Integer; begin With DBGrid1,TStringGrid(DBGrid1),DBGrid1.DataSource.DataSet do if ButtonDown then begin BeginUpdate; Try Direction := ORD(SelectedCells.Top=Row)*2-1; MoveCount := Abs(SelectedCells.Bottom-SelectedCells.Top) 1; Repeat Edit; For I:=SelectedCells.Left to SelectedCells.Right do Columns[I-1].Field.AsString := '1'; UpdateRecord; Dec(MoveCount); if MoveCount>0 then MoveBy(Direction); until MoveCount<=0; Finally EndUpdate; end; end; end;~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~ 發表人 - wameng 於 2005/06/09 20:25:17 |
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
歹勢啦 !
TDBGrid 宣告放在 type 與 TForm1=class(TForm)
TFBGrid 程式段, 放在 implementation 後面 在 Form 畫面, 拉一個 dbGrid 元件 compile , 出現錯誤訊息 , 如下 :
[Error] Undeclared Identifier :'TBookMark'
[Fatal Error] could not compile used unit 'unit1.pas' 可是, 查 help 確實有 TBookMark 這個 type 呀 ! 好怪 !
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
嘻嘻 , 可以了
With DBGrid1 do begin
if ButtonDown then
begin
dbGrid1.DataSource.DataSet.First;
for i:=1 to dbGrid1.rowcount do begin
dbGrid1.DataSource.DataSet.Edit;
for j:=1 to dbGrid1.ColCount do begin
if (j>=dbGrid1.fSelectedCells.Left) and (j<=dbGrid1.fSelectedCells.Right) and
(i>=dbGrid1.fSelectedCells.Top) and (i<=dbGrid1.fSelectedCells.Bottom) then begin
dbGrid1.Fields[j-1].AsString:=cMod;
end;
end;
dbGrid1.DataSource.DataSet.next;
end;
end;
end; 三塊給你買蔴薯
怎樣才能練到像你一樣厲害 ?!
|
cwc65536
初階會員 發表:47 回覆:121 積分:48 註冊:2004-10-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
引言: 1. 有沒有比較優的寫法? 我寫得笨笨的 2. 大大幫我解答這麼難的題目, 分數有沒有比較多呀! 3. 我一定不會讓大大失望的,等我練成武功,一定也會傚法大大出來行俠仗義我重新在貼過程式碼。請往上查閱。 嘿嘿!分數不會比較多! 為了台灣的IT產業添加一份心力!(開玩笑的!我沒那麼偉大) 回題志不在答分,在於一方面加深自己的功力另一方面心得共享。互相交流經驗。 {感觸}心胸放大,不必侷限自己在一小塊地方相互競爭。 祝你早日練得絕世武功!成為大內高手中的高高手! 呵呵... ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~ |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |