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

3

9

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).

Remainder answered 9/6, 2013 at 19:43 Comment(1)
+1 good question and I don't think you've any way to disable this behaviourMessmate
R
1

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.

Remainder answered 12/6, 2013 at 15:19 Comment(0)
Y
0

Try using SetWindowLong() to remove the ES_SELECTIONBAR window style from the RichEdit, eg:

dwStyle := GetWindowLong(RichEdit1.Handle, GWL_STYLE);
SetWindowLong(RichEdit1.Handle, GWL_STYLE, dwStyle and not ES_SELECTIONBAR);

Alternatively, derive a new component from TRichEdit, or use an interceptor class, to override the virtual CreateParams() method to remove the style:

type
  TMyRichEdit = class(TRichEdit)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

Procedure TMyRichEdit.CreateParams(var Params: TCreateParams);
Begin
  inherited;
  Params.Style := Params.Style and not ES_SELECTIONBAR;
End;
Yurev answered 10/6, 2013 at 1:7 Comment(1)
ES_SELECTIONBAR is never added to the styles so removing what isn't there changes nothing.Messmate
M
0

There is not documented way to disable this behaviour for the rich edit control. There are not styles, messages or functions that offer any way to disable this behaviour.

The ES_SELECTIONBAR style that you have mentioned allows a small margin to be added when the text is left aligned. The Delphi wrapper to the rich edit control does not include the ES_SELECTIONBAR style so it's not as if you can remove it since it's never there in the first place.

For centred and right aligned text, the selection area is always present, irrespective of the presence or otherwise of the ES_SELECTIONBAR style. In fact the ES_SELECTIONBAR style appears to make no difference at all to the control's behaviour for centred and right aligned text.

I expect that if you reverse engineered the implementation of this selection zone, you'd be able to remove the behaviour by modifying the rich edit control's window procedure.

Messmate answered 10/6, 2013 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.