The ListView control allows you to reorder and resize columns, but a cannot
find out how to catch events coming from the header. I have looked at all
the WM_NOTIFY events that come through. Only the click seems to fire.

I would like to be able to save the changes to the header that are made by
the user. How do I do this?

Thanks for any help.

Alfredo

Re: ListView control header events? by aualias

aualias
Mon Nov 08 14:10:38 CST 2004

Jeffrey,

Thanks for the sample. I never actually checked for resizing - I just saw
stuff on the Internet that indicated that no header messages are sent and
assumed that it was true. What I looked at, and what I am most interested
in, are the messages sent when you reorder the column headers.

I did look for the reorder messages and found nothing that matched values in
commctrl.h. ( I did find some messages that came between the gaps in
sequential values in commctrl.h, but I do not know what to do with them. )

Alfredo



""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:NuZj75TxEHA.2544@cpmsftngxa10.phx.gbl...
> 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[i].Width);
> }
> }
>
> public void restorecolumnsetting()
> {
> for(int i=0;i<this.Columns.Count;i++)
> {
> this.Columns[i].Width=(int)al[i];
> }
> }
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 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.



Re: ListView control header events? by v-jetan

v-jetan
Mon Nov 08 20:58:55 CST 2004

Hi Alfredo,

Thanks very much for your feedback!

Yes, I see your concern is on reorder issue now. But, winform listview does
not support column headers reorder. I am not sure what does your "column
header reorder" mean. Does it mean the behavior: if we click certain column
header all the rows will be reordered alphabetically? I think this should
be the resort behavior of the one column.

Anyway, the reoder of column headers is not supported and please confirm
the meaning of "column header reorder" for me, so I can help you better.
Thanks
============================================================
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.


Re: ListView control header events? by Claes

Claes
Tue Nov 09 01:45:41 CST 2004

Sounsd like you're looking for HDN_BEGINDRAG and HDN_ENDDRAG

private const int WM_NOTIFY = 0x004E;
private const int HDN_FIRST = -300
private const int HDN_BEGINDRAG = HDN_FIRST-10;
private const int HDN_ENDDRAG = HDN_FIRST-11;

Note that the AllowColumnReorder property must be true

/claes


"aualias" <aualias@newsgroups.nospam> wrote in message
news:#tKEF8cxEHA.1300@TK2MSFTNGP14.phx.gbl...
> Jeffrey,
>
> Thanks for the sample. I never actually checked for resizing - I just saw
> stuff on the Internet that indicated that no header messages are sent and
> assumed that it was true. What I looked at, and what I am most interested
> in, are the messages sent when you reorder the column headers.
>
> I did look for the reorder messages and found nothing that matched values
in
> commctrl.h. ( I did find some messages that came between the gaps in
> sequential values in commctrl.h, but I do not know what to do with them. )
>
> Alfredo
>
>
>
> ""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
> news:NuZj75TxEHA.2544@cpmsftngxa10.phx.gbl...
> > 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[i].Width);
> > }
> > }
> >
> > public void restorecolumnsetting()
> > {
> > for(int i=0;i<this.Columns.Count;i++)
> > {
> > this.Columns[i].Width=(int)al[i];
> > }
> > }
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > 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.
>
>



Re: ListView control header events? by v-jetan

v-jetan
Tue Nov 09 03:21:58 CST 2004

Oh, yes, I think Claes should be right. I missed
ListView.AllowColumnReorder property.

After enabling the column reorder, we can do the similiar as my last reply
sample code to intercept the WM_NOTIFY message and determine the
HDN_BEGINDRAG and HDN_ENDDRAG notification code. The program logic is the
same as my last reply's sample code.

Hope this helps

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.


Re: ListView control header events? by aualias

aualias
Tue Nov 09 13:57:20 CST 2004

Jeffrey and Claes,

Interesting...

I am getting the codes HDN_BEGINDRAG and HDN_ENDDRAG (0xFFFFFECA and
0xFFFFFEC9) in your demo app, but not in mine. I have to check what else is
going on.

Thanks for your help.

David


""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:UabUW2jxEHA.3984@cpmsftngxa10.phx.gbl...
> Oh, yes, I think Claes should be right. I missed
> ListView.AllowColumnReorder property.
>
> After enabling the column reorder, we can do the similiar as my last reply
> sample code to intercept the WM_NOTIFY message and determine the
> HDN_BEGINDRAG and HDN_ENDDRAG notification code. The program logic is the
> same as my last reply's sample code.
>
> Hope this helps
>
> 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.
>



