Title: to change a font for TListView
Today I want to show how to use the API calls and change a font for
ListView header without any extended programming and custom drawing.
For example, you have the ListView1 instance and want to set a bold font
for header.
This task must be solved so:
1. to retrieve a handle of header for list view
2. to get a font object by header handle
3. to define our font object with any custom attributes
4. to set our new font for header
The code below will do it:
const
LVM_GETHEADER = LVM_FIRST + 31;
var
LF: TLogFont;
hHeader, hCurrFont, hOldFont, hHeaderFont: THandle;
begin
{to get the windows handle for header}
hHeader := SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);
{to get the handle for header font}
hCurrFont := SendMessage(hHeader, WM_GETFONT, 0, 0);
{to get the LOGFONT with font details}
if GetObject(hCurrFont, SizeOf(LF), Addr(LF)) 0 then
begin
{set our custom attributes. I set a bold and underlined font style}
LF.lfWeight := FW_BOLD;
LF.lfUnderline := 1;
{create a new font for the header control to use.
This font must NOT be deleted until it is no
longer required by the control, typically when
the application will be closed or when a new font
will be applied to header}
hHeaderFont := CreateFontIndirect(LF);
{to select the new font}
hOldFont := SelectObject(hHeader, hHeaderFont);
{to notify the listview header about changes}
SendMessage(hHeader, WM_SETFONT, hHeaderFont, 1);
end;
end;
Note that somewhere in OnFormClose event you must also to release a
memory for hHeaderFont variable:
if hHeaderFont 0 then
DeleteObject(hHeaderFont)