The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: dacid on February 01, 2010, 10:09:08 AM

Title: code samples on sorting a listview?
Post by: dacid on February 01, 2010, 10:09:08 AM
Seems that there is not too much out there...
Title: Re: code samples on sorting a listview?
Post by: dacid on February 01, 2010, 02:31:25 PM
Im playing with Iczelion tutorial 31... it seems to work pretty well but as this code is a little old ... would you recommend me to use it or there is better routines out here?
Title: Re: code samples on sorting a listview?
Post by: dacid on February 01, 2010, 06:26:30 PM
any info or code about auto size column headers?
Title: Re: code samples on sorting a listview?
Post by: PBrennick on February 01, 2010, 07:23:17 PM
When you create the listview (or later) youe one of the following flags, LVS_SORTASCENDING OR LVS_SORTDECENDING.

Paul
Title: Re: code samples on sorting a listview?
Post by: dacid on February 01, 2010, 07:37:53 PM
This sorts the listview the first time you add items, doesnt it?... Im using the iczelin code adapted to my program to sort the items when you click on a header. I realized that the "arrow" that some apps shows in the header is not something that windows does automatically.. any info about this?

Title: Re: code samples on sorting a listview?
Post by: Gunner on February 01, 2010, 10:04:39 PM
Quote from: dacid on February 01, 2010, 07:37:53 PM
This sorts the listview the first time you add items, doesnt it?... Im using the iczelin code adapted to my program to sort the items when you click on a header. I realized that the "arrow" that some apps shows in the header is not something that windows does automatically.. any info about this?

You can add an icon to the column when you create it and you can modify the icon by sending the listview the message LVM_SETCOLUMN and set the LVCF_IMAGE to the new image in LVCOLUMN

You should really download the Platform SDK if you can it is a big help!
Title: Re: code samples on sorting a listview?
Post by: farrier on February 01, 2010, 10:21:22 PM
dacid,

Visit Donkey's Stable:

http://www.quickersoft.com/donkey/

Filled with knowledge and good code.  Everything I needed for ListViews I got from his example.  Click on "GoAsm Projects" and scroll down to ListView.  The code is in GoAsm style, but it will be easy to change to your assembler of choice.  I think it will do everything you asked for, except AutoSize the columns, and I would love to know how to do that.

Thanks again, Donkey!!

hth,

farrier
Title: Re: code samples on sorting a listview?
Post by: Gunner on February 01, 2010, 10:24:32 PM
Quote from: farrier on February 01, 2010, 10:21:22 PM
I think it will do everything you asked for, except AutoSize the columns, and I would love to know how to do that.

Something like this?

    ; set column 1 width
    invoke  SendMessage, hLVMain, LVM_SETCOLUMNWIDTH, 0, LVSCW_AUTOSIZE
; column 2
    invoke  SendMessage, hLVMain, LVM_SETCOLUMNWIDTH, 1, LVSCW_AUTOSIZE_USEHEADER

Title: Re: code samples on sorting a listview?
Post by: dacid on February 01, 2010, 10:53:19 PM
yes, it works pretty well here. thanks gunner.

thanks farrier for the link.
Title: Re: code samples on sorting a listview?
Post by: farrier on February 02, 2010, 11:17:21 AM
Gunner,

Yes!  The strange thing is that I use LVM_SETCOLUMNWIDTH many places in a few programs, but I never paid attention to the "Auto" features.  Thanks!

farrier
Title: Re: code samples on sorting a listview?
Post by: dacid on February 03, 2010, 09:29:08 AM
Hey, another question  :red .. Im using CreatePopupMenu and AppendMenu to create a right-click menu over the listview... Im triying the same way I do in the Window Menu:

MENUITEM "&Refresh\tF5",IDM_REFRESH

szRefresh "&Refresh\tF5",0

But it shows the "\tF5" instead of right-align the "F5" string.
Title: Re: code samples on sorting a listview?
Post by: jj2007 on February 03, 2010, 10:07:34 AM
Try szRefresh "&Refresh", 9, "F5", 0
Title: Re: code samples on sorting a listview?
Post by: dacid on February 03, 2010, 12:33:10 PM
thank you! 
Title: Re: code samples on sorting a listview?
Post by: dacid on February 03, 2010, 05:37:22 PM
What msg sends the listview to announce when the user select a item or multiple items?

EDIT: I fount it:  LVN_ITEMCHANGED

Title: Re: code samples on sorting a listview?
Post by: dacid on February 03, 2010, 08:51:08 PM
Can I make a Window menu (defined in .RC file) with some items disabled?


MENUITEM "&Refresh\tF5",IDM_REFRESH


Or I must disable in WM_CREATE ?
Title: Re: code samples on sorting a listview?
Post by: dacid on February 04, 2010, 05:21:41 PM
Now im triyin to copy a complete line of the listview to the clipboard..   I use GlobalAlloc & GlobalLock but when I call LV_GETITEM in a .WHILE to get the text of every SubItem to addr returned by GlobalLock obviously I overwrite every previous item and the LV_GETITEM call doesnt return the size of the text. Ideas?
Title: Re: code samples on sorting a listview?
Post by: Gunner on February 04, 2010, 11:24:29 PM
Wait, you confused me.... that is you on the other board right, just a diff nick?  Post your code so we can see where your problem is
Title: Re: code samples on sorting a listview?
Post by: donkey on February 05, 2010, 05:04:16 AM
Use LVM_GETITEMTEXT, it will return the number of characters written to the buffer which you can use to update the pointer in order to append the next string returned. At least I think that's what you want to do, I am a bit confused by the question.
Title: Re: code samples on sorting a listview?
Post by: dacid on February 05, 2010, 09:41:36 AM
Gunner: Yes.

Donkey: Thats exactly what im trying to do...

              invoke OpenClipboard,hWnd
              invoke EmptyClipboard

              invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,512
              mov    hMemory,eax
              invoke GlobalLock,hMemory

              xor    ecx,ecx             
              .WHILE  ecx!=4
               push   eax
               push   ecx
               
               mov    lvitem.iSubItem,ecx
               mov    lvitem.imask,LVIF_TEXT
               mov    lvitem.pszText,eax
               mov    lvitem.cchTextMax,511
               invoke SendMessage,hListView,LVM_GETITEMTEXT,Index,ADDR lvitem
               mov    edx,eax
                 
              pop    ecx
              pop    eax
              add    eax,edx
              inc    ecx
             .ENDW

              invoke GlobalUnlock,hMemory
              invoke SetClipboardData,CF_TEXT,hMemory
              invoke CloseClipboard


What do you think about this code?  Its ok? How can I add a blank space between subitems? I
Title: Re: code samples on sorting a listview?
Post by: dacid on February 05, 2010, 02:22:13 PM
and another question  :red .. how i can get all selected items to copy to clipboard? a loop with LVM_GETNEXTITEM & LVNI_FOCUSED ?
Title: Re: code samples on sorting a listview?
Post by: donkey on February 05, 2010, 03:30:43 PM
Hi dacid,

With your code, first I would use other registers than ECX and EAX, EBX and EDI are much better for this application as they are not overwritten by the SendMessage call, eliminating the need to preserve them with push/pop. To add a space just mov a 0x20 byte into the string after the call to SendMessage...

invoke GlobalLock,hMemory
mov edi,eax
xor ebx,ebx
mov esi, 511

.WHILE  ebx!=4
    mov    lvitem.iSubItem,ebx
    mov    lvitem.imask,LVIF_TEXT
    mov    lvitem.pszText,edi
    mov    lvitem.cchTextMax,esi
    invoke SendMessage,hListView,LVM_GETITEMTEXT,Index,ADDR lvitem

    add    edi,eax
    sub    esi,eax
    mov    BYTE PTR [edi],20h ; insert a space
    inc    edi ; update the pointer
    dec    esi
    inc    ebx
.ENDW


