Jump to content
vshvetsov

how to respond on user drag of a column border in a listview

Recommended Posts

Hi,

how can I write a respond to the event, that user has changed the width of a column in a TListView (report view) by dragging a column border with a mouse?

Share this post


Link to post

The column headers of a listview is in fact a separate header control owned by the listview. The column resize notifications are only sent to the parent of the header control (i.e. the listview) so in order to get at them you need to hook into the WndProc of the listview and catch the HDN_BEGINTRACK and HDN_ENDTRACK messages.

 

I'm guessing @Remy Lebeau has some code..?

  • Thanks 1

Share this post


Link to post

Off the top of my head, something like this:

private
  OldListViewWndProc: TWndMethod;
  procedure MyListViewWndProc(var Message: TMessage);

...

uses
  ..., Winapi.CommCtrl, Winapi.Messages;
  
procedure TMyForm.FormCreate(Sender: TObject);
begin
  OldListViewWndProc := ListView1.WindowProc;
  ListView1.WindowProc := MyListViewWndProc;
end;

procedure TMyForm.MyListViewWndProc(var Message: TMessage);
var
  pnmhdr: PHDNotifyW;
begin
  if Message.Msg = WM_NOTIFY then
  begin
    case TWMNotify(Message).NMHdr^.code of 
      HDN_BEGINTRACKW,
      HDN_ENDTRACKW,
      HDN_TRACKW:
        pnmhdr := PHDNotifyW(TWMNotify(Message).NMHdr);
        // use pnmhdr^ fields as needed:
        // - pnmhdr^.Item is the index of the column being tracked
        // - pnmhdr^.Button is the mouse button generating the message: 0=left, 1=right, 2=middle
        // - pnmhdr^.PItem is a pointer to a THDItemW describing the changes to the tracked column
        //
        // to block changes, set Message.Result := 1 and Exit here...
    end;
  end;
  OldListViewWndProc(Message);
end;

 

  • Thanks 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×