简体   繁体   中英

Flickering TSpeedButton in a TMaskEdit descendant

I try to create a TComboBox like component, inherited from TMaskEdit with a TSpeedButton inside the edit itself.

The problem when I type something into the edit the most of the button disappears (the right and the bottom edge still visible). If I move the mouse over the component or exit using TAB, the button appears again.

Here is the code:

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;

What do I miss?

Solved.

WS_CLIPCHILDREN flag have to be included in the CreateParams() AND the button must be placed on TWinControl descendant ( TPanel in my case) OR the button itself must be a TWinControl descendant (for example TButton ). Graphic controls has no Handle , thats the problem.

Modified code:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM