本人想自己写一个带Button的edit控件,看了TcxDBButtonEdit代码许久,都有点晕头了,里面涉及的代码太多,我只想简单的像他那样Edit右边有个按钮
谁有自己写过,代码提示一下,谢谢!!!!

解决方案 »

  1.   

    [email protected]晚上的时候我给你发一个!
      

  2.   

    现在有空写了一份:unit EditAndButtons;interfaceuses
      SysUtils, Classes, Controls, StdCtrls, Buttons, Messages;
      
    type
      TEditAndButton = class(TEdit)
      private
        { Private declarations }
        FBtn: TButton;
        FSpace: Integer;
        FOnBtnClick: TNotifyEvent;
        procedure SetSpace(const Value: Integer);
      protected
        { Protected declarations }
        procedure CMVISIBLECHANGED(var Message: TMessage); message CM_VISIBLECHANGED;
        procedure CMENABLEDCHANGED(var Message: TMessage); message CM_ENABLEDCHANGED;
        procedure CMFONTCHANGED(var message: TMessage); message CM_FONTCHANGED;
        procedure CMBIDIMODECHANGED(var message: TMessage); message CM_BIDIMODECHANGED;
        procedure SetParent(Aparent: TWinControl); override;
        procedure Notification(AComponent: TComponent; Operation: TOperation); override;
        procedure DoBtnClick(Sender: TObject);
      public
        { Public declarations }
        procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
      published
        { Published declarations }
        property Btn: TButton read FBtn;
        property Space: Integer read FSpace write SetSpace;
        property OnBtnClick: TNotifyEvent read FOnBtnClick write FOnBtnClick;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Samples', [TEditAndButton]);
    end;{ TEditAndButton }procedure TEditAndButton.CMBIDIMODECHANGED(var message: TMessage);
    begin
      inherited;  FBtn.BiDiMode := BiDiMode;
    end;procedure TEditAndButton.CMENABLEDCHANGED(var Message: TMessage);
    begin
      inherited;  FBtn.Enabled := Enabled;
    end;procedure TEditAndButton.CMFONTCHANGED(var message: TMessage);
    begin
      inherited;  FBtn.Font.Assign(Font);
    end;procedure TEditAndButton.CMVISIBLECHANGED(var Message: TMessage);
    begin
      inherited;  FBtn.Visible := Visible;
    end;constructor TEditAndButton.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);  FBtn := TButton.Create(Self);
      //FBtn.OnClick := DoBtnClick;
      FBtn.Caption := '';
      FBtn.FreeNotification(Self);
      FSpace := 3;
    end;destructor TEditAndButton.Destroy;
    begin
      
      inherited Destroy;
    end;procedure TEditAndButton.DoBtnClick(Sender: TObject);
    begin
      if Assigned(FOnBtnClick) then
        FOnBtnClick(FBtn); 
    end;procedure TEditAndButton.Notification(AComponent: TComponent;
      Operation: TOperation);
    begin
      inherited Notification(AComponent, Operation);  if (Operation = opRemove) and (AComponent = FBtn) then
        FBtn := nil; 
    end;procedure TEditAndButton.SetBounds(ALeft, ATop, AWidth, AHeight: Integer);
    begin
      inherited SetBounds(ALeft, ATop, AWidth, AHeight);  if FBtn <> nil then with FBtn do
      begin
        Left := ALeft + AWidth + Space;
        Top := ATop + (AHeight - Height) div 2;
      end;
    end;procedure TEditAndButton.SetParent(Aparent: TWinControl);
    begin
      inherited SetParent(AParent);  FBtn.Parent := AParent;
    end;procedure TEditAndButton.SetSpace(const Value: Integer);
    begin
      if FSpace <> Value then
      begin
        FSpace := Value;
        FBtn.Left := Left + Width + FSpace;
      end;
    end;end.