iczelion tut31[3]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 jimgreen 的 blog

let's summarize the steps in inserting an item/subitem into a listview control.

  1. create a variable of type lv_item structure
  2. fill it with necessary information
  3. send lvm_insertitem message to the listview control if you want to insert an item. or if you want to *insert* a subitem, send lvm_setitem instead. this is rather confusing if you don't understand the relationship between an item and its subitems. subitems are considered as properties of an item. thus you can insert items but not subitems and you can't have a subitem without an associated item. that's why you need to send lvm_setitem message to add a subitem instead of lvm_insertitem.

listview messages/notifications

now that you know how to create and populate a listview control, the next step is to communicate with it. a listview control communicates with the parent window via messages and notifications. the parent window can control the listview control by sending messages to it. the listview control notifies the parent of important/interesting events via wm_notify message, just like other common controls.

sorting items/subitems

you can specify the default sorting order of a listview control by specifying lvs_sortascending or lvs_sortdescending styles in createwindowex. these two styles order the items using their labels only. if you want to sort the items in other ways, you need to send lvm_sortitems message to the listview control.

lvm_sortitems
wparam = lparamsort
lparam = pcomparefunction

lparamsort is a user-defined value that will be passed to the compare function. you can use this value in any way you want.
pcomparefunction is the address of the user-defined function that will decide the outcome of the comparison of items in the listview control. the function has the following prototype:

comparefunc proto lparam1:dword, lparam2:dword, lparamsort:dword

lparam1 and lparam2 are the values in lparam member of lv_item that you specify when you insert the items into the listview control.
lparamsort is the value in wparam you sent with lvm_sortitems

when the listview control receives lvm_sortitems message, it calls the compare function specified in lparam of the message when it needs to ask us for the result of comparison between two items. in short, the comparison function will decide which of the two items sent to it will precede the other. the rule is simple: if the function returns a negative value, the first item (represented by lparam1) should precede the other. if the function returns a positive value, the second item (represented by lparam2) should precede the first one. if both items are equal, it must return zero.

what makes this method work is the value in lparam of lv_item structure. if you need to sort the items (such as when the user clicks on a column header), you need to think of a sorting scheme that makes use of the values in lparam member. in the example, i put the index of the item in this field so i can obtain other information about the item by sending lvm_getitem message. note that when the items are rearranged, their indexes also change. so when the sorting is done in my example, i need to update the values in lparam to reflect the new indexes. if you want to sort the items when the user clicks on a column header, you need to process lvn_columnclick notification message in your window procedure. lvn_columnclick is passed to your window proc via wm_notify message.

example:

this example creates a listview control and fills it with the names and sizes of the files in the current folder. the default view is the report one. in the report view, you can click on the column heads and the items will be sorted in ascending/descending order. you can select the view you want via the menu. when you double-click on an item, a message box showing the label of the item is displayed.

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\comctl32.inc
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

winmain proto :dword,:dword,:dword,:dword

idm_mainmenu equ 10000
idm_icon equ lvs_icon
idm_smallicon equ lvs_smallicon
idm_list equ lvs_list
idm_report equ lvs_report

rgb macro red,green,blue
  xor eax,eax
  mov ah,blue
  shl eax,8
  mov ah,green
  mov al,red
endm

.data
classname db "listviewwinclass",0

本文关键:iczelion asm
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top