How to show a check box in TListView header column?
Asked Answered
V

2

3

I need to have a check box in a column header of a TListView:

enter image description here

I have tried the following code:

with CheckBox1 do
begin
  Parent := ListView1;
  Top := 0;
  Left := 4;
end;

but the check box doesn't always work as expected. How can I properly create a check box in TListView header column ?

Vivica answered 4/1, 2012 at 16:26 Comment(10)
Have you seen #5520242 ?Lenni
@AndreasRejbrand I don't think that question addresses drawing a checkbox in the column header?Loadstone
What does "CheckBox is little buggy" mean?Nekton
@Ken, it seems to me, that the check box in the LV header is slower to response (comparing to the check boxes in the column) like there would be a longer time for double click delay.Luetic
@TLama, the OP needs to clarify that in the question. Speculating or guessing here in the comments is just useless. If the OP edits to improve the description of the problem, it both makes the chances of getting a useful answer better and improves the usability of the question to others in the future. (For instance, your answer only shows what the OP has already shown - drawing the checkboxes. You haven't actually answered any question, though, because you've just reproduced the original question's image with text in a different character set.)Nekton
@Ken, now I see the question. I thought that OP just made a paint what he wants to achieve. I missed the code in the question. What is buggy is clear now, just try that code. If you hover the first column, the check box disappear because the header draws over it. So I guess I've answered the question; I don't think you will be able to handle header drawing event ;)Luetic
Quite closer would be to use instead the line Parent:=ListView1; this Windows.SetParent(Handle, ListView_GetHeader(ListView1.Handle)); but then the check box stops to respond to the clicks.Luetic
@TLama, since the question remains the same, and "CheckBox is little buggy" doesn't mean anything, I don't know what the question is and therefore can't say whether you've answered it or not. :) (I suspect you have, BTW; the OP still needs to ask the question to make it clear.)Nekton
@dredei, does any of the answers here helped you ? Could you give us some feedback, please ? And could you elaborate what does the little buggy mean ?Luetic
@TLama, tnx for help. Working! This bug: youtube.com/watch?v=_jK18nMAFas , but ur code normal working, without this bug.Vivica
L
7

The following code will add the check box to the list view's header and shows how to handle the click event for it.

Please note, that the following code is supported since Windows Vista.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
  private
    HeaderID: Integer;
    procedure WMNotify(var AMessage: TWMNotify); message WM_NOTIFY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMNotify(var AMessage: TWMNotify);
begin
  if AMessage.NMHdr^.idFrom = HeaderID then
    if AMessage.NMHdr^.code = HDN_ITEMSTATEICONCLICK then
      ShowMessage('You have clicked the header check box');

  inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  HeaderHandle: HWND;
  HeaderItem: HD_ITEM;
  HeaderStyle: Integer;
begin
  ListView_SetExtendedListViewStyle(ListView1.Handle, LVS_EX_CHECKBOXES or LVS_EX_FULLROWSELECT);
  HeaderHandle := ListView_GetHeader(ListView1.Handle);
  HeaderStyle := GetWindowLong(HeaderHandle, GWL_STYLE);
  HeaderStyle := HeaderStyle or HDS_CHECKBOXES;
  SetWindowLong(HeaderHandle, GWL_STYLE, HeaderStyle);

  HeaderItem.Mask := HDI_FORMAT;
  Header_GetItem(HeaderHandle, 0, HeaderItem);
  HeaderItem.fmt := HeaderItem.fmt or HDF_CHECKBOX or HDF_FIXEDWIDTH;
  Header_SetItem(HeaderHandle, 0, HeaderItem);

  HeaderID := GetDlgCtrlID(HeaderHandle);
end;

end.


enter image description here

Luetic answered 4/1, 2012 at 19:17 Comment(5)
@David, thanks. As they said here the HDS_CHECKBOXES header style is available from ComCtl32.dll version 6.00, which might be also on Windows XP. But I don't get why would this header style exists in Windows XP without appropriate notification.Luetic
No you are right. I'm talking rubbish again. Sorry! This is what you get when you include an XP manifest. I will delete my mess.Isometrics
But, irrespective, I'm sad to say that your program does not display the checkbox in the header on XP. I believe that what they mean by Vista and comctl32 v6 is that you must have both Vista and a comctl v6 manifest. In other words, if you run on vista but don't manifest comctl v6 then you don't get the header checkbox. So this is Vista only I fear.Isometrics
Is there anyway to make the row checkboxes read only? I have the listview set as such, but the checkboxes can still be toggled.Nuclide
@ikathegreat, I cannot find any style and e.g. ListView_SetCheckState macro allows just to set or reset check box checked state. So I think there is no way to disable those check boxes.Luetic
W
4

If you are targeting Vista and later, obvious answer is TLama's. If not, parent the check box in the header control, not in the list box (again as TLama commented to the question). The check box will send notifications to its parent - the header control, so you need to subclass it. Working sample:

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FListHeaderWnd: HWND;
    FListHeaderChk: TCheckBox;
    FSaveListHeaderWndProc, FListHeaderWndProc: Pointer;
    procedure ListHeaderWndProc(var Msg: TMessage);
  end;

var
  Form1: TForm1;

implementation

uses
  commctrl;

{$R *.dfm}

function GetCheckSize: TPoint;     // from checklst.pas
begin
  with TBitmap.Create do
    try
      Handle := LoadBitmap(0, PChar(OBM_CHECKBOXES));
      Result.X := Width div 4;
      Result.Y := Height div 3;
    finally
      Free;
    end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  CheckSize: TPoint;
  HeaderSize: TRect;
begin
  ListView1.HandleNeeded;
  FListHeaderWnd := ListView_GetHeader(ListView1.Handle);

  FListHeaderChk := TCheckBox.Create(nil);
  CheckSize := GetCheckSize;
  FListHeaderChk.Height := CheckSize.X;
  FListHeaderChk.Width := CheckSize.Y;

  // the below won't show anything since the form is not visible yet
  ShowWindow(ListView1.Handle, SW_SHOWNORMAL); // otherwise header is not sized
  windows.GetClientRect(FListHeaderWnd, HeaderSize);
  FListHeaderChk.Top := (HeaderSize.Bottom - FListHeaderChk.Height) div 2;
  FListHeaderChk.Left := FListHeaderChk.Top;

  FListHeaderChk.Parent := Self;
  windows.SetParent(FListHeaderChk.Handle, FListHeaderWnd);

  FListHeaderWndProc := classes.MakeObjectInstance(ListHeaderWndProc);
  FSaveListHeaderWndProc := Pointer(GetWindowLong(FListHeaderWnd, GWL_WNDPROC));
  SetWindowLong(FListHeaderWnd, GWL_WNDPROC, NativeInt(FListHeaderWndProc));
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  SetWindowLong(FListHeaderWnd, GWL_WNDPROC, NativeInt(FSaveListHeaderWndProc));
  classes.FreeObjectInstance(FListHeaderWndProc);
  FListHeaderChk.Free;
end;

procedure TForm1.ListHeaderWndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_COMMAND) and (HWND(Msg.LParam) = FListHeaderChk.Handle)
        and (Msg.WParamHi = BN_CLICKED) then begin
    FListHeaderChk.Checked := not FListHeaderChk.Checked;

    // code that checks/clears all items

  end;

  Msg.Result := CallWindowProc(FSaveListHeaderWndProc, FListHeaderWnd,
                               Msg.Msg, Msg.WParam, Msg.LParam);
end;

Note that if you've 'ColumnClick' set, it looks ugly that the check box does not 'push' with the header button when you click on it.

Wernsman answered 4/1, 2012 at 23:43 Comment(1)
+1 I'm adopting this :) but I'm breaking my head on how to make the checkbox not still focus from the ListView or other active controls...Tyronetyrosinase

© 2022 - 2024 — McMap. All rights reserved.