Mirror

How to hide the caret in a TEdit (Views: 100)


Problem/Question/Abstract:

Does anyone know how I might be able to suppress the text cursor in a TEdit, so it's not visible?

Answer:

For that you need to call the HideCaret API function after the control has processed the WM_SETFOCUS message. The OnEnter event is a tad too early for that, so if you do not want to create a TEdit descendent with an overriden message handler for WM_SETFOCUS you need to delay the action by posting a message to the form and have the handler for that message do the HideCaret.

The three edits on this example form share the same OnEnter handler (AllEditEnter), have ReadOnly = true and AutoSelect = false.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, stdctrls, jpeg;

const
  UM_HIDECARET = WM_USER + 101;
type
  TUMHideCaret = packed record
    msg: Cardinal;
    control: TWinControl;
  end;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    procedure AllEditEnter(Sender: TObject);
  private
    { Private declarations }
    procedure UMHideCaret(var msg: TUMHideCaret); message UM_HIDECARET;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AllEditEnter(Sender: TObject);
begin
  PostMessage(handle, UM_HIDECARET, wparam(sender), 0);
end;

procedure TForm1.UMHideCaret(var msg: TUMHideCaret);
begin
  HideCaret(msg.control.Handle);
end;

end.

Of course this is an excellent way to confuse the user, since there will be no indication where the focus is anymore when the user tabs into one of the edits...

<< Back to main page