Hi Alfredo,
When the user drags the certain column header, the column header will send
a WM_NOTIFY message with HDN_BEGINTRACKW(A) code to the ListView, and when
the draging is over, a WM_NOTIFY message with HDN_ENDTRACKW(A) code will
also be sent to ListView control.
So, we can inherit from the ListView class, override WndProc method, then
intercept the WM_NOTIFY message with these 2 notify code to get the column
width draging event. Sample code lists below:
private const uint WM_NOTIFY=0x004E;
private const uint HDN_BEGINTRACKA = 0xFFFFFECE;
private const uint HDN_BEGINTRACKW = 0xFFFFFEBA;
private const uint HDN_ENDTRACKA = 0xFFFFFECD;
private const uint HDN_ENDTRACKW = 0xFFFFFEB9;
[StructLayout(LayoutKind.Sequential)]
public struct NMHDR
{
public uint hwndFrom;
public uint idfrom;
public uint code;
}
protected override void WndProc(ref Message m)
{
if(m.Msg==WM_NOTIFY)
{
NMHDR nm=(NMHDR)m.GetLParam(typeof(NMHDR));
if(nm.code==HDN_BEGINTRACKA||nm.code ==HDN_BEGINTRACKW)
{
//the draging begins, just save the column width setting, call
savecolumnsetting() method I listed below
}
else if(nm.code == HDN_ENDTRACKA ||nm.code == HDN_ENDTRACKW)
{
//draging is over
}
}
base.WndProc (ref m);
}
To record and restore the column width setting, we may write 2 methods for
our own, like this:
ArrayList al=new ArrayList();
public void savecolumnsetting()
{
al.Clear();
for(int i=0;i<this.Columns.Count;i++)
{
al.Add(this.Columns
.Width);
}
}
public void restorecolumnsetting()
{
for(int i=0;i<this.Columns.Count;i++)
{
this.Columns.Width=(int)al;
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Usage:
Because whenever we drag the column header, its original width setting is
saved in arraylist, we can use a button to invoke restorecolumnsetting()
method, then when we clicked the button, the listview headers will restore
to its original width.
It works well on my side, I will attach the sample project in this reply
for your information.
============================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.