📄 lion-tutorial31.htm
字号:
<html>
<head>
<link rel="stylesheet" href="../../asm.css">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Iczelion's win32 asm tutorial</title>
</head>
<body bgcolor="#FFFFFF" background="../../images/back01.jpg">
<p align="center">Tutorial 31: Listview Control</p>
<hr size="1">
We will learn how to create and use the listview control in this tutorial.
<p>Download <b><a href="files/tut31.zip" style="text-decoration:none">the example</a></b>.</p>
<h3>Theory:</h3>
<p>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 <b>InitCommonControls</b>
within your asm source code. The other method is to call <b>CreateWindowEx</b>
in your source code. You must specify the correct window class name for the
control, ie. <b>SysListView32</b>. 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 <b>SetWindowLong</b>, specifying <b>GWL_STYLE</b> flag.</p>
<p> 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:</p>
<ol>
<li>Create a listview control with <b>CreateWindowEx</b>, specifying <b>SysListView32
</b>as the class name. You can specify the initial view at this time.</li>
<li>(if exists) Create and initialize image lists to be used with the listview
items.</li>
<li>Insert column(s) into the listview control. This step is necessary if the
listview control will use report view.</li>
<li>Insert items and subitems into the listview control.</li>
</ol>
<h4>Columns</h4>
<p>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 <b>LVM_INSERTCOLUMN</b> to the listview control.</p>
<blockquote>
<p><b>LVM_INSERTCOLUMN</b><br>
wParam = iCol<br>
lParam = pointer to a <b>LV_COLUMN</b> structure</p>
</blockquote>
<p>iCol is the column number, starting from 0.<br>
<b>LV_COLUMN</b> contains information about the column to be inserted. It has
the following definition:</p>
<blockquote>
<p>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 </p>
</blockquote>
<table border="1" cellspacing="2" cellpadding="2" align="center">
<tr>
<th bgcolor="#006666"><b>Field name</b></th>
<th bgcolor="#006666">Meanings</th>
</tr>
<tr>
<td><b>imask</b></td>
<td>
<p>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:</p>
<p><b>LVCF_FMT</b> = The <b>fmt</b> member is valid. <br>
<b>LVCF_SUBITEM</b> = The <b>iSubItem</b> member is valid. <br>
<b>LVCF_TEXT</b> = The <b>pszText</b> member is valid. <br>
<b>LVCF_WIDTH</b> = The <b>lx </b>member is valid.</p>
<p>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 <b>pszText</b> member. And you <b>must</b> tell Windows that pszText
member contains data by specifying <b>LVCF_TEXT</b> flag in this field
else Windows will ignore the value in <b>pszText</b>.</p>
</td>
</tr>
<tr>
<td><b>fmt</b></td>
<td>
<p>Specify the alignment of items/subitems in the column. The available
values are:</p>
<p><b>LVCFMT_CENTER</b> = Text is centered. <br>
<b>LVCFMT_LEFT</b> = Text is left-aligned. <br>
<b>LVCFMT_RIGHT</b> = Text is right-aligned.</p>
</td>
</tr>
<tr>
<td><b>lx</b></td>
<td>The width of the column, in pixels. You can later change the width of
the column with <b>LVM_SETCOLUMNWIDTH</b>.</td>
</tr>
<tr>
<td><b>pszText</b></td>
<td>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 <b>cchTextMax</b> below. You
can ignore <b>cchTextMax</b> if you want to set the name of the column because
the name must be an ASCIIZ string which Windows can determine the length.</td>
</tr>
<tr>
<td><b>cchTextMax</b></td>
<td>The size, in bytes, of the buffer specified in <b>pszText</b> 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.</td>
</tr>
<tr>
<td><b>iSubItem</b></td>
<td>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 <b>LVM_GETCOLUMN</b>
message to it, specifying <b>LVCF_SUBITEM</b> in the <b>imask</b> member.
The listview control will fill the <b>iSubItem</b> 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. </td>
</tr>
<tr>
<td><b>iImage and iOrder</b></td>
<td>Used with Internet Explorer 3.0 upwards. I don't have info about them.</td>
</tr>
</table>
<p>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
<b>LV_COLUMN</b> structure, fill it with necessary information, specify the
column number and then send the structure to the listview control with <b>LVM_INSERTCOLUMN</b>
message.</p>
<blockquote>
<p> 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 </p>
</blockquote>
<p>The above code snippet demonstrates the process. It specifies the column header
text and its width then send <b>LVM_INSERTCOLUMN</b> message to the listview
control. It's that simple.</p>
<h3>Items and subitems</h3>
<p>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<b> LVM_INSERTITEM</b>
message to it. You also need to pass the address of an<b> LV_ITEM</b> structure
to it in<b> lParam</b>. <b>LV_ITEM</b> has the following definition:</p>
<blockquote>
<p>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 </p>
</blockquote>
<table border="1" cellspacing="2" cellpadding="2" align="center">
<tr>
<th nowrap bgcolor="#006666">Field name</th>
<th nowrap bgcolor="#006666">Meanings</th>
</tr>
<tr>
<td>imask</td>
<td>A collection of flags indicating which members in this structure are valid
for this call. In general, this field is similar to <b>imask</b> member
of <b>LV_COLUMN</b> above. Check your win32 api reference for more detail
on the available flags.</td>
</tr>
<tr>
<td>iItem</td>
<td>The index of the item this structure refers to. The index is zero-based.
You can think of this field as containing the "row" number of
a table.</td>
</tr>
<tr>
<td>iSubItem</td>
<td>The index of the subitem associated with the item specified by<b> iItem</b>
above. You can think of this field as containing the "column"
of a table. For example, if you want to insert an item into a newly created
listview control, the value in<b> iItem</b> would be 0 (because this item
is the first one), and the value in <b>iSubItem</b> would also be 0 (we
want to insert the item into the first column). If you want to specify a
subitem associated with this item, the <b>iItem</b> would be the index of
the item you want to associate with (in the above example, it's 0), the
<b>iSubItem</b> would be 1 or greater, depending on which column you want
to insert the subitem into. For example, if your listview control has 4
columns, the first column will contain the items. The remaining 3 columns
are for subitems. If you want to insert a subitem into the 4th column, you
need to specify the value 3 in <b>iSubItem</b>. </td>
</tr>
<tr>
<td>state</td>
<td>
<p>This member contains flags that reflect the status of the item. The state
of an item can change because of the user's actions or it can be modified
by our program. The state includes whether the item has the focus/is hilited/is
selected for cut operation/is selected. In addition to the state flags,
It can also contains one-based index into the overlay image/state image
for use by the item. </p>
</td>
</tr>
<tr>
<td>stateMask</td>
<td>Since the state member above can contain the state flags, overlay image
index , and state image index, we need to tell Windows which value we want
to set or retrieve. The value in this field is for such use.</td>
</tr>
<tr>
<td>pszText</td>
<td>The address of an ASCIIZ string that will be used as the label of the
item in the case we want to set/insert the item. In the case that we use
this structure to retrieve the item's property, this member must contain
the address of a buffer that will be filled with the label of the item.</td>
</tr>
<tr>
<td>cchTextMax</td>
<td>This field is used only when you use this structure to receive info about
an item. In this case, this field contains the size in bytes of the buffer
specified in <b>pszText</b> above.</td>
</tr>
<tr>
<td>iImage</td>
<td>The index into the imagelist containing the icons for the listview control.
This index points to the icon to be used with this item.</td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -