Delphi TMaskEdit子体中闪烁的TSpeedButton
我尝试创建一个类似于Delphi TMaskEdit子体中闪烁的TSpeedButton,delphi,delphi-7,Delphi,Delphi 7,我尝试创建一个类似于TComboBox的组件,该组件继承自TMaskEdit,在编辑本身内部有一个TSpeedButton 当我在“编辑”中键入内容时,按钮的大部分消失(右侧和底部边缘仍然可见)。如果将鼠标移到组件上或使用TAB退出,则该按钮将再次出现 代码如下: interface uses Windows, Messages, SysUtils, Classes, Controls, Forms, Mask, Buttons; type TMyEdit = class(TCust
TComboBox
的组件,该组件继承自TMaskEdit
,在编辑本身内部有一个TSpeedButton
当我在“编辑”中键入内容时,按钮的大部分消失(右侧和底部边缘仍然可见)。如果将鼠标移到组件上或使用TAB退出,则该按钮将再次出现
代码如下:
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Forms, Mask, Buttons;
type
TMyEdit = class(TCustomMaskEdit)
private
FButton: TSpeedButton;
protected
procedure CreateButton;
public
constructor Create(AOwner: TComponent); override;
procedure CreateWnd; override;
destructor Destroy; override;
end;
implementation
constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited;
CreateButton;
end;
procedure TMyEdit.CreateButton;
begin
FButton := TSpeedButton.Create(Self);
FButton.Parent := Self;
FButton.Align := alRight;
FButton.Width := 16;
FButton.Caption := '';
FButton.Transparent := False;
end;
destructor TMyEdit.Destroy;
begin
FreeAndNil(FButton);
inherited;
end;
procedure TMyEdit.CreateWnd;
begin
inherited;
Perform(EM_SETMARGINS, EC_RIGHTMARGIN, (FButton.Width + 4) shl 16);
end;
我错过了什么?解决了
WS_CLIPCHILDREN
标志必须包含在CreateParams()
中,并且按钮必须放置在TWinControl
子体(TPanel
)上,或者按钮本身必须是TWinControl
子体(例如TButton
)。图形控件没有句柄
,这就是问题所在
修改代码:
procedure TMyEdit.CreateButton;
var
xDrawRect: TRect;
xPanel : TPanel;
begin
xPanel := TPanel.Create(Self);
xPanel.Parent := Self;
xPanel.SetBounds(Width - Height, 0, Height, Height);
xPanel.BevelOuter := bvNone;
FButton := TSpeedButton.Create(Self);
FButton.Parent := xPanel;
FButton.Align := alClient;
FButton.Caption := '';
end;
procedure TMyEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or WS_CLIPCHILDREN;
end;
您应该查看TButtonEdit和TCustomButtonEdit源代码。它继承自TCustomEdit和add按钮。您应该-可能-复制代码,并使您的代码从TCutomMaskEdit继承。忘记提到我使用D7,没有TCUSTOMBUTNEDDIT之类的东西。编辑您的问题以添加该信息。使用Delphi-7标签。为什么不升级到最新的Delphi版本(有一个免费的社区版,甚至可以用于小型企业(条件适用))。