问题:
我写了一个自定义控件FileDlg(包含一个Edit和一个SpeedButton)
怎样给SpeedButton的OnClick事件写代码,让系统响应Click事件。
代码如下:
unit FileEdit;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls;const
xWidth = 160; //FEdit 的初始化宽度type
TFileEdit = class(TWinControl)
private
{ Private declarations }
FEdit : TEdit;
FSbtn : TSpeedButton;
FFilter :string;
FDirectory :string;
procedure FSBtnClick(Sender: TObject);
protected
{ Protected declarations }
procedure WMSize(var Message:TWMSize);message WM_SIZE;
function GetText:string;
procedure SetText(value : string);
function GetMaxLength :integer;
procedure SetMaxLength(value : integer);
function GetDirectory :string;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Clear;
property Directory:string read GetDirectory write FDirectory;
published
{ Published declarations }
property Anchors;
property BiDiMode;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property MaxLength :integer read GetMaxLength write SetMaxLength default 150;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property Visible;
//property OnChange;
property OnEnter;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
property Text : string read GetText write SetText;
property Filter : string read FFilter write FFilter;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Hawk UI', [TFileEdit]);
end;procedure TFileEdit.FSBtnClick(Sender: TObject);//单击事件
var
OpenDlg : TOpenDialog;
begin
OpenDlg := TOpenDialog.Create(nil);
if Length(Trim(Filter))=0
then OpenDlg.Filter:='*.*'
else OpenDlg.Filter:=Filter;
OpenDlg.InitialDir:=OpenDlg.HistoryList[0];
if OpenDlg.Execute
then SetText(OpenDlg.FileName);
end;constructor TFileEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit := TEdit.Create(self);
FEdit.Parent := self;
FEdit.Width := xWidth;
FEdit.Height :=21;
FEdit.Top :=1;
SetMaxLength(MaxLength);
FSbtn := TSpeedButton.Create(self);
FSbtn.Parent := self;
FSbtn.Width := 20;
FSbtn.Height := 21;
FSbtn.Top:=1;
FSbtn.Left := FEdit.Width +2;
FSbtn.Caption :='...';
SetText(self.Name)
//FSbtn.OnClick := FSBtnClick(self);//问题所在------怎么修改????
end;destructor TFileEdit.Destroy;
begin
FEdit.Free;
FSbtn.Free;
inherited Destroy;
end;procedure TFileEdit.WMSize(var Message:TWMSize);
begin
if (Message.Width<21)or(Message.Height<21)
then Exit;
Message.Height := 21;
inherited;
FEdit.Width:=Message.Width-FSBtn.Width;
FEdit.Height := Message.Height;
FSBtn.Left:=FEdit.Width;
end;procedure TFileEdit.SetText(value : string );
begin
FEdit.Text := value ;
end;function TFileEdit.GetText : string;
begin
Result:= FEdit.Text;
end;function TFileEdit.GetMaxLength :integer;
begin
Result := FEdit.MaxLength;
end ;procedure TFileEdit.SetMaxLength(value :integer);
begin
FEdit.MaxLength := value;
end;procedure TFileEdit.Clear;
begin
FEdit.Clear;
end;function TFileEdit.GetDirectory:string;
begin
Result := ExtractFilepath(FEdit.Text);
end;end.
我写了一个自定义控件FileDlg(包含一个Edit和一个SpeedButton)
怎样给SpeedButton的OnClick事件写代码,让系统响应Click事件。
代码如下:
unit FileEdit;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Buttons, StdCtrls;const
xWidth = 160; //FEdit 的初始化宽度type
TFileEdit = class(TWinControl)
private
{ Private declarations }
FEdit : TEdit;
FSbtn : TSpeedButton;
FFilter :string;
FDirectory :string;
procedure FSBtnClick(Sender: TObject);
protected
{ Protected declarations }
procedure WMSize(var Message:TWMSize);message WM_SIZE;
function GetText:string;
procedure SetText(value : string);
function GetMaxLength :integer;
procedure SetMaxLength(value : integer);
function GetDirectory :string;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Clear;
property Directory:string read GetDirectory write FDirectory;
published
{ Published declarations }
property Anchors;
property BiDiMode;
property Color;
property Constraints;
property Ctl3D;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property MaxLength :integer read GetMaxLength write SetMaxLength default 150;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property Visible;
//property OnChange;
property OnEnter;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
property Text : string read GetText write SetText;
property Filter : string read FFilter write FFilter;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Hawk UI', [TFileEdit]);
end;procedure TFileEdit.FSBtnClick(Sender: TObject);//单击事件
var
OpenDlg : TOpenDialog;
begin
OpenDlg := TOpenDialog.Create(nil);
if Length(Trim(Filter))=0
then OpenDlg.Filter:='*.*'
else OpenDlg.Filter:=Filter;
OpenDlg.InitialDir:=OpenDlg.HistoryList[0];
if OpenDlg.Execute
then SetText(OpenDlg.FileName);
end;constructor TFileEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEdit := TEdit.Create(self);
FEdit.Parent := self;
FEdit.Width := xWidth;
FEdit.Height :=21;
FEdit.Top :=1;
SetMaxLength(MaxLength);
FSbtn := TSpeedButton.Create(self);
FSbtn.Parent := self;
FSbtn.Width := 20;
FSbtn.Height := 21;
FSbtn.Top:=1;
FSbtn.Left := FEdit.Width +2;
FSbtn.Caption :='...';
SetText(self.Name)
//FSbtn.OnClick := FSBtnClick(self);//问题所在------怎么修改????
end;destructor TFileEdit.Destroy;
begin
FEdit.Free;
FSbtn.Free;
inherited Destroy;
end;procedure TFileEdit.WMSize(var Message:TWMSize);
begin
if (Message.Width<21)or(Message.Height<21)
then Exit;
Message.Height := 21;
inherited;
FEdit.Width:=Message.Width-FSBtn.Width;
FEdit.Height := Message.Height;
FSBtn.Left:=FEdit.Width;
end;procedure TFileEdit.SetText(value : string );
begin
FEdit.Text := value ;
end;function TFileEdit.GetText : string;
begin
Result:= FEdit.Text;
end;function TFileEdit.GetMaxLength :integer;
begin
Result := FEdit.MaxLength;
end ;procedure TFileEdit.SetMaxLength(value :integer);
begin
FEdit.MaxLength := value;
end;procedure TFileEdit.Clear;
begin
FEdit.Clear;
end;function TFileEdit.GetDirectory:string;
begin
Result := ExtractFilepath(FEdit.Text);
end;end.
FSbtn.OnClick := FSBtnClick;
我在Create构造函数中设置了控件的高和宽,但是没有起到效果。
constructor TFileEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 200;
Width := 300; FEdit := TEdit.Create(self);
//...
FSbtn := TSpeedButton.Create(Self);
//...
end;
在下面代码中怎么写比较好。procedure TFileEdit.WMSize(var Message:TWMSize);
begin
if (Message.Width<21)or(Message.Height<21)
then Exit;
Message.Height := 50;//在这里对高度重新赋值好像达不到效果
inherited;
FEdit.Width:=Message.Width-FSBtn.Width;
FEdit.Height := Message.Height;
FSBtn.Left:=FEdit.Width;
end;