Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 660
  • Last Modified:

TListBox - how to not show the rectangle around the item with the focus

I use a TListBox with lbOwnerDrawFixed - when the ListBox has the focus, the selected item has a rectangle around it. I would like to get rid of that rectangle that shows the focus. How can I di that?
0
moonrise
Asked:
moonrise
  • 2
  • 2
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
you can create a TListBox descendant and rewrite the CNDrawItem procedure

Look at the following:

unit Unit1;

interface

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

type
  TMyListBox = Class(TListBox)
    private
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
  end;      
  TForm1 = class(TForm)
      procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  MyListBox: TMyListBox;
implementation

{$R *.dfm}

procedure TMyListBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  with Message.DrawItemStruct^ do
  begin
    State := TOwnerDrawState(LongRec(itemState).Lo);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;
    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := clHighlight;
      Canvas.Font.Color := clHighlightText
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State) else
      Canvas.FillRect(rcItem);
    // this is the part that draw the focus rect; you must rem it
    //if odFocused in State then DrawFocusRect(hDC, rcItem);
    Canvas.Handle := 0;
  end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
MyListBox := tmylistbox.Create(self);
MyListBox.Parent := Form1;
MyListBox.Top := 20;
MyListBox.Left := 20;
MyListBox.Style := lbOwnerDrawFixed;
for i := 0 to 10 do
      MyListBox.Items.Add(inttostr(i));
end;

F68 ;-)
0
 
snehanshuCommented:
MoonRise,
  Here's code for a component to do what you want.
  You would have to set the style property of the listbox (This Listbox) to
lbOwnerDrawFixed
or
lbOwnerDrawVariable
  to get it work the way you want.
The component has two properties: ShowFocus and ShowSelection to turn those features on and off.
Cheers...
Snehanshu

Component code:

unit MyListBox;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;

type
  TMyListBox = class(TListBox)
  private
    { Private declarations }
    FShowSelection, FShowFocus: Boolean;
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
  protected
    { Protected declarations }
  public
    { Public declarations }
  published
    { Published declarations }
    property ShowSelection: Boolean read FShowSelection write FShowSelection;
    property ShowFocus: Boolean read FShowFocus write FShowFocus;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('ExEx', [TMyListBox]);
end;

{ TListBox1 }

procedure TMyListBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  with Message.DrawItemStruct^ do
  begin
    State := TOwnerDrawState(LongRec(itemState).Lo);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;
    if not FShowFocus then
      State := State - [odFocused];
    if not FShowSelection then
      State := State - [odSelected];
    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := clHighlight;
      Canvas.Font.Color := clHighlightText
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State) else
      Canvas.FillRect(rcItem);
    if odFocused in State then DrawFocusRect(hDC, rcItem);
    Canvas.Handle := 0;
  end;
end;

end.
0
 
snehanshuCommented:
Oops...
Ferruccio68, I am way too late!
:)
...S
0
 
moonriseAuthor Commented:
Hi, it worked great for a ListBox, now I am trying to do the same for a TCheckListBox but can't seem to find similar code. Do you know what the code would look like to do the same with a CheckListBox? Let me know if I need to create another question. Thank you.
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
mmm...it's quite simple.... (and if you want to give some additional points it'd be very appreciated :))

in CheckListBox.CNDrawItem there's a  difference from ListBox just for the indent measurement depending on the CheckBox width.
then it inherits the CustomListBox CNDRAWITEM.

So we must combine this measurement part with the one writed for TMyListBox...

well, here's the example (all 2 possibilities):

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CheckLst;
type
  TMycheckListBox = Class(TCheckListBox)
    private
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
  end;
  TMyListBox = Class(TListBox)
    private
    procedure CNDrawItem(var Message: TWMDrawItem); message CN_DRAWITEM;
  end;      
  TForm1 = class(TForm)
      procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
  MyListBox: TMyListBox;
  MyCheckListBox: TMyCheckListBox;
implementation

{$R *.dfm}
procedure TMyListBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  with Message.DrawItemStruct^ do
  begin
    State := TOwnerDrawState(LongRec(itemState).Lo);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;
    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := clHighlight;
      Canvas.Font.Color := clHighlightText
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State) else
      Canvas.FillRect(rcItem);
    // this is the part that draw the focus rect; you must rem it
    //if odFocused in State then DrawFocusRect(hDC, rcItem);
    Canvas.Handle := 0;
  end;
end;
procedure TMycheckListBox.CNDrawItem(var Message: TWMDrawItem);
var
  State: TOwnerDrawState;
begin
  if Items.Count = 0 then exit;
  with Message.DrawItemStruct^ do begin
    if not Header[itemID] then
      if not UseRightToLeftAlignment then
        rcItem.Left := rcItem.Left + GetCheckWidth
      else
        rcItem.Right := rcItem.Right - GetCheckWidth;
    State := TOwnerDrawState(LongRec(itemState).Lo);
    Canvas.Handle := hDC;
    Canvas.Font := Font;
    Canvas.Brush := Brush;
    if (Integer(itemID) >= 0) and (odSelected in State) then
    begin
      Canvas.Brush.Color := clHighlight;
      Canvas.Font.Color := clHighlightText
    end;
    if Integer(itemID) >= 0 then
      DrawItem(itemID, rcItem, State) else
      Canvas.FillRect(rcItem);
    // this is the part that draw the focus rect; you must rem it
    //if odFocused in State then DrawFocusRect(hDC, rcItem);
    Canvas.Handle := 0;
    end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
MyListBox := tmylistbox.Create(self);
MyListBox.Parent := Form1;
MyListBox.Top := 20;
MyListBox.Left := 20;
MyListBox.Style := lbOwnerDrawFixed;
MycheckListBox := tmychecklistbox.Create(self);
MycheckListBox.Parent := Form1;
MycheckListBox.Top := 20;
MycheckListBox.Left := 200;
MycheckListBox.Style := lbOwnerDrawFixed;
for i := 0 to 10 do begin
     MyListBox.Items.Add(inttostr(i));
     MycheckListBox.Items.Add(inttostr(i));
     end;
end;
end.

F68 ;-)

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now