Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to change the default highlight color of a TListBox Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
27-Aug-02
Category
VCL-General
Language
Delphi 2.x
Views
83
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Can anyone tell me how to change the default highlight color used in a TListBox? I 
need it to be clAqua instead of the standard Navy as the text in the listbox is 
made other colors in the OwnerDraw and you can't read some of them with the Navy 
selection color.

Answer:

Solve 1:

Check the 'State' parameter in the DrawItem event. It lets you know if the item is 
selected. If it is then use a different brush color.
1   
2   procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
3     Rect: TRect; State: TOwnerDrawState);
4   begin
5     if odSelected in State then
6       ListBox1.Canvas.Brush.Color := clAqua;
7     ListBox1.Canvas.FillRect(Rect);
8     ListBox1.Canvas.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
9   end;


Solve 2:

Set Style := lbOwnerDrawFixed and OnDrawItem := ListBoxDrawItem; . Remove the last 
line from the example if you want to have the focus rectangle.
10  
11  procedure TListBox.ListBoxDrawItem(Control: TWinControl; Index: Integer; Rect: 
12  TRect;
13    State: TOwnerDrawState);
14  begin
15    if (odSelected in State) then
16      Canvas.Brush.Color := clBlue
17    else
18      Canvas.Brush.Color := Color;
19    Canvas.FillRect(Rect);
20    Canvas.Font := Font;
21    SetTextAlign(Canvas.Handle, TA_LEFT or TA_TOP or TA_NOUPDATECP);
22    ExtTextOut(Canvas.Handle, Rect.Left + 2, Rect.Top + 1, ETO_CLIPPED or ETO_OPAQUE, 
23  @Rect,PChar(Items[Index]), Length(Items[Index]), nil);
24    if (odSelected in State) then
25      DrawFocusRect(Canvas.Handle, Rect);
26  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC