Mega Code Archive

 
Categories / Delphi / VCL
 

How to add a double click event to the header of a Listview

Title: How to add a double-click event to the header of a Listview uses commctrl; function HeaderProc(wnd: HWND; Msg: Cardinal; wParam: wParam; lParam: lParam): Longint; stdcall; var hti: THDHitTestInfo; begin Result := CallWindowProc(Pointer(GetWindowLong(wnd, GWL_USERDATA)), wnd, Msg, wParam, lParam); if Msg = WM_LBUTTONDBLCLK then begin FillChar(hti, SizeOf(hti), 0); hti.Point := SmallPointToPoint(TSmallPoint(lParam)); if SendMessage(wnd, HDM_HITTEST, 0, Longint(@hti)) = 0 then if hti.Flags = HHT_ONHEADER then // would usually send a custom notification to GetParent(wnd) here Form1.Memo1.Lines.Add(Format('doubleclick on header item %d', [hti.Item])); end; end; procedure TForm1.Button1Click(Sender: TObject); var wnd: HWND; buffer: array [0..80] of Char; oldProc: Integer; begin wnd := GetWindow(Listview1.Handle, GW_CHILD); if wnd 0 then begin Windows.GetClassname(wnd, buffer, SizeOf(buffer)); memo1.Text := buffer; if (GetClassLong(wnd, GCL_STYLE) and CS_DBLCLKS) 0 then begin Memo1.Lines.Add('Has doubleclicks style'); oldproc := GetWIndowLong(wnd, GWL_WNDPROC); if GetWindowLong(wnd, GWL_USERDATA) 0 then raise Exception.Create('Cannot sublcass header, USERDATA already in use'); SetWIndowLong(wnd, GWL_USERDATA, oldproc); SetWindowLong(wnd, GWL_WNDPROC, Integer(@HeaderProc)); end; end else Memo1.Text := 'No child'; end;