Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

808 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