Author: Tomas Rutkauskas
How can I display a sort order arrow on the tiltle row in TListView control (on the
right side of the column caption)?
Answer:
The easiest way would be to add the arrow picture to the imagelist, assign it to
the listview's smallimages property and specify the image index to the column
(ImageIndex property of the TListColumn). Now you'll see the picture on the left
side of the column header.
Another approach would be to draw the header by yourself. In case you're working
with the standard (not overridden)TListView control, you can set the new window
proc to the header in the form's OnCreate event. In the new header's window
procedure you can check up if the WM_PAINT message is coming and perform custom
drawing for the header or its part. See the example below for details:
1 { ... }2 type3 TForm1 = class(TForm)
4 ListView1: TListView;
5 { ... }6 protected7 FHeader: longint;
8 FOldWndProc: pointer;
9 procedure HeaderWndProc(varmessage: TMessage);
10 end;
11 12 procedure TForm1.FormCreate(Sender: TObject);
13 begin14 FHeader := ListView_GetHeader(ListView1.Handle);
15 FOldWndProc := Pointer(GetWindowLong(FHeader, GWL_WNDPROC));
16 SetWindowLong(FHeader, GWL_WNDPROC,
17 integer(Classes.MakeObjectInstance(HeaderWndProc)));
18 end;
19 20 procedure TForm1.HeaderWndProc(varmessage: TMessage);
21 var22 XCanvas: TCanvas;
23 XDC: HDC;
24 XSizeRect: TRect;
25 begin26 if Assigned(FOldWndProc) then27 message.Result := CallWindowProc(FOldWndProc, FHeader, message.Msg,
28 message.WParam, message.LParam);
29 casemessage.Msg of30 WM_PAINT:
31 begin32 XCanvas := TCanvas.Create;
33 XDC := GetWindowDC(FHeader);
34 try35 XCanvas.Handle := XDC;
36 Windows.GetClientRect(FHeader, XSizeRect);
37 XCanvas.Brush.Color := clRed;
38 XCanvas.FillRect(XSizeRect);
39 {draw the new header's content here...}40 finally41 ReleaseDC(FHeader, XDC);
42 XCanvas.Free;
43 end;
44 end;
45 end;
46 end;