📄 tut31.html
字号:
<html>
<head>
<title>Iczelion's Win32 Assembly Tutorial 31: ListView Control</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#003366" text="#FFFFFF" link="#FFFFCC" vlink="#FFCCCC" alink="#CCFFCC">
<h1 align="center"><font face="Arial, Helvetica, sans-serif" color="#FFFFCC">Tutorial
31: Listview Control</font></h1>
<p><font face="MS Sans Serif" size="-1">We will learn how to create and use the
listview control in this tutorial.</font></p>
<p><font face="MS Sans Serif" size="-1">Download <b><a href="files/tut31.zip" style="text-decoration:none">the
example</a></b>.</font></p>
<h3><font face="Arial, Helvetica, sans-serif">Theory:</font></h3>
<p><font face="MS Sans Serif" size="-1">A listview control is one of the common
controls like treeview, richedit etc. You are familiar with it even if you may
not know it by its name. For example, the right pane of Windows Explorer is
a listview control. A listview control is good for displaying items. In this
regard, it's like a listbox but with enhanced capabilities.<br>
You can create a listview control in two ways. The first method is also the
easiest one: create it with a resource editor. Just don't forget to call <font color="#FFFFCC"><b>InitCommonControls</b></font>
within your asm source code. The other method is to call <font color="#FFFFCC"><b>CreateWindowEx</b></font>
in your source code. You must specify the correct window class name for the
control, ie. <font color="#CCFFCC"><b>SysListView32</b></font>. The window class
"WC_LISTVIEW" is incorrect. <br>
There are four methods of viewing data in a listview control: icon, small icon,
list and report views. You can see examples of these views by selecting View->Large
Icons (icon view), Small Icons (small icon view), List (list view) and Details
(report view). Views are just data representation methods:they only affect the
appearances of data. For example, you can have a lot of data in the listview
control, but if you want, you can view only some of them. Report view is the
most informative one while the remaining views give less info. You can specify
the view you want when you create a listview control. You can later change the
view by calling <font color="#FFFFCC"><b>SetWindowLong</b></font>, specifying
<font color="#CCFFCC"><b>GWL_STYLE</b></font> flag.</font></p>
<p><font face="MS Sans Serif" size="-1"> Now that we know how to create a listview
control, we will continue on how to use it. I'll focus on report view which
can demonstrate many features of listview control. The steps in using a listview
control are as follows:</font></p>
<ol>
<li><font face="MS Sans Serif" size="-1">Create a listview control with <font color="#FFFFCC"><b>CreateWindowEx</b></font>,
specifying <font color="#CCFFCC"><b>SysListView32 </b></font>as the class
name. You can specify the initial view at this time.</font></li>
<li><font face="MS Sans Serif" size="-1">(if exists) Create and initialize image
lists to be used with the listview items.</font></li>
<li><font face="MS Sans Serif" size="-1">Insert column(s) into the listview
control. This step is necessary if the listview control will use report view.</font></li>
<li><font face="MS Sans Serif" size="-1">Insert items and subitems into the
listview control.</font></li>
</ol>
<h4><font face="Arial, Helvetica, sans-serif">Columns</font></h4>
<p><font face="MS Sans Serif" size="-1">In report view, there are one or more
columns. You can think of the arrangement of data in the report view as a table:
the data are arranged in rows and columns. You must have at least one column
in your listview control (only in report view). In views other than report,
you need not insert a column because there can be one and only one column in
those views.<br>
You can insert a column by sending <font color="#CCFFCC"><b>LVM_INSERTCOLUMN</b></font>
to the listview control.</font></p>
<blockquote>
<p><font face="MS Sans Serif" size="-1"><b>LVM_INSERTCOLUMN</b><br>
wParam = iCol<br>
lParam = pointer to a <font color="#CCFFCC"><b>LV_COLUMN</b></font> structure</font></p>
</blockquote>
<p><font face="MS Sans Serif" size="-1">iCol is the column number, starting from
0.<br>
<font color="#CCFFCC"><b>LV_COLUMN</b></font> contains information about the
column to be inserted. It has the following definition:</font></p>
<blockquote>
<p><font face="Fixedsys">LV_COLUMN STRUCT <br>
imask dd ? <br>
fmt dd ? <br>
lx dd ? <br>
pszText dd ? <br>
cchTextMax dd ? <br>
iSubItem dd ? <br>
iImage dd ? <br>
iOrder dd ? <br>
LV_COLUMN ENDS </font></p>
</blockquote>
<table border="1" cellspacing="2" cellpadding="2" align="center">
<tr bgcolor="#006666">
<th><b><font face="MS Sans Serif" size="-1">Field name</font></b></th>
<th><font face="MS Sans Serif" size="-1">Meanings</font></th>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">imask</font></b></td>
<td>
<p><font face="MS Sans Serif" size="-1">A collection of flags that governs
which members in this structure are valid. The reason behind this member
is that not all members in this structure are used at the same time. Some
members are used in some situations. And this structure is used both for
input and output. Thus it's important that you *mark* the members that
are used in this call to Windows so Windows knows which members are valid.
The available flags are:</font></p>
<p><font face="MS Sans Serif" size="-1"><b><font color="#CCFFCC">LVCF_FMT</font></b>
= The <font color="#FFFFCC"><b>fmt</b></font> member is valid. <br>
<font color="#CCFFCC"><b>LVCF_SUBITEM</b></font> = The <font color="#FFFFCC"><b>iSubItem</b></font>
member is valid. <br>
<font color="#CCFFCC"><b>LVCF_TEXT</b></font> = The <font color="#FFFFCC"><b>pszText</b></font>
member is valid. <br>
<font color="#CCFFCC"><b>LVCF_WIDTH</b></font> = The <font color="#FFFFCC"><b>lx
</b></font>member is valid.</font></p>
<p><font face="MS Sans Serif" size="-1">You can combine the above flags.
For example, if you want to specify the text label of the column, you
must supply the pointer to the string in <font color="#FFFFCC"><b>pszText</b></font>
member. And you <font color="#FFCC33"><b>must</b></font> tell Windows
that pszText member contains data by specifying <font color="#CCFFCC"><b>LVCF_TEXT</b></font>
flag in this field else Windows will ignore the value in <font color="#FFFFCC"><b>pszText</b></font>.</font></p>
</td>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">fmt</font></b></td>
<td>
<p><font face="MS Sans Serif" size="-1">Specify the alignment of items/subitems
in the column. The available values are:</font></p>
<p><font face="MS Sans Serif" size="-1"><b><font color="#CCFFCC">LVCFMT_CENTER</font></b>
= Text is centered. <br>
<font color="#CCFFCC"><b>LVCFMT_LEFT</b></font> = Text is left-aligned.
<br>
<font color="#CCFFCC"><b>LVCFMT_RIGHT</b></font> = Text is right-aligned.</font></p>
</td>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">lx</font></b></td>
<td><font face="MS Sans Serif" size="-1">The width of the column, in pixels.
You can later change the width of the column with <font color="#FFFFCC"><b>LVM_SETCOLUMNWIDTH</b></font>.</font></td>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">pszText</font></b></td>
<td><font face="MS Sans Serif" size="-1">Contains a pointer to the name of
the column if this structure is used to set the column's properties. If
this structure is used to receive the properties of a column, this field
contains a pointer to a buffer large enough to receive the name of the column
that will be returned. In that case, you must give the size of the buffer
in <font color="#FFFFCC"><b>cchTextMax</b></font> below. You can ignore
<font color="#FFFFCC"> <b>cchTextMax</b></font> if you want to set the name
of the column because the name must be an ASCIIZ string which Windows can
determine the length.</font></td>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">cchTextMax</font></b></td>
<td><font face="MS Sans Serif" size="-1">The size, in bytes, of the buffer
specified in <font color="#FFFFCC"><b>pszText</b></font> above. This member
is used only when you use this structure to receive info about a column.
If you use this structure to set the properties of a column, this field
is ignored.</font></td>
</tr>
<tr bgcolor="#333300">
<td><b><font face="MS Sans Serif" size="-1">iSubItem</font></b></td>
<td><font face="MS Sans Serif" size="-1">Specify the index of subitem associated
with this column. This value is used as a marker which subitem this column
is associated with. If you want, you can specify an absurd number in this
field and your listview control will still run like a breeze. The use of
this field is best demonstrated when you have the column number and need
to know with which subitem this column is associated. You can query the
listview control by sending <font color="#CCFFCC"><b>LVM_GETCOLUMN</b></font>
message to it, specifying <font color="#CCFFCC"><b>LVCF_SUBITEM</b></font>
in the <font color="#FFFFCC"><b>imask</b></font> member. The listview control
will fill the <font color="#FFFFCC"><b>iSubItem</b></font> member with whatever
value you specify in this field when the column is inserted. In order for
this method to work, you need to input the correct subitem index into this
field. </font></td>
</tr>
<tr bgcolor="#333300">
<td><font face="MS Sans Serif" size="-1"><b>iImage and iOrder</b></font></td>
<td><font face="MS Sans Serif" size="-1">Used with Internet Explorer 3.0 upwards.
I don't have info about them.</font></td>
</tr>
</table>
<p><font face="MS Sans Serif" size="-1">So after the listview control is created,
you should insert one or more columns into it. Columns are not necessary if
you don't plan to switch the listview control into report view. In order to
insert a column, you need to create a <font color="#FFFFCC"><b>LV_COLUMN</b></font>
structure, fill it with necessary information, specify the column number and
then send the structure to the listview control with <font color="#CCFFCC"><b>LVM_INSERTCOLUMN</b></font>
message.</font></p>
<blockquote>
<p><font face="Fixedsys" size="-1"> LOCAL lvc:LV_COLUMN<br>
mov lvc.imask,LVCF_TEXT+LVCF_WIDTH <br>
mov lvc.pszText,offset Heading1 <br>
mov lvc.lx,150 <br>
invoke SendMessage,hList, LVM_INSERTCOLUMN,0,addr lvc </font></p>
</blockquote>
<p><font face="MS Sans Serif" size="-1">The above code snippet demonstrates the
process. It specifies the column header text and its width then send <font color="#CCFFCC"><b>LVM_INSERTCOLUMN</b></font>
message to the listview control. It's that simple.</font></p>
<h3><font face="Arial, Helvetica, sans-serif">Items and subitems</font></h3>
<p><font face="MS Sans Serif" size="-1">Items are the main entries in the listview
control. In views other than report view, you will only see items in the listview
control. Subitems are details of the items. An item may have one or more associated
subitems. For example, if the item is the name of a file, then you can have
the file attributes, its size, the date of file creation as subitems. In report
view, the leftmost column contains items and the remaining columns contain subitems.
You can think of an item and its subitems as a database record. The item is
the primary key of the record and the subitems are fields in the record. <br>
At the bare minimum, you need some items in your listview control: subitems
are not necessary. However, if you want to give the user more information about
the items, you can associate items with subitems so the user can see the details
in the report view.<br>
You insert an item into a listview control by sending<font color="#CCFFCC"><b>
LVM_INSERTITEM</b></font> message to it. You also need to pass the address of
an<font color="#CCFFCC"><b> LV_ITEM</b></font> structure to it in<font color="#FFFFCC"><b>
lParam</b></font>. <font color="#CCFFCC"><b>LV_ITEM</b></font> has the following
definition:</font></p>
<blockquote>
<p><font face="Fixedsys">LV_ITEM STRUCT <br>
imask dd ? <br>
iItem dd ? <br>
iSubItem dd ? <br>
state dd ? <br>
stateMask dd ? <br>
pszText dd ? <br>
cchTextMax dd ? <br>
iImage dd ? <br>
lParam dd ? <br>
iIndent dd ? <br>
LV_ITEM ENDS </font></p>
</blockquote>
<table border="1" cellspacing="2" cellpadding="2" align="center">
<tr bgcolor="#006666">
<th nowrap><font face="MS Sans Serif" size="-1">Field name</font></th>
<th nowrap><font face="MS Sans Serif" size="-1">Meanings</font></th>
</tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -