- 1 year ago
Change background color of TListView header in older Delphi
In older versions of Delphi, such as Delphi 7 or earlier, changing the background color of the TListView header requires custom drawing. You can achieve this by handling the CustomDraw
event of the TListView and manually painting the header background. Here's an example:
- Add a TListView control to your form.
- Set the
OwnerDraw
property of the TListView toTrue
. - Write an event handler for the
CustomDraw
event of the TListView:
delphiprocedure TForm1.ListView1CustomDraw(Sender: TCustomListView; const ARect: TRect; var DefaultDraw: Boolean);
var
HeaderRect: TRect;
begin
if Sender is TListView then
begin
// Set the background color of the header
Sender.Canvas.Brush.Color := clRed;
// Calculate the rectangle of the header
HeaderRect := ARect;
HeaderRect.Bottom := Sender.Header.Height;
// Draw the header background
Sender.Canvas.FillRect(HeaderRect);
// Set DefaultDraw to False to prevent default drawing
DefaultDraw := False;
end;
end;
- Run the application, and the TListView header background color should be changed to red.
In the CustomDraw
event handler, we set the Brush.Color
property of the canvas to the desired background color. Then, we calculate the rectangle of the header and use FillRect
to draw the header background. Finally, we set DefaultDraw
to False
to prevent default drawing of the header.
Note that this approach applies to older versions of Delphi and may not be compatible with newer Delphi versions. In newer Delphi versions, there might be more straightforward methods to change the background color of the TListView header.