To change a font for TListView header
|
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.
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) |
本文介绍了一种不依赖于第三方库或自定义绘制的方式,通过API调用更改TListView头部字体的方法。具体步骤包括获取头部窗口句柄、获取当前字体句柄、设置自定义字体属性并应用新字体。

被折叠的 条评论
为什么被折叠?



