不用自绘(owner draw)就改变TListView header的字体

本文介绍了一种不依赖于第三方库或自定义绘制的方式,通过API调用更改TListView头部字体的方法。具体步骤包括获取头部窗口句柄、获取当前字体句柄、设置自定义字体属性并应用新字体。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
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)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值