给个以前写的给你当例子吧:
应该直接拷贝下去就能用的了。
unit ButtonEdit;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls, Buttons, Menus;
type
TButtonEdit = class(TCustomMemo)
private
FButtonVisible: Boolean;
FButtonFlat: Boolean;
FOnButtonClick: TNotifyEvent;
procedure SetButtonVisible(const Value: Boolean);
procedure SetButtonBounds;
procedure SetButtonFlat(const Value: Boolean);
function GetButtonHint: string;
procedure SetButtonHint(const Value: string);
protected
FButton: TSpeedButton;
procedure BtnClickHandler(Sender: TObject)
virtual;
procedure UpdateFormatRect;
procedure WMSize(var Msg: TWMSize)
message WM_SIZE;
procedure WMSetCursor(var Msg: TWMSetCursor)
message WM_SETCURSOR;
procedure CMEnabledChanged(var Msg: TWMNoParams)
message CM_ENABLEDCHANGED;
procedure CreateHandle
override;
public
constructor Create(AOwner: TComponent)
override;
published
property ButtonVisible: Boolean read FButtonVisible write SetButtonVisible default True;
property ButtonFlat: Boolean read FButtonFlat write SetButtonFlat;
property ButtonHint: string read GetButtonHint write SetButtonHint;
property Align;
property Alignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BorderStyle;
property CharCase;
property Color;
property Ctl3D;
property Enabled;
property Font;
property HideSelection;
property MaxLength;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnButtonClick: TNotifyEvent read FOnButtonClick write FOnButtonClick;
end;
implementation
constructor TButtonEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 21;
Width := 150;
WordWrap := False;
WantReturns := False;
FButtonVisible := True;
FButton := TSpeedButton.Create(Self);
with FButton do
begin
Parent := Self;
Caption := '...';
Align := alRight;
Spacing := -1;
ShowHint := True;
Margin := -1;
OnClick := BtnClickHandler;
end;
end;
procedure TButtonEdit.CreateHandle;
begin
inherited CreateHandle;
UpdateFormatRect;
end;
procedure TButtonEdit.UpdateFormatRect;
var
Rect: TRect;
begin
Rect := ClientRect;
if FButtonVisible then
Dec(Rect.Right, FButton.Height)
else
Inc(Rect.Right, FButton.Height);
SendMessage(Handle, EM_SETRECTNP, 0, Longint(@Rect));
end;
procedure TButtonEdit.WMSize(var Msg: TWMSize);
begin
inherited;
FButton.Width := FButton.Height;
UpdateFormatRect;
end;
procedure TButtonEdit.WMSetCursor(var Msg: TWMSetCursor);
var
P: TPoint;
PosWidth: Integer;
begin
GetCursorPos(P);
P := ScreenToClient(P);
PosWidth := ClientWidth;
if FButtonVisible then
PosWidth := PosWidth - FButton.Width;
if (P.X >= PosWidth) then
SetCursor(Screen.Cursors[crDefault])
else
inherited;
end;
procedure TButtonEdit.CMEnabledChanged(var Msg: TWMNoParams);
begin
inherited;
FButton.Enabled := Enabled;
end;
procedure TButtonEdit.SetButtonBounds;
begin
if not FButtonVisible then
FButton.Width := 0
else
FButton.Width := Height - 1;
UpdateFormatRect;
if not (csLoading in ComponentState) then
begin
SendMessage(Handle, EM_SETMARGINS, EC_LEFTMARGIN, 0);
SendMessage(Handle, EM_SETMARGINS, EC_RIGHTMARGIN, MakeLong(0, 2));
end;
end;
procedure TButtonEdit.SetButtonVisible(const Value: Boolean);
begin
if FButtonVisible <> Value then
begin
FButtonVisible := Value;
FButton.Visible := Value;
SetButtonBounds;
Invalidate;
end;
end;
procedure TButtonEdit.SetButtonFlat(const Value: Boolean);
begin
if FButtonFlat <> Value then
begin
FButtonFlat := Value;
FButton.Flat := Value;
Invalidate;
end;
end;
function TButtonEdit.GetButtonHint: string;
begin
Result := FButton.Hint;
end;
procedure TButtonEdit.SetButtonHint(const Value: string);
begin
FButton.Hint := Value;
end;
procedure TButtonEdit.BtnClickHandler(Sender: TObject);
begin
if Assigned(FOnButtonClick) then
FOnButtonClick(Self);
end;
end.