win32项目 树形控件TreeView的使用

Posted keepthreemunites

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了win32项目 树形控件TreeView的使用相关的知识,希望对你有一定的参考价值。

树形控件的结构体,LVITEMA structure

typedef struct tagLVITEMA {
  UINT   mask;
  int    iItem;
  int    iSubItem;
  UINT   state;
  UINT   stateMask;
  LPSTR  pszText;
  int    cchTextMax;
  int    iImage;
  LPARAM lParam;
  int    iIndent;
  int    iGroupId;
  UINT   cColumns;
  PUINT  puColumns;
  int    *piColFmt;
  int    iGroup;
} LVITEMA, *LPLVITEMA;

成员变量

mask

Type: UINT

Set of flags that specify which members of this structure contain data to be set or which members are being requested. This member can have one or more of the following flags set:

指定此结构的哪些成员包含要设置的数据或正在请求的成员的标志集。此成员可以设置以下一个或多个标志:

ValueMeaning
LVIF_COLFMT

Windows Vista and later. The piColFmt member is valid or must be set. If this flag is used, the cColumns member is valid or must be set.

Windows Vista及更高版本。piColFmt成员有效或必须设置。如果使用此标志,则cColumns成员有效或必须设置。

LVIF_COLUMNS

The cColumns member is valid or must be set.

cColumns成员有效或必须设置。

LVIF_DI_SETITEM

The operating system should store the requested list item information and not ask for it again. This flag is used only with the LVN_GETDISPINFO notification code.

操作系统应存储请求的列表项信息,而不是再次请求。此标志仅用于LVN_GETDISPINFO通知代码。

LVIF_GROUPID

The iGroupId member is valid or must be set. If this flag is not set when an LVM_INSERTITEM message is sent, the value of iGroupId is assumed to be I_GROUPIDCALLBACK.

iGroupId成员有效或必须设置。如果在发送LVM_INSERTITEM消息时未设置此标志,则假定iGroupId的值为I_GROUPIDCALLBACK。

LVIF_IMAGE

The iImage member is valid or must be set.

iImage成员有效或必须设置。

LVIF_INDENT

The iIndent member is valid or must be set.

iIndent成员有效或必须设置。

LVIF_NORECOMPUTE

The control will not generate LVN_GETDISPINFO to retrieve text information if it receives an LVM_GETITEM message. Instead, the pszText member will contain LPSTR_TEXTCALLBACK.

如果控件接收到LVM_GETITEM消息,则不会生成LVN_GETDISPINFO来检索文本信息。相反,pszText成员将包含LPSTR_TEXTCALLBACK。

LVIF_PARAM

The lParam member is valid or must be set.

lParam成员有效或必须设置。

LVIF_STATE

The state member is valid or must be set.

state成员有效或必须设置。

LVIF_TEXT

The pszText member is valid or must be set.

pszText成员有效或必须设置。

iItem

Type: int

Zero-based index of the item to which this structure refers.

此结构引用的项的从零开始的索引。

iSubItem

Type: int

One-based index of the subitem to which this structure refers, or zero if this structure refers to an item rather than a subitem.

此结构引用的子项的一个基索引,如果此结构引用的是项而不是子项,则为零。

state

Type: UINT

Indicates the item‘s state, state image, and overlay image. The stateMask member indicates the valid bits of this member.

指示项的状态、状态图像和覆盖图像。statemack成员指示此成员的有效位。

Bits 0 through 7 of this member contain the item state flags. This can be one or more of the item state values.

此成员的位0到7包含项状态标志。这可以是一个或多个项状态值。

Bits 8 through 11 of this member specify the one-based overlay image index. Both the full-sized icon image list and the small icon image list can have overlay images. The overlay image is superimposed over the item‘s icon image. If these bits are zero, the item has no overlay image. To isolate these bits, use the LVIS_OVERLAYMASK mask. To set the overlay image index in this member, you should use the INDEXTOOVERLAYMASK macro. The image list‘s overlay images are set with the ImageList_SetOverlayImage function.

