Solved

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

Posted on 2003-10-26
5
621 Views
Last Modified: 2010-08-05
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
Comment
Question by:moonrise
  • 2
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
Ferruccio Accalai earned 125 total points
ID: 9625606
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9625857
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
 
LVL 5

Expert Comment

by:snehanshu
ID: 9625873
Oops...
Ferruccio68, I am way too late!
:)
...S
0
 

Author Comment

by:moonrise
ID: 9679520
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
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 9679853
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now