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 put an image (e.g sort arrow) on a listview column header 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
12-Sep-02
Category
VCL-General
Language
Delphi 5.x
Views
165
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

When sorting TListViews it is good practise to show which column is sorted and in 
which direction.

Answer:

Add this to your form:

1   uses commctrl;
2   
3   procedure TForm1.SetColumnImage(List: TListView; Column, Image: Integer; ShowImage:
4     Boolean);
5   var
6     Align, hHeader: integer;
7     HD: HD_ITEM;
8   begin
9     hHeader := SendMessage(List.Handle, LVM_GETHEADER, 0, 0);
10    with HD do
11    begin
12      case List.Columns[Column].Alignment of
13        taLeftJustify: Align := HDF_LEFT;
14        taCenter: Align := HDF_CENTER;
15        taRightJustify: Align := HDF_RIGHT;
16      else
17        Align := HDF_LEFT;
18      end;
19  
20      mask := HDI_IMAGE or HDI_FORMAT;
21  
22      pszText := PChar(List.Columns[Column].Caption);
23  
24      if ShowImage then
25        fmt := HDF_STRING or HDF_IMAGE or HDF_BITMAP_ON_RIGHT
26      else
27        fmt := HDF_STRING or Align;
28  
29      iImage := Image
30  
31    end;
32    SendMessage(hHeader, HDM_SETITEM, Column, Integer(@HD));
33  end;



Images are taken from the SmallImages list. You should call this function for each 
column, and set the ShowImage to TRUE for the column you sorted. You can do this in 
the OnColumnClick() function:


34  var
35    Ascending: boolean;
36  
37  procedure TForm1.ListViewColumnClick(Sender: TObject; Column: TListColumn);
38  var
39    i: integer;
40  begin
41    // Toggle column Tag
42    Column.Tag := 1 - Column.Tag; // 0 -> 1  ;  1 -> 0
43    // Determine sort order based on the value of the Tag
44    Ascending := Column.Tag = 1;
45  
46    // This loop displays the icon in the selected column.
47    for i := 0 to ListView.Columns.Count - 1 do
48      SetColumnImage(ListView, i, Column.Tag, i = Column.Index);
49  
50    // The CustomSort function is not covered in this
51    // article but is explained elsewhere in Delphi Knowledge Base
52    TListView(Sender).CustomSort(@SortByColumn, Column.Index);
53  end;


Problem:  Resizing the column header causes a WM_PAINT which will erase the image.
Solution: Override WM_PAINT and call SetColumnImage again from there. I used 
TApplicationEvents component from delphi 5. 
If someone knows a better solution please let me know.

			
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