此成员的位8到11指定基于一个的覆盖图像索引。全尺寸图标图像列表和小图标图像列表都可以有覆盖图像。覆盖图像叠加在项目的图标图像上。如果这些位为零,则该项没有覆盖图像。要隔离这些位,请使用LVIS_OVERLAYMASK掩码。若要在此成员中设置覆盖图像索引,应使用INDEXTOOVERLAYMASK宏。图像列表的覆盖图像是使用image list_set overlay image函数设置的。

Bits 12 through 15 of this member specify the state image index. The state image is displayed next to an item‘s icon to indicate an application-defined state. If these bits are zero, the item has no state image. To isolate these bits, use the LVIS_STATEIMAGEMASK mask. To set the state image index, use the INDEXTOSTATEIMAGEMASK macro. The state image index specifies the index of the image in the state image list that should be drawn. The state image list is specified with the LVM_SETIMAGELIST message.

此成员的位12到15指定状态图像索引。状态图像显示在项目图标旁边,指示应用程序定义的状态。如果这些位为零,则该项没有状态图像。要隔离这些位,请使用LVIS_STATEIMAGEMASK掩码。要设置状态图像索引,请使用indextostatimagemask宏。状态图像索引指定应在状态图像列表中绘制的图像的索引。状态映像列表是用LVM_SETIMAGELIST消息指定的。

stateMask

Type: UINT

Value specifying which bits of the state member will be retrieved or modified. For example, setting this member to LVIS_SELECTED will cause only the item‘s selection state to be retrieved.

指定将检索或修改状态成员的位的值。例如,将此成员设置为LVIS_SELECTED将只检索项目的选择状态。

This member allows you to modify one or more item states without having to retrieve all of the item states first. For example, setting this member to LVIS_SELECTED and state to zero will cause the item‘s selection state to be cleared, but none of the other states will be affected.

此成员允许您修改一个或多个项状态,而无需首先检索所有项状态。例如,将此成员设置为LVIS_SELECTED且state设置为零将导致清除项的选择状态,但不会影响其他任何状态。

To retrieve or modify all of the states, set this member to (UINT)-1.
要检索或修改所有状态,请将此成员设置为(UINT)-1。

You can use the macro ListView_SetItemState both to set and to clear bits.
您可以使用宏ListView_SetItemState来设置和清除位。

pszText

Type: LPTSTR

If the structure specifies item attributes, pszText is a pointer to a null-terminated string containing the item text. When responding to an LVN_GETDISPINFO notification, be sure that this pointer remains valid until after the next notification has been received.

如果结构指定项属性,则pszText是指向包含项文本的以空结尾的字符串的指针。响应LVN_GETDISPINFO通知时,请确保此指针在收到下一个通知之前保持有效。

If the structure receives item attributes, pszText is a pointer to a buffer that receives the item text. Note that although the list-view control allows any length string to be stored as item text, only the first 259 TCHARs are displayed.
如果结构接收到项属性,则pszText是指向接收项文本的缓冲区的指针。请注意,尽管列表视图控件允许将任何长度字符串存储为项文本,但仅显示前259tchars。

If the value of pszText is LPSTR_TEXTCALLBACK, the item is a callback item. If the callback text changes, you must explicitly set pszText to LPSTR_TEXTCALLBACK and notify the list-view control of the change by sending an LVM_SETITEM or LVM_SETITEMTEXT message.
如果pszText的值为LPSTR_TEXTCALLBACK,则该项为回调项。如果回调文本更改,则必须将pszText显式设置为LPSTR_text callback,并通过发送LVM_SETITEM或LVM_SETITEM text消息将更改通知列表视图控件。

