Hi I'm trying to build an TEdit control with TButton to have Buttoned Edit but the problem is that the text clip gets under the Button and some latter doesn't appear because the button is over it. how to fix that? please note that when I call UpdateEditMargins (which is the procedure to adjust the text clip)
Here is my code:
unit YazButtonedEdit;
interface
uses
SysUtils, Classes, Controls, StdCtrls, Buttons, Messages, Windows, Forms;
type
TYazButtonedEdit = class(TCustomEdit)
private
FEditButton: TBitBtn;
FButtonWidth: Integer;
FButtonVisible: Boolean;
procedure WMSize(var Message: TMessage); message WM_SIZE;
procedure SetButtonVisible(const Value: Boolean);
procedure GetEditButtonClick(const Value: TNotifyEvent);
function SetEditButtonClick: TNotifyEvent;
procedure SetButtonWidth(const Value: Integer);
protected
procedure RefreshButton;
procedure CreateParams(var Params: TCreateParams); override;
procedure WndProc(var Message: TMessage); override;
public
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure UpdateEditMargins;
published
property ButtonWidth: Integer read FButtonWidth write SetButtonWidth;
property ButtonVisible: Boolean read FButtonVisible write SetButtonVisible;
property OnEditButtonClick: TNotifyEvent read SetEditButtonClick write GetEditButtonClick;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('KH-Controls', [TYazButtonedEdit]);
end;
{ TYazButtonedEdit }
constructor TYazButtonedEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle - [csSetCaption];
FEditButton := TBitBtn.Create(self);
with FEditButton do begin
Parent := self;
TabStop := false;
Visible := true;
OnClick := OnEditButtonClick;
end;
end;
procedure TYazButtonedEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;
destructor TYazButtonedEdit.Destroy;
begin
FEditButton.Free;
inherited;
end;
procedure TYazButtonedEdit.GetEditButtonClick(const Value: TNotifyEvent);
begin
FEditButton.OnClick := Value;
end;
procedure TYazButtonedEdit.RefreshButton;
begin
FEditButton.Width := ButtonWidth;
FEditButton.Height := Height - 4;
FEditButton.Visible := ButtonVisible;
UpdateEditMargins;
end;
procedure TYazButtonedEdit.SetButtonVisible(const Value: Boolean);
begin
if FButtonVisible <> Value then
begin
FButtonVisible := Value;
RefreshButton;
end;
end;
procedure TYazButtonedEdit.SetButtonWidth(const Value: Integer);
begin
if FButtonWidth <> Value then
begin
FButtonWidth := Value;
RefreshButton;
end;
end;
function TYazButtonedEdit.SetEditButtonClick: TNotifyEvent;
begin
Result := FEditButton.OnClick;
end;
procedure TYazButtonedEdit.WMSize(var Message: TMessage);
begin
RefreshButton;
end;
procedure TYazButtonedEdit.WndProc(var Message: TMessage);
var
LLeft, LTop: Integer;
begin
case Message.Msg of
CN_CTLCOLORSTATIC,
CN_CTLCOLOREDIT:
if FEditButton.Visible then
begin
LLeft := FEditButton.Left;
LTop := FEditButton.Top;
ExcludeClipRect(Message.WParam, LLeft + 1, LTop + 1,
FEditButton.Width + FEditButton.Left - 1, FEditButton.Height - 1);
end;
end;
inherited;
end;
procedure TYazButtonedEdit.UpdateEditMargins;
var
LMargin, RMargin: Integer;
begin
if HandleAllocated then
begin
LMargin := 0;
RMargin := 0;
if FEditButton.Visible then
LMargin := FEditButton.Width + 2;
SendMessage(Handle, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, MakeLong(LMargin, RMargin));
Invalidate;
end;
end;
procedure TYazButtonedEdit.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if Operation = opRemove then
begin
if AComponent = FEditButton then
begin
RefreshButton;
end;
end;
end;
end.
Call it after the edit window is created, ideally in CreateWnd method. So, add the following:
type
TYazButtonedEdit = class(TCustomEdit)
...
protected
procedure CreateWnd; override;
...
end;
implementation
procedure TYazButtonedEdit.CreateWnd;
begin
inherited;
UpdateEditMargins;
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With