Solved

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

Posted on 2003-10-26
5
637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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