Also, by keeping cchTextMax fixed you are allowing the possibility of a bufffer overflow exploit, the size of the buffer passed should reflect the size remaining, I used ESI for that purpose.

Edgar
Title: Re: code samples on sorting a listview?
Post by: farrier on February 05, 2010, 03:40:02 PM
dacid,

This won't answer your question directly, but ...

I mainly use listview to display a range of choices from a database: customer; item; transaction; etc.  When an item is added to the listview, I include the record number as one of the items in the listview.  This way, when the user selects an item from the listview, I can use the record number to retrieve all the items from the original database.  The advantage to this, is that all the items from the database don't have to be included in the listview, and you only need to include the few items in the listview that make it easy for the user to select on particular record.

hth,

farrier
Title: Re: code samples on sorting a listview?
Post by: dacid on February 05, 2010, 07:48:35 PM
Thak you very much donkey!!

Farrier I will take your idea in consideration!


Now the next step, copy to clipboard all selected items...


              invoke OpenClipboard,hWnd
              invoke EmptyClipboard

              invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,4096
              mov    hMemory,eax
              invoke GlobalLock,hMemory

              mov    edi,eax

              invoke SendMessage,hListView,LVM_GETSELECTEDCOUNT,0,0
              mov    nselected,eax

              .WHILE nselected!=0
               invoke SendMessage,hListView,LVM_GETNEXTITEM,-1,LVNI_SELECTED          
               .IF    eax!=-1
                mov    Index,eax

                xor    ebx,ebx
                mov    esi,4095

                .WHILE  ebx!=4
                 mov    lvitem.iSubItem,ebx
                 mov    lvitem.imask,LVIF_TEXT
                 mov    lvitem.pszText,edi
                 mov    lvitem.cchTextMax,esi
                 invoke SendMessage,hListView,LVM_GETITEMTEXT,Index,ADDR lvitem

                 add    edi,eax
                 sub    esi,eax
                 mov    BYTE PTR [edi],20h                                                 ; insert a space
                 inc    edi                                                                ; update the pointer
                 dec    esi
                 inc    ebx
                .ENDW
              .ENDIF
               dec  nselected
             .ENDW

              invoke GlobalUnlock,hMemory
              invoke SetClipboardData,CF_TEXT,hMemory
              invoke CloseClipboard


This copy the same item over and over again... Seems that I dont understand how LVM_GETNEXTITEM works...
Title: Re: code samples on sorting a listview?
Post by: dacid on February 05, 2010, 08:03:22 PM
I just fixed it this way:

             
              mov    Index,-1

              .WHILE nselected!=0
               invoke SendMessage,hListView,LVM_GETNEXTITEM,Index,LVNI_SELECTED          



This seems to work ... but Im not sure this code is "ok"
Title: Re: code samples on sorting a listview?
Post by: donkey on February 05, 2010, 11:04:19 PM
Looks good to me. Be sure to preserve the values of EBX, EDI and ESI, this is usually done in the PROC statement:

MyProcedure PROC uses EBX EDI ESI SomeParam:DWORD
...
MyProcedure ENDP

Edgar
Title: Re: code samples on sorting a listview?
Post by: dacid on February 06, 2010, 06:37:30 PM
Yep, thanks.  Im triying to "draw" the arrows in the columns when the user clicks on it... Im looking at your example "CustomListview" but I use MASM and this code:

            mov    hditem.mask,HDI_FORMAT+HDI_IMAGE
            mov    hditem.fmt,HDF_IMAGE+HDF_STRING+HDF_BITMAP_ON_RIGHT


gives me this error:

error A2081: missing operand after unary operator


Do youk know why?
Title: Re: code samples on sorting a listview?
Post by: donkey on February 06, 2010, 11:13:15 PM
QuoteDo youk (sic) know why?

Yes, as a matter of fact I do, hditem.mask is not allowed in masm, it uses the keyword MASK. I think Hutch changes it to imask, you'll have to check windows.inc.
Title: Re: code samples on sorting a listview?
Post by: dacid on February 07, 2010, 10:27:06 AM
Oops, yes, it is "imask"