M
maxim
Unregistered / Unconfirmed
GUEST, unregistred user!
请各位给小弟看看,我想做个带按钮的DBEDIT编辑框,加上按钮后,按钮没法相应点击事件,请各位大侠帮忙看看。
unit MxDBEditBtn;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Mask, DBCtrls,
Buttons,Forms;
type
TMxDBEditBtn = class(TDBEdit)
private
FButton: TSpeedButton;
// FOnBtnClick: TNotifyEvent;
{ Private declarations }
procedure SetEditRect;
function GetMinHeight: Integer;
procedure WMSize (var Message: TWMSize); message WM_SIZE;
// procedure WMPaste (var Message: TWMPaste); message WM_PASTE;
// procedure WMCut (var Message: TWMCut); message WM_CUT;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
property Button: TSpeedButton read FButton;
// property OnBtnClick: TNotifyEvent read FOnBtnClick write FOnBtnClick;// stored IsOnClickStored;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MxDBTools', [TMxDBEditBtn]);
end;
{ TMxDBEditBtn }
constructor TMxDBEditBtn.Create(AOwner: TComponent);
begin
inherited;
FButton := TSpeedButton.Create (Self);
FButton.Parent := Self;
FButton.Width := 22;
FButton.Height := 20;
FButton.Visible := True;
FButton.Transparent:=True;
FButton.Caption:='...';
FButton.OnClick := OnDblClick;
// FButton.Glyph
ControlStyle := ControlStyle - [csSetCaption];
end;
destructor TMxDBEditBtn.Destroy;
begin
FButton := nil;
inherited Destroy;
end;
function TMxDBEditBtn.GetMinHeight: Integer;
var
DC: HDC;
SaveFont: HFont;
SysMetrics, Metrics: TTextMetric;
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
Result := Metrics.tmHeight + 7;
end;
procedure TMxDBEditBtn.SetEditRect;
var
Loc: TRect;
begin
SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
if BorderStyle=bsNone then
Loc := Rect(0, 0, ClientWidth - FButton.Width - 1, ClientHeight)
else
Loc := Rect(0, 0, ClientWidth - FButton.Width - 3, ClientHeight);
SendMessage(Handle, EM_SETRECTNP, 0, LongInt(@Loc));
SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
end;
procedure TMxDBEditBtn.WMSize(var Message: TWMSize);
var
MinHeight: Integer;
begin
inherited;
MinHeight := GetMinHeight;
{ text edit bug: if size to less than minheight, then edit ctrl does
not display the text }
if Height < MinHeight then
Height := MinHeight
else
if FButton <> nil then
begin
if BorderStyle=bsNone then
FButton.SetBounds(Width - FButton.Width - 2, 0, FButton.Width, Height - 1)
else
FButton.SetBounds(Width - FButton.Width - 5, 0, FButton.Width, Height - 6);
SetEditRect;
end;
end;
end.
unit MxDBEditBtn;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Mask, DBCtrls,
Buttons,Forms;
type
TMxDBEditBtn = class(TDBEdit)
private
FButton: TSpeedButton;
// FOnBtnClick: TNotifyEvent;
{ Private declarations }
procedure SetEditRect;
function GetMinHeight: Integer;
procedure WMSize (var Message: TWMSize); message WM_SIZE;
// procedure WMPaste (var Message: TWMPaste); message WM_PASTE;
// procedure WMCut (var Message: TWMCut); message WM_CUT;
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations }
property Button: TSpeedButton read FButton;
// property OnBtnClick: TNotifyEvent read FOnBtnClick write FOnBtnClick;// stored IsOnClickStored;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MxDBTools', [TMxDBEditBtn]);
end;
{ TMxDBEditBtn }
constructor TMxDBEditBtn.Create(AOwner: TComponent);
begin
inherited;
FButton := TSpeedButton.Create (Self);
FButton.Parent := Self;
FButton.Width := 22;
FButton.Height := 20;
FButton.Visible := True;
FButton.Transparent:=True;
FButton.Caption:='...';
FButton.OnClick := OnDblClick;
// FButton.Glyph
ControlStyle := ControlStyle - [csSetCaption];
end;
destructor TMxDBEditBtn.Destroy;
begin
FButton := nil;
inherited Destroy;
end;
function TMxDBEditBtn.GetMinHeight: Integer;
var
DC: HDC;
SaveFont: HFont;
SysMetrics, Metrics: TTextMetric;
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
Result := Metrics.tmHeight + 7;
end;
procedure TMxDBEditBtn.SetEditRect;
var
Loc: TRect;
begin
SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
if BorderStyle=bsNone then
Loc := Rect(0, 0, ClientWidth - FButton.Width - 1, ClientHeight)
else
Loc := Rect(0, 0, ClientWidth - FButton.Width - 3, ClientHeight);
SendMessage(Handle, EM_SETRECTNP, 0, LongInt(@Loc));
SendMessage(Handle, EM_GETRECT, 0, LongInt(@Loc));
end;
procedure TMxDBEditBtn.WMSize(var Message: TWMSize);
var
MinHeight: Integer;
begin
inherited;
MinHeight := GetMinHeight;
{ text edit bug: if size to less than minheight, then edit ctrl does
not display the text }
if Height < MinHeight then
Height := MinHeight
else
if FButton <> nil then
begin
if BorderStyle=bsNone then
FButton.SetBounds(Width - FButton.Width - 2, 0, FButton.Width, Height - 1)
else
FButton.SetBounds(Width - FButton.Width - 5, 0, FButton.Width, Height - 6);
SetEditRect;
end;
end;
end.