Do not set pszText to LPSTR_TEXTCALLBACK if the list-view control has the LVS_SORTASCENDING or LVS_SORTDESCENDING style.
如果列表视图控件具有LVS-SORTASCENDING或LVS-SORTDESCENDING样式,则不要将pszText设置为LPSTR-TEXTCALLBACK。

cchTextMax

Type: int

Number of TCHARs in the buffer pointed to by pszText, including the terminating NULL.
pszText指向的缓冲区中的TCHARs数,包括终止空值。

This member is only used when the structure receives item attributes. It is ignored when the structure specifies item attributes. For example, cchTextMax is ignored during LVM_SETITEM and LVM_INSERTITEM. It is read-only during LVN_GETDISPINFO and other LVN_ notifications.
此成员仅在结构接收到项属性时使用。当结构指定项属性时,它将被忽略。例如,在LVM_SETITEM和LVM_INSERTITEM期间忽略cchTextMax。它在LVN_GETDISPINFO和其他LVN_通知期间是只读的。

Note Never copy more than cchTextMax TCHARs—where cchTextMax includes the terminating NULL—into pszText during an LVN_ notification, otherwise your program can fail.
注意:在LVNú通知期间,不要复制超过cchTextMax TCHARs的cchTextMax,其中cchTextMax将终止NULL包含到pszText中,否则您的程序可能会失败。

iImage
Type: int

Index of the item‘s icon in the control‘s image list. This applies to both the large and small image list. If this member is the I_IMAGECALLBACK value, the parent window is responsible for storing the index. In this case, the list-view control sends the parent an LVN_GETDISPINFO notification code to retrieve the index when it needs to display the image.
控件图像列表中项目图标的索引。这适用于大图像列表和小图像列表。如果此成员是I_IMAGECALLBACK值,则父窗口负责存储索引。在这种情况下,列表视图控件在需要显示图像时向父级发送LVN_GETDISPINFO通知代码以检索索引。

lParam
Type: LPARAM

Value specific to the item. If you use the LVM_SORTITEMS message, the list-view control passes this value to the application-defined comparison function. You can also use the LVM_FINDITEM message to search a list-view control for an item with a specified lParam value.
特定于项的值。如果使用LVM_SORTITEMS消息,列表视图控件会将此值传递给应用程序定义的比较函数。还可以使用LVM_FINDITEM消息在列表视图控件中搜索具有指定lParam值的项。

iIndent

Type: int

Version 4.70. Number of image widths to indent the item. A single indentation equals the width of an item image. Therefore, the value 1 indents the item by the width of one image, the value 2 indents by two images, and so on. Note that this field is supported only for items. Attempting to set subitem indentation will cause the calling function to fail.
版本4.70。缩进项目的图像宽度数。一个缩进等于项目图像的宽度。因此,值1按一个图像的宽度缩进项,值2按两个图像缩进项,依此类推。请注意,此字段仅支持项。试图设置子项缩进将导致调用函数失败。

iGroupId

Type: int

Version 6.0 Identifier of the group that the item belongs to, or one of the following values.
项所属组的版本6标识符,或下列值之一。

 

ValueMeaning
I_GROUPIDCALLBACK

The listview control sends the parent an LVN_GETDISPINFO notification code to retrieve the index of the group.

listview控件向父级发送LVN_GETDISPINFO通知代码,以检索组的索引。

I_GROUPIDNONE

The item does not belong to a group.

该项不属于组。

cColumns

Type: UINT

Version 6.0 Number of data columns (subitems) to display for this item in tile view. The maximum value is 20. If this value is I_COLUMNSCALLBACK, the size of the column array and the array itself (puColumns) are obtained by sending a LVN_GETDISPINFO notification.
版本6.0要在平铺视图中为此项显示的数据列(子项)数。最大值是20。如果此值为I_COLUMNSCALLBACK,则通过发送LVN_GETDISPINFO通知获得列数组的大小和数组本身。

puColumns

Type: PUINT

