Less Thirty-first Lesson View Control
In this lesson, we will learn how to create and use the list view control.
theory:
List view controls and tree views, enrich text editing controls is one of the general controls. You may have known the list view control, just don't know its exact name. The list view control can be used to display items well. In this regard, it is the same as the list box, but its performance is stronger. There are two ways to create a list view control. The first is also the simplest method is to create it with a resource editor. Use this method just not to forget the call to the INITCOMMONTROLS function (remember to call this function, "calling this function is just to imply DLL containing universal controls. Another method is to call the CREATEWINDOWEX function, here you must specify the appropriate class name, such as: SyslistView32, wc_listview is not the correct class name to display data: large icon, small icon, list, and reporting method . These methods are related to the resource management species to select View-> Large Icons, Small Icons, List, and Details. A variety of different display methods are only shown in different appearances. For example, you may have a lot of data, just don't want to display all. The news provided by the report is the most complete, and other methods are much less. When you just create a list view, you can select an initial display method, then you can call the setwinodwlong function and set the GWL_Style flag to change the display method.
Since we already know how to create a list control, let's learn how to use them. We will focus primarily on the report of the report because this mode demonstrates the most list of list control features. The steps to use the list are as follows:
Call the CREATEWINDOWEX function to create a list control, specify its class named SyslistView32. You can also specify how the control is initially displayed here. Create and initialize the image list (if present) in the list control. Insert the column in the list control, if the way the display is the step in the report mode is necessary. Insert the project and from the project to the control.
Column:
In the report, there is more than one column. You can view the data in the list control as a form: At this time, the data is arranged in the ranks. There is at least one column in the control. In other display modes, it doesn't matter because these display methods have only one column. The joining column is implemented by sending a LVM_INSERTCOLUMN message to the list control.
LVM_INSERTCOLUMNWPARAM = ICOLLPARAM = Pointer to point to LV_COLUMN Structural Variables
The number of ICOL columns, starting from 0. The LV_COLUMN contains information that will be inserted. It is defined as follows:
LV_COLUMN STRUCT IMASK DD? FMT DD? LX DD? PSZTEXT DD? CCHTEXTMAX DD? ISUBITEM DD? IIMAGE DD? IORDER DD? LV_COLUMN ENDS
Field Namemeningsimask A set of flags indicating those members variables in the structure. The member variables in the structure are not active. At some point, only certain member variables may be effective. Structures can be used to input and output. This makes Windows know that those members variables are very important. Possible marks are: LVCF_FMT = FMT valid LVCF_SUBITEM = ISUBITEM valid LVCF_Text = psztext is valid. LVCF_WIDTH = LX Valid You can use several flags at a time. For example, if you want to provide a column name (column name) to the specified column, you must provide the column name in the PSzText member variable, then specify the flag LVCF_Text tells the value in the psztext in the PSZText, otherwise Windows will ignore the psztext. value. FMT specifies the alignment of the project / sub-project. Possible values are: lvcfmt_center = Text homance LVCFMT_LEFT = Text left align LVCFMT_Right = text Right alignment LXLX is the width of the column (in units of pixels). You can send a message LVM_SETCOLUMNWIDTH to change the width of the column. PSZText If used to set the properties of the column, the member variable is a pointer to the column name. If it is a query column name, the member variable points to a sufficiently big buffer to receive the returned column name, which is you must specify the size of the buffer in the member cchtextmax. If it is set to set the column name, the variable can be ignored because the pointer points to a string of an ASCII code, and Windows can resolve the length of the ASCII string. CchtextMaxCchtextMax is a small buffer pointing to a member variable in a byte meter. The member variable is only used when you query the properties of the column. If it is the property of the set column, the variable will be ignored. ISUBITEM specifies the index number of subtitals connected to the column. The value of the member variable is used to identify subplices connected to the column. The use of this column will best explain how to connect the column number and subjects. To query the properties of the column, you can send the LVM_GETCOLUMN message and specify the LVCF_SUBITEM flag in the member variable iMask, and the list control will return the ISUBITEM value set when inserted in the ISUBITEM. To use this method, you need to put the correct value in this member variable. IIMAGE AND IORDER is compatible with IE3.0 or above. I don't have information on this aspect. After the list view control is created, you must insert a column in place. Of course, if you don't plan to use the report mode, it is not necessary to insert the column. To insert columns, you need to define a lv_column type structural variable, assign the correct value to its member variable, specify the column number, and then send the LVM_INSERTCOLUMN message to the list view control and pass the value of the structure variable.
LOCAL LVC: LV_COLUMN MOV LVC.IMASK, LVCF_TEXT LVCF_WIDTH MOV LVC.PSZText, Offset Heading1 MOV LVC.LX, 150 Invoke SendMessage, HLIST, LVM_INSERTCOLUMN, 0, AddR LVC
The above code segment shows the process. When the LVM_INSERTCOLUMN message is sent, he specifies the column title bar text and its width.
Projects and subprojects
The project is the primary content in the list view. In addition to the report, you can only see the item in the list view. Sub-project is the details of the project. A project may have more than one related subproject. For example, such as the project is a file name, the relevant subproject may have file properties, size, and date. In the view of the report, the leftmost column is a project, and the other columns are subprojects. From the perspective of database record, the project is similar to primary keys, and subjects are similar to records. At least your list view requires some items: child projects are optional. If you want to provide more information to users, you can connect sub-items and items, and then put them in the list view to report. You can add an item to the list view to the list view, and you also need to put a pointer to the LV_Item type structure to the list view. The definition of lv_item is as follows: LV_Item Struct IMASK DD? IIITEM DD? ISUBITEM DD? State DD? STATEMASK DD? PSZTEXT DD? CCHTEXTMAX DD? IIMAGE DD? LPARAM DD? IINDENT DD? LV_Item Ends
Field Namemeningsimask A set of flags indicate valid in those members variables in the structure. Its significance is basically the same as the corresponding member variables in the LV_Column structure we mentioned above. More detailed information, you can query the Win32 API manual. IIITEM The index number of the project represented by the structure. The index number is numbered from 0. This value is similar to the "line" of the form. ISUBITEM and the index number of the subpredal of the items specified by the previous member variable. You can treat it as a "column" of your form. For example, if you want to insert a project into the newly created list view control, the value of IIIM should be 0 (because the item is the first item), the value of isubitem should also be 0 (we want to plug the project to the first Column). If you want to specify a sub-project and the item is connected, IIITEM should be the index number of the item you want to connect, the value of isubitem should be greater than 0, depending on what you want to plug the child. a row. If your list view control has a total of 4 columns, the first column contains items, and the remaining 3 columns are left to subtores. If you want to plug the sub-project in the fourth column, you should specify that the value is 3. State This member variable contains the flag of the project to react the status of the project. The change in state may be caused by the user's operation or the program change. These states include: whether an focus / high brightness display / selected (due to being cut) / selected. It is also also included that the index of 1 is used to represent whether or not the overlapping / status icon is used. STATEMASK Since the above member variable contains status flag, overlapping bitmap index, and status bitmap index number, we need to tell Windows We need to set or query that value. The member variable is used to do this. PSZText When we want to set the properties of the project, it contains the address of the ASCII code of the project name. When querying the properties of the item, the member variable will be used to receive the name of the item returned by the query. CCHTextMax only needs to use this value only when you use the properties of the item, and it contains the size of the last member variable. The IImage icon index number in the image linked list in the list view. LPARAM user-defined values when you are sorted by the project. When you tell the list view, the list view will be paired in the project when sorting items. It will pass the value of the LPARAM of the two projects to you so you can list the one first. If you don't quite understand now, there is no system, we will still talk about the problem of sorting.
Let us now summarize the steps to insert the project / subpower in the list control:
Define a lv_item type structural variable. Give the variable to the appropriate value If you want to insert an item, send the LVM_INSERTITEM value to the list view control. If you want to insert a subproject, send LVM_SETITEM. If you don't understand the relationship between projects and sub-projects, there may be some doubts. Subprint is only the properties of the project, that is, you can insert a project but cannot insert a subproject. So add a sub-project ten that can only send the LVM_SETITEM message and cannot send a LVM_INSERTITEM message. List view control message / notification
Since you know how to create and add content, the next step is how to communicate with it. The communication between the list view control and its parent window is performed by message / notification. The parent window controls the list view control by sending a message, and the list view control notifies its parent window by sending a WM_NOTIFY message. There is no difference between this and other general controls.
Sort Project / Sub Project
You can specify the LVS_SORTASCENDING or LVS_SORTDESCENDING style when calling the CreateWindowEx function to specify the default sorting method. These two styles only sort the name of the project. If you want to sort the other properties of the project, you can do it by sending a LVM_SORTITEMS message.
LVM_SORTITEMSWPARAM = lparamsortlparam = pcomparefunction
LPARAMSORT user-defined values that will pass the function that is used to compare. PCompArEfunction user-defined addresses used to compare sorted functions. The prototype of this function is as follows:
Comparefunc Proto Lparam1: DWORD, LPARAM2: DWORD, LPARAMSORT: DWORD
LPARAM1 and LPARAM2 are values of members variables in the LV_Item type. LPARAMSORT is the value in the parameter wparam when sending a LVM_SORTITEMS message
When the list view control receives the LVM_SORTITEMS message, it needs to be compared to the comparison function specified in LPARAM. The comparison function will determine the one project. Method is simple: If the function returns a negative value, the first item (LParam is represented) first project, if the positive value is returned, the second project is ranked before. If equally, you must return 0.
It is truly enabled the method to run a member variable lparam value in the LV_Item type structure. When you need to sort (such as when you click on the title of the column), you need to consider a good sorting solution. In this example, we put the index of the project in this member variable so that we can get other information from the project by sending a LVM_GETITEM message. Note: After the project is rejected, their index has changed. So after being sorted, I need to react new index in the LPARAM parameter. If you want to reorder when the user clicked the title strip, you need to process the LVN_COLUMNCLICK notification message in your window process function. The LVN_COLUMNCLICK message is sent along with the WM_NOTIFY message.
example:
This example creates a list view control and displays the file size and file name in the current folder. The default view is the report method. If you click on the column headline bar, the title will rearrange the / descending. You can choose different display modifications (large icons, small icons, etc.) through menus. When you double-click an item, the project's name will be displayed in a dialog.
.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 AppName db "Testing a ListView Control", 0 ListViewClassName DB "syslistView32", 0 Heading1 DB "filename", 0 Heading2 DB "size", 0 filenamepattern DB "*. *", 0 FileNameSortorder DD 0 SIZESORTORTER DD 0 Template DB "% Lu", 0.data? Hinstance Hinstance? HLIST DD? HMENU DD? Code Start: Invoke GetModuleHandle, Null Mov Hinstance, Eax Invoke Winmain, Hinstance, Null, Null, SW_SHOWDEFAULT INVOKE EXITP rocess, eax invoke InitCommonControls WinMain proc hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD LOCAL wc: WNDCLASSEX LOCAL msg: MSG LOCAL hwnd: HWND mov wc.cbSize, SIZEOF WNDCLASSEX mov wc.style, NULL mov wc. LpfnWndProc, Offset WndProc Mov Wc.cbclsextra, Null Mov Wc.cbwndextra, Null Push Hinstance Pop Wc.hinstance Mov Wc.hbrbackground, Color_Window
1 mov wc.lpszMenuName, IDM_MAINMENU mov wc.lpszClassName, OFFSET ClassName invoke LoadIcon, NULL, IDI_APPLICATION mov wc.hIcon, eax mov wc.hIconSm, eax invoke LoadCursor, NULL, IDC_ARROW mov wc.hCursor, eax invoke RegisterClassEx, addr wc invoke CreateWindowEx, NULL, ADDR ClassName, ADDR AppName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInst, NULL mov hwnd, eax invoke ShowWindow, hwnd, SW_SHOWNORMAL invoke UpdateWindow, hwnd .while TRUE invoke GetMessage, ADDR msg, NULL, 0,0 .break .if (! eax) invoke TranslateMessage, ADDR msg invoke DispatchMessage, ADDR msg .endw mov eax, msg.wParam ret WinMain endp InsertColumn proc LOCAL lvc: LV_COLUMN mov lvc.imask, LVCF_TEXT LVCF_WIDTH mov lvc .pszText, offset Heading1 mov lvc.lx, 150 invoke SendMessage, hList, LVM_INSERTCOLUMN, 0, addr lvc or lvc.imask, LVCF_FMT mov lvc.fmt, LVCFMT_RIGHT mov lvc.pszText, offset Heading2 mov lvc.lx, 100 invoke SendMessage, hList, LVM_INSERTCOLUMN, 1, addr lvc ret InsertColumn endp ShowFileInfo proc uses edi row: DWORD, lpFind: DWORD LOCAL lvi: LV_ITEM LOCAL buffer [20]: BYTE mov edi, lpFind assume edi: ptr WIN32_FIND_DATA mov lvi.imask, LVIF_TEXT
LVIF_PARAM push row pop lvi.iItem mov lvi.iSubItem, 0 lea eax, [edi] .cFileName mov lvi.pszText, eax push row pop lvi.lParam invoke SendMessage, hList, LVM_INSERTITEM, 0, addr lvi mov lvi.imask, LVIF_TEXT inc lvi.iSubItem invoke wsprintf, addr buffer, addr template, [edi] .nFileSizeLow lea eax, buffer mov lvi.pszText, eax invoke SendMessage, hList, LVM_SETITEM, 0, addr lvi assume edi: nothing ret ShowFileInfo endp FillFileInfo proc uses edi LOCAL finddata: WIN32_FIND_DATA LOCAL FHandle: DWORD invoke FindFirstFile, addr FileNamePattern, addr finddata .if eax = INVALID_HANDLE_VALUE mov FHandle, eax xor edi, edi .while eax = 0 test finddata.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY .if ZERO invoke ShowFileInfo, edi!!? Addr Finddata Inc Edi .endif Invoke FindnextFile, Fhandle, AddR Finddata .Endw Invoke Findclose, Fhandle .endif Ret Fillfileinfo Endp String2dword Proc Uses EDI EDX ESI STRING: DWORD LOCAL Resul T: DWORD MOV Result, 0 MOV EDI, STRING INVOKE LSTRLEN, STRING .WHILE EAX! = 0 xor Edx, EDX MOV DL, BYTE PTR [EDI] Sub DL, "0" Mov ESI, ESI PUSH EAX MOV EAX, edx push ebx mov ebx, 10 .while esi> 0 mul ebx dec esi .endw pop ebx add Result, eax pop eax inc edi dec eax .endw mov eax, Result ret String2Dword endp compareFunc proc uses edi lParam1: DWORD, lParam2: DWORD , SortType: DWORD LOCAL buffer [256]: BYTE LOCAL buffer1 [256]: BYTE LOCAL lvi: LV_ITEM mov lvi.imask, LVIF_TEXT lea eax, buffer mov lvi.pszText, eax mov lvi.cchTextMax, 256 .if SortType ==
1 mov lvi.iSubItem, 1 invoke SendMessage, hList, LVM_GETITEMTEXT, lParam1, addr lvi invoke String2Dword, addr buffer mov edi, eax invoke SendMessage, hList, LVM_GETITEMTEXT, lParam2, addr lvi invoke String2Dword, addr buffer sub edi, eax mov eax, edi .elseif SortType == 2 mov lvi.iSubItem, 1 invoke SendMessage, hList, LVM_GETITEMTEXT, lParam1, addr lvi invoke String2Dword, addr buffer mov edi, eax invoke SendMessage, hList, LVM_GETITEMTEXT, lParam2, addr lvi invoke String2Dword, addr buffer sub eax, edi .elseif SortType == 3 mov lvi.iSubItem, 0 invoke SendMessage, hList, LVM_GETITEMTEXT, lParam1, addr lvi invoke lstrcpy, addr buffer1, addr buffer invoke SendMessage, hList, LVM_GETITEMTEXT, lParam2, addr lvi invoke lstrcmpi, addr buffer1 , addr buffer .else mov lvi.iSubItem, 0 invoke SendMessage, hList, LVM_GETITEMTEXT, lParam1, addr lvi invoke lstrcpy, addr buffer1, addr buffer invoke SendMessage, hList, LVM_GETITEMTEXT, lParam2, addr lvi invoke lstrcmpi, addr buffer, addr buffer1 .endif ret CompareFunc endp UpdatelParam proc uses edi LOCAL lvi: LV_ITEM invoke SendMessage, hList, LVM_GETITEMCOUNT, 0,0 mov edi, eax mov lvi.imask, LVIF_PARAM mov lvi.iSubItem, 0 mov lvi. IIITEM, 0 .While Edi>
0 push lvi.iItem pop lvi.lParam invoke SendMessage, hList, LVM_SETITEM, 0, addr lvi inc lvi.iItem dec edi .endw ret UpdatelParam endp ShowCurrentFocus proc LOCAL lvi: LV_ITEM LOCAL buffer [256]: BYTE invoke SendMessage, hList, LVM_GETNEXTITEM , -1, LVNI_FOCUSED mov lvi.iItem, eax mov lvi.iSubItem, 0 mov lvi.imask, LVIF_TEXT lea eax, buffer mov lvi.pszText, eax mov lvi.cchTextMax, 256 invoke SendMessage, hList, LVM_GETITEM, 0, addr lvi invoke MessageBox, 0, addr buffer, addr AppName, MB_OK ret ShowCurrentFocus endp WndProc proc hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM .if uMsg == WM_CREATE invoke CreateWindowEx, NULL, addr ListViewClassName, NULL, LVS_REPORT WS_CHILD WS_VISIBLE, 0,0,0,0, hWnd, NULL, hInstance, NULL mov hList, eax invoke InsertColumn invoke FillFileInfo RGB 255,255,255 invoke SendMessage, hList, LVM_SETTEXTCOLOR, 0, eax RGB 0,0,0 invoke SendMessage, hList, LVM_SETBKCOLOR, 0, EAX RGB 0, 0, 0 invoke SendMessage, hList, LVM_SETTEXTBKCOLOR, 0, eax invoke GetMenu, hWnd mov hMenu, eax invoke CheckMenuRadioItem, hMenu, IDM_ICON, IDM_LIST, IDM_REPORT, MF_CHECKED .elseif uMsg == WM_COMMAND .if lParam == 0 invoke GetWindowLong, hList, GWL_STYLE and eax , not LVS_TYPEMASK mov edx, wParam and edx, 0FFFFh push edx or eax, edx invoke SetWindowLong, hList, GWL_STYLE, eax pop edx invoke CheckMenuRadioItem, hMenu, IDM_ICON, IDM_LIST, edx, MF_CHECKED .endif .elseif uMsg == WM_NOTIFY push edi mov EDI, LPARAM Assume EDI: PTR NMHDR MOV EAX, [EDI] .hwndfrom .if Eax == Hlist .IF [EDI] .code ==
LVN_COLUMNCLICK assume edi: ptr NM_LISTVIEW .if [edi] .iSubItem == 1 .if SizeSortOrder == 0 || SizeSortOrder == 2 invoke SendMessage, hList, LVM_SORTITEMS, 1, addr CompareFunc invoke UpdatelParam mov SizeSortOrder, 1 .else invoke SendMessage, hList, LVM_SORTITEMS, 2, addr compareFunc invoke UpdatelParam mov SizeSortOrder, 2 .endif .else .if FileNameSortOrder == 0 || FileNameSortOrder == 4 invoke SendMessage, hList, LVM_SORTITEMS, 3, addr compareFunc invoke UpdatelParam mov FileNameSortOrder, 3 .else invoke SendMessage, hList, LVM_SORTITEMS, 4, addr compareFunc invoke UpdatelParam mov FileNameSortOrder, 4 .endif .endif assume edi: ptr NMHDR .elseif [edi] .code == NM_DBLCLK invoke ShowCurrentFocus .endif .endif pop edi .elseif uMsg == WM_SIZE mov EAX, LP aram mov edx, eax and eax, 0ffffh shr edx, 16 invoke MoveWindow, hList, 0, 0, eax, edx, TRUE .elseif uMsg == WM_DESTROY invoke PostQuitMessage, NULL .else invoke DefWindowProc, hWnd, uMsg, wParam, lParam ret .endif xor Eax, EAX RET WNDPROC END Start Analysis:
The first thing to do after the main window is created is to create a list view control application.
..................... ..
We call CREATEWINDOWEX to create a window and pass the name "syslistView32" of the window class. The default display method is the report mode because you specify the LVS_REPORT flag as its style.
Invoke insertColumn
After creating a list view control, we insert columns.
LOCAL lvc: LV_COLUMN mov lvc.imask, LVCF_TEXT LVCF_WIDTH mov lvc.pszText, offset Heading1 mov lvc.lx, 150 invoke SendMessage, hList, LVM_INSERTCOLUMN, 0, addr lvc we specify a title bar and the first column and the column, in order The name of the file is displayed in this column, and we need to set the flag bit LVCF_TEXT or LVCF_WIDTH in the member variable IMASK of the LV_COLUMN structure variable. We set PSZText as the value of the column header bar text string, and the LX is set to the width of the column (in units of pixel points). Then we send the LVM_INSERTCOLUMN message to the list view control and pass the structure variable to it.
OR lvc.imask, LVCF_FMT MOV LVC.FMT, LVCFMT_RIGHT
After inserting the first column, we insert a second column, click the title strip of the column to sort by the size of the file. Because we need to align the text, we need to specify the flag bit LVCFMT_Right in the member variable FMT. We must also specify the flag bit LVCF_FMT in addition to the flag bit LVCF_Text and LVCF_WIDTH in the member variable iMask.
MOV LVC.PSZText, Offset Heading2 MOV LVC.LX, 100 Invoke SendMessage, Hlist, LVM_INSERTCOLUMN, 1, AddR LVC
The remaining code is relatively simple. Add the address of the text string in the psztext, put the width of the column in the LX. Then send a message LVM_INSERTCOLUMN to the list view control, transmit the address of the column number and the structure variable in the parameter.
When the column is inserted, we join the item to the list control.
Invoke FillFileinfo
The code of FillFileInfo is as follows:
Fillfileinfo Proc Uses Edi Local Finddata: Win32_Find_Data Local Fhandle: DWORD INVOKE FINDFIRSTFILE, AddR FileNamePattern, AddR Finddata
We call FindFirstFile to get information about the first file that meets the search criteria. The prototype of the FindFirstFile function is as follows
Findfirstfile Proto PfileName: DWORD, PWIN32_FIND_DATA: DWORD
PfileName is the address used to match the file name of the search. This string contains wildcards. In our example is *. *, This will search all the files in the current folder. PWIN32_FIND_DATA is the address of the Win32_Find_Data type structure variable, and the Win32_find_data type structure variable will be used to save the returned file.
If the matching file is not found, the function will return to invalid_handle_value in Eax. Otherwise, a search handle will be returned, you can use this handle to search for the next eligible file in the FindNextFile function.
.IF EAX! = Invalid_Handle_Value Mov Fhandle, EAX XOR EDI, EDI
If a file is found, we save the search handle in a variable and clear the register EDI, which will use the index number of the project.
.while eax! = 0 Test Finddata.dwfileAttributes, file_attribute_directory .if Zero?
In this lesson, we will not process the folder, so we check if the value of the DWFileAttributes member variable has a File_Attribute_directory flag. If we have, we ignore it, then call FindNextFile. Invoke ShowfileInfo, EDI, AddR Finddata Inc Edi .endif Invoke FindnextFile, Fhandle, AddR Finddata .endw
We call the name and size information of the showfileinfo function package file to add to the list view control. The EDI register is then added to add the line number of the project. Finally, we call the FindNextFile function Continue to search for files in the current folder until the function returns 0 (this means no file available).
Invoke FindClose, Fhandle .endif Ret FillfileInfo Endp
After the file enumeration in the current folder, we must close the search handle.
Let's take a look at the showfileinfo function. This function is from two parameters, one is the index number of the project (also line number), and the other is the address of the win32_find_data structure variable.
Showfileinfo Proc Uses Edi Row: DWORD, LPFIND: DWORD LOCAL LVI: LV_Item Local Buffer [20]: BYTE MOV EDI, LPFIND Assume EDI: PTR WIN32_FIND_DATA
Place the value of the Win32_Find_DATA structure variable into the register EDI.
MOV LVI.IMASK, LVIF_TEXT LVIF_PARAM PUSH ROW POP LVI.IITEM MOV LVI.ISUBITEM, 0
We will pass the name of the project and the value of LPARAM, so we put the marker LVIF_TEXT and LVIF_PARAM in iMask. Next we put into the transfer line number in IITEM, and because this is the main project we must set the value of ISUBITEM equal to 0.
Lea Eax, [EDI] .cfilename Mov Lvi.psztext, Eax Push Row Pop Lvi.lparam
We now have to put the address of the label string, which is also in the name of the file in the Win32_Find_Data structure variable, in Psztext. Since we have to complete the sort of the project, you must set the value of lParam, which set it into a line number value so that we can query the item according to the index value.
Invoke SendMessage, HLIST, LVM_INSERTITEM, 0, AddR LVI
After setting the value in all LV_Item type variables, we send the LVM_INSERTITEM message to the list view control to insert the project into it.
MOV LVI.IMASK, LVIF_TEXT INC LVI.ISUBITEM INVOKE WSPRINTF, AddR Buffer, Addr Template, [EDI] .NFILESZELOW LEA EAX, Buff MoV Lvi.psztext, EAX
We will insert subjects into the second column. A sub-project can only have a label. This way we specify a LVIF_TEXT flag in iMask. Then we specify the column where the child project is located. In this example, we give this value equal to 1 by adding the ISUBITEM. The tag value is the size of the file, in order to convert the WSPRINTF function to text, then put the address of the text in PSZText.
Invoke SendMessage, HLIST, LVM_SETITEM, 0, ADDR LVI Assume Edi: Nothing Ret ShowFileInfo ENDP When the value in the LV_Item type variable is set, we send a LVM_SETITEM message to the list view control and pass the address of the LV_Item variable over. Note: The message sent is lv_item instead of LVM_INSERTITEM, because we insert it into a sub-project, the child project is not a real project but the properties of the primary project. So we are in setting the properties of the project, rather than joining a project.
When all items are inserted into the list view control, we set its text and background colors.
RGB 255,255,255 invoke SendMessage, hList, LVM_SETTEXTCOLOR, 0, eax RGB 0,0,0 invoke SendMessage, hList, LVM_SETBKCOLOR, 0, eax RGB 0,0,0 invoke SendMessage, hList, LVM_SETTEXTBKCOLOR, 0, eax
We use RGB (R --- Red G --- Green B - BLUE) to convert the three-color conversion and put it in Eax. We set the foreground and background colors of the text by sending LVM_SETTEXTCOLOR and LVM_SETTEXTBKCOLOR messages.
Invoke GetMenu, HWnd Mov Hmenu, Eax Invoke CHECKMENURADIOIOITEM, HMENU, IDM_ICON, IDM_LIST, IDM_REPORT, MF_CHECKED
We will let users choose what they want through the menu. This way we must first get the handle of the menu. I let the user track the current view, let's put a set of radio buttons in the menu. We can call the checkmenuradioItem function, which will put a radio button before a menu item.
Note that we create a list of widths and heights when we create a list view control. When the parent window changes the size, it will change the size at the same time. This allows the list view to always change as the main window. In our example, let's let the list view fill the entire client area.
.ELSEIF UMSG == WM_SIZE MOV EAX, LPARAM MOV EDX, EAX AND Eax, 0fffh SHR EDX, 16 Invoke MoveWindow, Hlist, 0, 0, Eax, EDX, TRUE
When the parent window receives the WM_SIZE message, the bottom part of the LParam contains new width and height of the client area. Let's call MoveWindow to change the size of the list view control makes it overwrites the entire client area.
When the user selects a choice method through the menu, we must change the display mode in the list view accordingly. We call the setWindowlong function to set new style.
.ELSEIF uMSG == WM_COMMAND .IF LPARAM == 0 Invoke getWindowlong, Hlist, GWL_Style and Eax, Not LVS_TYPEMASK
First get the current style, then remove the old style. LVS_TYPEMASK is a collection of four styles of LVS_ICON LVS_SMALLICON LVS_LIST LVS_REPORT. This way we use the current style "" not lvs_typemask "is equal to clear the current display style.
In the design menu, we used some tips. We package the constant string of the style as the ID number of the menu.
IDM_ICON EQU LVS_ICON IDM_SMALLICON EQU LVS_SMALLICON IDM_LIST EQU LVS_LIST IDM_REPORT EQU LVS_REPORT This When the parent window receives the WM_COMMAND message, the style value that you want to display will be passed as the ID number as a menu.
Mov Edx, WParam and EDX, 0FFFH
The low-character part in wPARAM is a style that wants to display, and we need to do it just to clear the high character part.
Push Edx or Eax, EDX
We add what you want to display to the list of the list view (already removed from the old style).
Invoke SetWindowlong, Hlist, GWL_Style, EAX
Call the setWindowlong function to set a new style.
Pop Edx Invoke CHECKMENURADIOIOITEM, HMENU, IDM_ICON, IDM_LIST, EDX, MF_CHECKED .enDiF
We need to put the radio button before the selected display method. If we want to sort, we must handle WM_NOTIFY messages.
.ELSEIF UMSG == WM_Notify Push EDI MOV EDI, LPARAM Assume EDI: PTR NMHDR MOV EAX, [EDI] .hwndfrom .if Eax == HLIST
When we receive the WM_NOTIFY message, the LPARAM contains a pointer to the NMHDR structure variable. By comparing the value of the list view control and the value of the HWNDFROM member variable in the NMHDR structure variable, we can determine that the message is sent by the list view control if we are equal.
.IF [EDI] .code == lvn_columnclick associ edi: PTR NM_LISTVIEW
If the notification message is sent by the list view control, we detect if the message is LVN_COLUMNCLICK. If so, it means that the user clicks the column headings. After receiving the LVN_COLUMNCLICK message, we assume that the LPARAM parameter contains a pointer to the NM_ListView structure variable, the NM_ListView type structure is an expansion of the NMHDR structure. We need to know that the user clicks on that column, the value in the isubitem is the column number, the number of the column starts from 0.
.IF [EDI] .isubitem == 1 .IF SIZESORTORDER == 0 || Sizesortorder == 2
The value of Isubitem here is 1, which means that the user clicks the second column, that is, the size of the file. We use state variables to maintain the current sort order. 0 represents no sorting, 1 represents ascending, 2 represents desperation. If the items / subproject in the column are not sorted or descended before, we set it ascended.
Invoke SendMessage, Hlist, LVM_SORTITEMS, 1, AddR Comparefunc
We send messages to LVM_SortItems to the list view control, pass 1 in WPARAM, pass the parameters of the comparison function in the LPARAM. Note that the value in WPARAM is user-defined, users can explain them according to their needs, here we use it as sorting methods. Let's take a look at the comparison function:
CompareFunc proc uses edi lParam1: DWORD, lParam2: DWORD, SortType: DWORD LOCAL buffer [256]: BYTE LOCAL buffer1 [256]: BYTE LOCAL lvi: LV_ITEM mov lvi.imask, LVIF_TEXT lea eax, buffer mov lvi.pszText, eax mov Lvi.cchtextmax, 256 list view control will pass the comparison function of LPARAM (LV_ITEM type structural variables) that needs to be compared. Do you remember? We placed your eye-catching index numbers in LPARAM. This way we use these index numbers to query the list view to get item information. The news we need is the label text for the project / subpower. To this end, we are ready to set the LV_item structure variable and set the flag bit LVIF_TEXT in iMask, set the address of the buffer in the PSZText, and set the size of the buffer in cchtextmax.
.IF SortType == 1 MOV LVI.ISubitem, 1 Invoke SendMessage, Hlist, LVM_GetItemText, LParam1, Addr Lvi
If the value of SortType is 1 or 2, we know that you click on that column, 1 represents all items in ascending according to the size of the file. 2 The meaning is the opposite. This way we specify ISUBITEM 1 (representing the file size column) and send a LVM_GETITEMTEXT message to the list view control to get the tag text string on the project.
Invoke String2dword, AddR Buffer Mov Edi, EAX
Call the sub-defined String2Dword function to convert the string into a DWORD value. It will return the converted value in EAX, and we save it in the EDI for later comparison.
Invoke SendMessage, Hlist, LVM_GetItemte, LParam2, Addr Lvi Invoke String2dword, Addr Buff EDI, EAX MOV EAX, EDI
Do the same operation on the value in LPARAM2. When we get two files, we can compare them. The rules of the comparison are as follows:
If the first item is placed in front, returning a negative value in EAX If the second item is placed in front, returning a positive value in EAX If equally, return 0 in Eax
In us, we want to arrange according to ascending order, so we only reduce the file size of the second item to the file size and return to the value placed in Eax.
.elseif SortType == 3 mov lvi.iSubItem, 0 invoke SendMessage, hList, LVM_GETITEMTEXT, lParam1, addr lvi invoke lstrcpy, addr buffer1, addr buffer invoke SendMessage, hList, LVM_GETITEMTEXT, lParam2, addr lvi invoke lstrcmpi, addr buffer1, addr buffer
When the user hits the file name, we must compare the name of the file. Let's get the name of the file, then call the lstrcmpi function to compare, then simply return the value of lstrcmpi because the rule is used in the same way as us.
When the project is sorted, we call the UpdateParam function to update the value of all items of LPARAM to reflect the latest changes.
Invoke UpdatelParam Mov Sizesortorder, 1
This function simply enumerates all items in the list view and updates their lParam to the project's index number. .ELSEIF [EDI] .code == nm_dblclk invoke showcurrentfocus .endifu
If the user doubles a project, we will display a message box with the relevant tag value of the item. We must check if the Code value in nmHDR is NM_DBLCLK. If so, we get its tag value and display it in a message box.
Showcurrentfocus Proc Local LVI: LV_Item Local Buffer [256]: Byte Invoke SendMessage, HLIST, LVM_GETNEXTITEM, -1, LVNI_FOCUSED
How do we know how to know a project? When you click or double-click an item, its status is set to "focus". Even if there are multiple items being selected, there is only one project with focus. Our job is to find the focus project. We send the LVM_GetNextItem message to the list view control, specify the desired state in the LPARAM. If you are in WPARAM, you want to search for all items. The item determined in the focus is returned in EAX.
MOV LVI.IITEM, EAX MOV LVI.ISUBITEM, 0 MOV LVI.IMASUBITEM, 0 MOV LVI.IMASUBITEM, 0 MOV LVI.IMASK, LVIF_TEXT Lea Eax, Buffer Mov Lvi.psztext, Eax Mov Lvi.cchtextmax, 256 Invoke SendMessage, Hlist, Lvm_GetItem, 0, AddR LVI
Send a LVM_GETITEM message to get the tag.
Invoke Messagebox, 0, AddR Buffer, Addr Appname, MB_OK
Finally we display the label in a message box.
If you want to display icons in the list view control, you can read the course about the tree view control. Their steps are basically the same.