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

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.

你可能感兴趣的:(Delphi)