Version 6.0 A pointer to an array of column indices, specifying which columns are displayed for this item, and the order of those columns.
版本6.0指向列索引数组的指针,指定为此项显示哪些列以及这些列的顺序。

piColFmt

Type: int*

Windows Vista: Not implemented. Windows 7 and later: A pointer to an array of the following flags (alone or in combination), specifying the format of each subitem in extended tile view.
Windows Vista:未实现。Windows7及更高版本:指向下列标志数组(单独或组合)的指针,指定扩展平铺视图中每个子项的格式。

ValueMeaning
LVCFMT_LINE_BREAK

Forces the column to wrap to the top of the next list of columns.

强制将列换行到下一列列表的顶部。

LVCFMT_FILL

Fills the remainder of the tile area. Might have a title.

填充平铺区域的其余部分。可能有个标题。

LVCFMT_WRAP

Allows the column to wrap within the remaining space in its list of columns.

允许列在其列列表的剩余空间内换行。

LVCFMT_NO_TITLE

Removes the title from the subitem.

从子项中删除标题。

LVCFMT_TILE_PLACEMENTMASK

Equivalent to a combination of LVCFMT_LINE_BREAK and LVCFMT_FILL.

相当于LVCFMT_LINE_BREAK和LVCFMT_FILL的组合。

iGroup

Type: int

Windows Vista: Group index of the item. Valid only for owner data/callback (single item in multiple groups).
Windows Vista:项的组索引。仅对所有者数据/回调有效(多个组中的单个项)。

Remarks

The LVITEM structure is used with several messages, including LVM_GETITEM, LVM_SETITEM, LVM_INSERTITEM, and LVM_DELETEITEM.
LVITEM结构用于多个消息,包括LVM_GETITEM、LVM_SETITEM、LVM_INSERTITEM和LVM_deleteeitem。

In tile view, the item name is displayed to the right of the icon. You can specify additional subitems (corresponding to columns in the details view), to be displayed on lines below the item name. The puColumns array contains the indices of subitems to be displayed. Indices should be greater than 0, because subitem 0, the item name, is already displayed. Column information can also be set in the LVTILEINFO structure when modifying the list item.
在平铺视图中,项目名称显示在图标的右侧。可以指定其他子项(对应于“详细信息”视图中的列),这些子项将显示在项名称下面的行上。puColumns数组包含要显示的子项的索引。索引应大于0,因为已显示项名称子项0。在修改列表项时,还可以在LVTILEINFO结构中设置列信息。

Note Comctl32.dll version 6 is not redistributable but it is included in Windows or later. To use Comctl32.dll version 6, specify it in a manifest. For more information on manifests, see Enabling Visual Styles.
注意:Comctl32.dll版本6不可再发行,但它包含在Windows或更高版本中。要使用Comctl32.dll版本6,请在清单中指定它。有关清单的详细信息,请参见启用视觉样式。

Requirements

Minimum supported client Windows Vista [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Header commctrl.h

参考文档:MSDN

个人见解:

新建win32项目之后,打开rc文件,通过工具箱拖拽TreeView控件到对话框中。

通过GetDlgItem函数,获取控件句柄。

然后设置LVITEM结构体,然后使用SendMessage函数发送此结构到控件上。

控件显示结构体中设置的文字等信息。

还有一些别的结构体,如:LVCOLUMN、TVITEM、TVINSERTSTRUCT。

SDK开发,就是通过这些结构体,使用Sendmessage,控制及实现控件的各种显示效果。

以上是关于win32项目 树形控件TreeView的使用的主要内容,如果未能解决你的问题,请参考以下文章

获取 Win32 TreeView 控件的宽度

winForm树形菜单,除了用TreeView还有其他的控件吗?

[WTL/ATL]_[初级]_[TreeView控件如何显示ToolTip]

Python可视化编程 TreeView树形控件

[ATL/WTL]_[初级]_[自定义多列TreeView]

树形控件的节点图标显示问题(实现每个子节点的图标不一样)