Re: ListView control header events? by aualias

aualias
Tue Nov 09 14:50:03 CST 2004

Hi Jeffrey,

I discovered my problem...
I thought that the notifications were coming from the ListView and was
filtering the messages. However, duh, they were coming from the header.

One more problem, though...
After reordering the columns, how do I detect the new configuration so that
I can save it? I did a dump of the columns before and after reordering and
there was no change in the order of the collection.

Here is the dump method:
private void DumpListView(ListView lv)
{
Trace.WriteLine("\r\nDumping Column Headers...");

for ( int i = 0; i < lv.Columns.Count; i++ )
Trace.WriteLine(lv.Columns[i].Text);

// variation - same result...
// foreach ( ColumnHeader hdr in lv.Columns )
// {
// Trace.WriteLine(hdr.Text);
// }

Trace.WriteLine("End Dump Column Headers...\r\n");
}

Thanks.

Alfredo



""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:UabUW2jxEHA.3984@cpmsftngxa10.phx.gbl...
> Oh, yes, I think Claes should be right. I missed
> ListView.AllowColumnReorder property.
>
> After enabling the column reorder, we can do the similiar as my last reply
> sample code to intercept the WM_NOTIFY message and determine the
> HDN_BEGINDRAG and HDN_ENDDRAG notification code. The program logic is the
> same as my last reply's sample code.
>
> Hope this helps
>
> 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.
>



Re: ListView control header events? by aualias

aualias
Wed Nov 10 10:09:27 CST 2004

Jeffrey,

Thank you for the example. And thanks for the quick response. I was just
in the process of figuring out how to marshal an array for SendMessage(),
but was not coming up with any examples. Looking at how you did it, it is
pretty obvious - but I was thinking of doing custom marshaling. I did not
think of changing the footprint for SendMessage(). You saved me a lot of
time.

Alfredo


""Jeffrey Tan[MSFT]"" <v-jetan@online.microsoft.com> wrote in message
news:NanS2XvxEHA.768@cpmsftngxa10.phx.gbl...
> Hi aualias,
>
> I am glad you finally intercepted these 2 notification.
>
> To save the column headers order, we may send LVM_GETCOLUMNORDERARRAY
> message to the listview. Then we can use LVM_SETCOLUMNORDERARRAY message
to
> restore to certain column orders. Full code snippet lists below:
>
> private const uint WM_NOTIFY=0x004E;
> private const uint HDN_BEGINDRAG = 0xFFFFFECA;
> private const uint HDN_ENDDRAG = 0xFFFFFEC9;
> private const uint LVM_FIRST=0x1000;
> private const uint LVM_SETCOLUMNORDERARRAY=LVM_FIRST + 58;
> private const uint LVM_GETCOLUMNORDERARRAY=LVM_FIRST + 59;
>
> [DllImport("user32.dll")]
> static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
> int[] lParam);
>
> [StructLayout(LayoutKind.Sequential)]
> public struct NMHDR
> {
> public uint hwndFrom;
> public uint idfrom;
> public uint code;
> }
>
> int[] column_array;
> public void savecolumnsetting()
> {
> column_array=new int[this.Columns.Count];
> SendMessage(this.Handle, LVM_GETCOLUMNORDERARRAY,
> (IntPtr)this.Columns.Count, column_array);
> for(int i=0;i<column_array.Length;i++)
> {
> Console.WriteLine(column_array[i].ToString());
> }
> }
>
> public void restorecolumnsetting()
> {
> SendMessage(this.Handle, LVM_SETCOLUMNORDERARRAY,
> (IntPtr)this.Columns.Count, column_array);
> this.Invalidate();
> }
>
> protected override void WndProc(ref Message m)
> {
> if(m.Msg==WM_NOTIFY)
> {
> NMHDR nm=(NMHDR)m.GetLParam(typeof(NMHDR));
> if(nm.code==HDN_BEGINDRAG)
> {
> savecolumnsetting();
> }
> else if(nm.code == HDN_ENDDRAG)
> {
> Console.WriteLine("draging is over");
> }
> }
> base.WndProc (ref m);
> }
> Note: after sending LVM_SETCOLUMNORDERARRAY to the listview, we should
> invoke ListView.Invalidate method to refresh the UI of listview.
>
> I have also attached the sample project in this reply. It works well on my
> side.
> =============================================================
> 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.



Re: ListView control header events? by v-jetan

v-jetan
Thu Nov 11 01:42:29 CST 2004

You are welcome!! If you need further help, please feel free to tell me,
thanks

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.