ListView: handle column resize

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello. I am trying to handle the event of resizing a column (if there is one). For example when I resize with the mouse a column at runtime, a label should display the new width of that column. Thank you.
 
* =?Utf-8?B?QW5kcmVpIFppbmNh?= said:
Hello. I am trying to handle the event of resizing a column (if there
is one). For example when I resize with the mouse a column at runtime, a
label should display the new width of that column. Thank you.

Maybe you can base your implementation on this snippet:

<http://www.google.de/[email protected]>
 
Andrei Zinca said:
Hello. I am trying to handle the event of resizing a column (if there is one).
For example when I resize with the mouse a column at runtime, a label
should display the new width of that column. Thank you.

It's tricky, but not impossible. You need to intercept the
HDN_ITEMCHANGING message sent to the ListView control. That will fire when
the columns are resized, but may also fire on other occasions (I can't
remember), so you might have to implement something to tell whether the
columns have really been resized. In any case, here's some VB code:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure NMHDR
Public hWndFrom As IntPtr
Public idFrom As Integer
Public code As Integer
End Structure

Public Const WM_NOTIFY = &H4E
Public Const HDN_ITEMCHANGING = -320

Protected Overrides Sub WndProc(ByRef m As Message)
Dim headerInfo As NMHDR

MyBase.WndProc(m)

Select Case m.Msg
Case WM_NOTIFY
headerInfo = CType(m.GetLParam(GetType(NMHDR)), NMHDR)
If headerInfo.code = HDN_ITEMCHANGING Then [Do Whatever]
End Select
End Sub

I -think- that code is right. Let me know if it works or if you find a
problem!

Jeremy
 
Jeremy's code looks fine to me but use the HDN_TRACK
notification instead. It's fired while the user is resizing.
If you want to know when the user starts/stops resizing,
use HDN_BEGINTRACK/HDN_ENDTRACK
Note that all these come in two flavors (ANSI and Unicode)

Public Const HDN_FIRST As Integer = -300
Public Const HDN_BEGINTRACKA As Integer = HDN_FIRST-6
Public Const HDN_BEGINTRACKW As Integer = HDN_FIRST-26
Public Const HDN_ENDTRACKA As Integer = HDN_FIRST-7
Public Const HDN_ENDTRACKW As Integer = HDN_FIRST-27
Public Const HDN_TRACKA As Integer = HDN_FIRST-8
Public Const HDN_TRACKW As Integer = HDN_FIRST-28

/claes


Jeremy Todd said:
Andrei Zinca said:
Hello. I am trying to handle the event of resizing a column (if there is one).
For example when I resize with the mouse a column at runtime, a label
should display the new width of that column. Thank you.

It's tricky, but not impossible. You need to intercept the
HDN_ITEMCHANGING message sent to the ListView control. That will fire when
the columns are resized, but may also fire on other occasions (I can't
remember), so you might have to implement something to tell whether the
columns have really been resized. In any case, here's some VB code:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure NMHDR
Public hWndFrom As IntPtr
Public idFrom As Integer
Public code As Integer
End Structure

Public Const WM_NOTIFY = &H4E
Public Const HDN_ITEMCHANGING = -320

Protected Overrides Sub WndProc(ByRef m As Message)
Dim headerInfo As NMHDR

MyBase.WndProc(m)

Select Case m.Msg
Case WM_NOTIFY
headerInfo = CType(m.GetLParam(GetType(NMHDR)), NMHDR)
If headerInfo.code = HDN_ITEMCHANGING Then [Do Whatever]
End Select
End Sub

I -think- that code is right. Let me know if it works or if you find a
problem!

Jeremy
 
Thank you all for your help. I am on the right way with that code. Anyway, I realized that I must go back to Charles Petzolds' wonderful "Programming Windows" and improve my win32api programming skills then get back to c# and write an event like onColumnResize and a delegate for it.
 
Back
Top