DelphiFAQ Home Search:

Changing header style in a TListView control

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

How can I change the style of the header in a TListView control?

Answer:

Delphi's VCL does not provide this. You need to
  1. get the header handle for the listview
  2. read the current style attributes for the header
  3. modify a style
  4. apply the new style
  5. invalidate the listview (force a redraw)

See the code snipped below. The result is a 'flat' listview as shown in this screenshot on the left side:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ListView2: TListView;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  CommCtrl;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
const
  LVM_GETHEADER = LVM_FIRST+31;
var
  hHeader: THandle;
  Style: DWORD;
begin
  { get the header handle for the listview }
  hHeader := SendMessage(ListView1.Handle, LVM_GETHEADER, 0, 0);

  { get the current style attributes for the header }
  Style := GetWindowLong(hHeader, GWL_STYLE);

  { toggle the HDS_BUTTONS style }
  Style := Style xor HDS_BUTTONS;

  { apply the new style }
  SetWindowLong(hHeader, GWL_STYLE, Style);

  { invalidate the listview (force a redraw) }
  SetWindowPos(ListView1.Handle, Form1.Handle, 
               0, 0, 0, 0,
               SWP_NOZORDER or SWP_NOSIZE or 
               SWP_NOMOVE or SWP_DRAWFRAME)
end;

end.

Comments:

2006-03-15, 15:42:51
[hidden] from United States  
While setting the 'Style' property, is it possible to RIGHT_ALIGN the images in the cloumns header?

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: