Solved

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

Posted on 2003-10-26
5
615 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

21 Experts available now in Live!

Get 1:1 Help Now