如何寫 Edit + DBGRID 功能的VCL ? |
尚未結案
|
jeffreck
高階會員 ![]() ![]() ![]() ![]() 發表:247 回覆:340 積分:197 註冊:2003-01-23 發送簡訊給我 |
請教各位前輩 如何寫 Edit + DBGRID 功能的VCL ? 目前想寫一個Edit + DBGRID 功能的VCL
有點像 TComboBox,但要自己加一些東西
但不知為何,無法顯示出DBGRID??
可否請各位前輩指導?? 萬分感謝.....謝謝.....謝謝 --------
unit EditDBGRID; interface uses SysUtils, Classes, Controls, StdCtrls,DBGrids ; type TEditDBGRID = class(TEdit) private { Private declarations } DBGrid1: TDBGrid; protected { Protected declarations } public { Public declarations } constructor Create(Aowner:TComponent);override; destructor Destroy;override; published { Published declarations } end; procedure Register; implementation constructor TEditDBGRID.Create(AOwner:TComponent); begin inherited Create(Aowner); {Initialize Button} DBGrid1:= TDBGrid.Create(self.Parent); DBGrid1.Top :=0 ; DBGrid1.Left :=0 ; DBGrid1.Height:=100; DBGrid1.Width:=100; DBGrid1.Parent :=self.Parent ; DBGrid1.Show; DBGrid1.Visible:=True; // end; destructor TEditDBGRID.Destroy; begin inherited Destroy; // end; procedure Register; begin RegisterComponents('Test', [TEditDBGRID]); end; end. |
wameng
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
臨時寫的,若不完善及Bug 請自行處理一下。
粉累!事實上這類元件,網站上搜一下是蠻多的。
不過自己寫樂趣多彈性大,也能加深對 VCL 的認識。
unit EveditGrid; interface uses Sysutils,Classes,StdCtrls,Messages,Controls,Forms,Windows,Graphics ,Grids,DBGrids; Type TEvEditGrid = Class; TEvDown = class(TGraphicControl) private FPressed : Boolean; fEditCtrl : TEvEditGrid; protected procedure MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); Override; procedure MouseUp(Button: TMouseButton; Shift: TShiftState;X, Y: Integer);Override; procedure Paint; Override; public constructor Create(AOwner: TComponent); override; property EditCtrl:TEvEditGrid read fEditCtrl write fEditCtrl; published end; TEvEditGrid = class(TEdit) private FButton : TEvDown; fDBGrid :TDBGrid; procedure SetEditRect; procedure WMSize(var Message: TWMSize); message WM_SIZE; procedure CMCancelMode(var Message: TCMCancelMode); message CM_CancelMode; protected procedure Loaded; override; procedure KeyPress(var Key: Char);override; procedure CreateWnd;override; procedure CreateParams(var Params: TCreateParams);Override; public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published Property DBGrid :TDBGrid read fDBGrid write fDBGrid; Property Ctl3D; property BorderStyle; Property Align; end; procedure Register; implementation procedure Register; begin RegisterComponents('Samples',[TEvEditGrid]); end; constructor TEvDown.Create(AOwner: TComponent); begin inherited Create(AOwner); Width := GetSystemMetrics(SM_CXVSCROLL); Height := 10; ControlStyle := ControlStyle - [csDoubleClicks]; end; procedure TEvDown.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); Const GridVisibleRowCount = 5; var GridHeight:Integer; PS : TPoint; begin inherited MouseDown(Button, Shift, X, Y); if (Button = mbLeft) then begin if not fEditCtrl.DBGrid.Visible then begin FPressed := True; With fEditCtrl do begin GridHeight := TStringGrid(DBGrid).RowHeights[0] (TStringGrid(DBGrid).DefaultRowHeight*5) 4; PS := TForm(Owner).ClientToScreen(Point(Left,Top Height)); DBGrid.SetBounds(PS.x,PS.y,Width,GridHeight); DBGrid.Visible := True; SetWindowPos(DBGrid.Handle,HWND_TOP,0,0, 0, 0 ,SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_SHOWWINDOW); end; Repaint; end else fEditCtrl.DBGrid.Visible := False; end; end; procedure TEvDown.MouseUp(Button: TMouseButton; Shift: TShiftState;X, Y: Integer); begin inherited MouseUp(Button, Shift, X, Y); if FPressed then begin FPressed := False; Repaint; end; end; procedure TEvDown.Paint; var Flags : Integer; ARect : TRect; begin ARect := Canvas.ClipRect; if FPressed then Flags := DFCS_FLAT or DFCS_PUSHED else Flags := 0; DrawFrameControl(Canvas.Handle, ARect, DFC_SCROLL, Flags or DFCS_SCROLLCOMBOBOX); end; {TEvEditGrid} constructor TEvEditGrid.Create(AOwner: TComponent); begin inherited Create(AOwner); FDBGrid := TDBGrid.Create(Self); FDBGrid.Parent := Self; FDBGrid.Visible := False; FButton := TEvDown.Create(Self); FButton.Parent := Self; FButton.EditCtrl := Self; FButton.Cursor := crArrow; end; destructor TEvEditGrid.Destroy; begin FButton.Free; FDBGrid.Free; inherited Destroy; end; procedure TEvEditGrid.Loaded; begin inherited; Windows.SetParent(FDBGrid.Handle, 0); end; procedure TEvEditGrid.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); Params.Style := Params.Style or ES_MULTILINE; end; procedure TEvEditGrid.SetEditRect; var BRect : TRect; begin BRect := Rect(0,0,ClientWidth - FButton.Width - 1 ,ClientHeight 1 ); SendMessage(Handle, EM_SETRECT, 0, LongInt(@ BRect)); end; procedure TEvEditGrid.CreateWnd; begin inherited CreateWnd; SetEditRect; end; procedure TEvEditGrid.WMSize(var Message: TWMSize); begin inherited; FButton.SetBounds(ClientWidth-FButton.Width,0,FButton.Width,ClientHeight); SetEditRect; end; procedure TEvEditGrid.KeyPress(var Key: Char); begin inherited KeyPress(Key); if Key=#13 then begin Key := #0; MessageBeep(0) end; end; procedure TEvEditGrid.CMCancelMode(var Message: TCMCancelMode); begin inherited; fDBGrid.Visible := False; end; end.若不瞭解之處,請跟我說。 ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~ 發表人 - wameng 於 2005/05/21 22:33:38 |
change.jian
版主 ![]() ![]() ![]() ![]() ![]() 發表:29 回覆:620 積分:439 註冊:2003-06-02 發送簡訊給我 |
to jeffreck: 1.Owner與Parent功能不同,搜尋一下站上主題,應能找到不少資料.你的做法在Create的地方有錯誤,DBGrid1要能顯示,並不應該在Create時就顯示,而是在設定TEditDBGrid.Parent時才顯示,所以,你應該override SetParent這個procedure(宣告在Protected裡),當然,create裡就不需要去設定DBGrid的Parent,Show,visible等(我想你應該是在試如何讓物件顯示才加的吧),如下
procedure TEditDBGRID.SetParent(AParent: TWinControl); begin DBGrid1.Parent:=AParent; inherited; end;2.新的物件,建議從TCustomerEdit繼承下來會比直接從TEdit繼承下來要好. 3.如果有source code的話,可以參考TLabeledEdit的做法,應該可以得到不少的東西 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |