Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double-clicking TStaticText in Delphi XE2 app copies caption to clipboard

Double-clicking a TStaticText on a form copies the caption of that TStaticText to the clipboard. No double-click event is needed.

Steps to reproduce:

  • Using Win 64 and Delphi XE2 update 4.
  • Create a VCL Forms app.
  • Place a TEdit on the form.
  • Place a TStaticText on the form. Change caption to "TStaticText1Caption"
  • Place a second TStaticText on the form. Change caption to "TStaticText2Caption"
  • Run program(F9)
  • Type some text into the TEdit. Select it all and copy it via CTRL+C.
  • Delete the text in the TEdit. Paste it in to verify the text is what you copied.
  • Delete the text in the TEdit.
  • Double-click either TStaticText.
  • Paste text into TEdit. Notice it is not the original copied text but the caption of the TStaticText.

I have already submitted a bug report to Embarcadero.

I tried assigning a double-click event to the TStaticTexts. It still copies the caption to the clipboard even though it executes the double-click event.

procedure TForm1.StaticText1DblClick(Sender: TObject);
begin
  Edit1.Text := 'Hello';
end;

procedure TForm1.StaticText2DblClick(Sender: TObject);
begin
  Edit1.Text := 'World';
end;

This does not happen with TLabel or any other VCL control I have tried.

We have lots of TStaticTexts on our forms for visual design purposes and changing to TLabels is not an option.

Anybody have any ideas on how to prevent this from happening?

like image 258
TJ Asher Avatar asked Jun 20 '12 14:06

TJ Asher


1 Answers

This is not a delphi bug, this behaviour is caused by the Windows Static Control which is created by the TStaticText VCL component.

Starting in Windows Vista, the Static text controls automatically copy their contents to the clipboard when you double-click them if you set the SS_NOTIFY style (the SS_NOTIFY style is set by the CreateParams method of the TCustomStaticText component)

Recomended lecture How do I make it so that users can copy static text on a dialog box to the clipboard easily?

As workaround you can remove the SS_NOTIFY style overriding the CreateParams method like so

type
  TStaticText = class(Vcl.StdCtrls.TStaticText)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

  TForm1 = class(TForm)
    StaticText1: TStaticText;
  private
  public
  end;

var
  Form1: TForm42;

implementation

{$R *.dfm}

{ TStaticText }

procedure TStaticText.CreateParams(var Params: TCreateParams);
begin
  inherited;
  with Params do
    Style := Style and not SS_NOTIFY;
end;

Note : you must be aware if you remove this style from the control you will no receive the STN_CLICKED, STN_DBLCLK, STN_DISABLE, and STN_ENABLE notification codes when the user clicks or double-clicks the control.

like image 127
RRUZ Avatar answered Oct 23 '22 06:10

RRUZ