Delphi TListView 修改列表头高度、字体大小

本文介绍如何使用Delphi自定义ListView的Header部分样式,包括调整Header的高度、设置自定义字体等,适用于希望改善用户界面视觉效果的开发者。

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

unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, CommCtrl, WinSkinData;

type
  TForm2 = class(TForm)
    SkinData1: TSkinData;
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form2: TForm2;

  lvhOldProc, lvhNewProc: TFarProc;
  FLVHeader: HWND;

implementation

{$R *.dfm}

function LvHeaderNewWndProc(h: THandle; uMsg: UINT; wParam: wParam;
  lParam: lParam): LRESULT; stdcall;
var
  phdl: PHDLayout;
  prct: PRect;
  pwp: PWindowPos;
begin
  Result := CallWindowProc(lvhOldProc, h, uMsg, wParam, lParam);

  if uMsg = HDM_LAYOUT then
  begin
    phdl := PHDLayout(lParam);
    prct := PRect(phdl.Rect);
    pwp := PWindowPos(phdl.WindowPos);
    pwp.cy := pwp.cy + 50; // Header默认高是17,修改成17 + 10 = 27;
    prct.top := pwp.cy + 2;
  end;

end;

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SetWindowLong(FLVHeader, GWL_WNDPROC, Longint(lvhOldProc));

end;

procedure TForm2.FormCreate(Sender: TObject);
const
  LVM_GETHEADER = LVM_FIRST + 31;

var
  LF: TLogFont;
  hCurrFont, hOldFont, hHeaderFont: THandle;
begin
  FLVHeader := ListView_GetHeader(ListView1.Handle);

  lvhOldProc := FARPROC(GetWindowLong(FLVHeader, GWL_WNDPROC));

  SetWindowLong(FLVHeader, GWL_WNDPROC, Longint(@LvHeaderNewWndProc));

 {to get the windows handle for header}
  FLVHeader := SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);

  {to get the handle for header font}
  hCurrFont := SendMessage(FLVHeader, 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;
    LF.lfHeight := 35;
    LF.lfItalic := 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(FLVHeader, hHeaderFont);

    {to notify the listview header about changes}
    SendMessage(FLVHeader, WM_SETFONT, hHeaderFont, 1);
  end;
end;



end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值