Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to completely remove the selection bar from a TRichEdit control?

On the left side of every line in a TRichEdit control there's an invisible space where the cursor changes to a right-up arrow and when you click there the entire line is selected. It's easy to see it when the TRichEdit's text alignment is Center or Right. I believe this space is called a selection bar.

Such a bar doesn't exist in the TMemo control.

My question:

How to remove this selection bar, so that the cursor behaviour would be the same as in TMemo?

I'm using Delphi 7 and there are no TRichEdit properties to control this behaviour.

There's an ECO_SELECTIONBAR value you can use with the EM_SETOPTIONS message, but it only adds or removes a small portion of the selection bar (only useful when you want to add a selection bar to a TRichEdit that has a Left Alignment).

like image 418
jedivader Avatar asked Jun 09 '13 19:06

jedivader


1 Answers

Thanks everyone for your answers.

As there seems to be no "proper" way to do this, I devised the following solution:

unit TRichEditRemoveSelectionBar;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure RichEdit1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  B: Boolean = False;

implementation

{$R *.dfm}

// ------------------------------------------------------------------------- //

procedure TForm1.RichEdit1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if (GetCursor <> Screen.Cursors[crDefault]) and
     (GetCursor <> Screen.Cursors[crIBeam]) then
  begin
    SetCursor(Screen.Cursors[crIBeam]);
    B := True;
  end else
    B := False;
end;

procedure TForm1.RichEdit1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if B then
  begin
    SetCursor(Screen.Cursors[crIBeam]);
    RichEdit1.SelLength := 0;
  end;
end;

procedure TForm1.RichEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if B then
    SetCursor(Screen.Cursors[crIBeam]);
end;

// ------------------------------------------------------------------------- //

end.

It's not elegant at all, but it gets the job done.

Note that this code doesn't allow for a double-click full row selection and it doesn't handle triple-click full text selection. For that you'll probably have to use an interceptor class for example.

like image 94
jedivader Avatar answered Nov 01 '22 15:11

jedivader