⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lion-tutorial19.htm

📁 内有一些代码
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<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 19: Tree View Control</p>
<hr size="1">
<strong> </strong> In this tutorial, we will learn how to use tree view control. 
Moreover, we will also learn how to do drag and drop under tree view control and 
how to use an image list with it. <br>
Download the example<a href="files/tut19.zip"> here</a>. 
<h3> Theory:</h3>
A tree view control is a special kind of window that represents objects in hierarchical 
order. An example of it is the left pane of Windows Explorer. You can use this 
control to show relationships between objects. <br>
You can create a tree view control by calling CreateWindowEx, passing "SysTreeView32" 
as the class name or you can incorporate it into a dialog box. Don't forget to 
put InitCommonControls call in your code. <br>
There are several styles specific to the tree view control. These three are the 
ones mostly used. 
<ul>
  <b>TVS_HASBUTTONS == Displays plus (+) and minus (-) buttons next to parent 
  items. The user clicks the buttons to expand or collapse a parent item's list 
  of child items. To include buttons with items at the root of the tree view, 
  TVS_LINESATROOT must also be specified.</b> <br>
  <b>TVS_HASLINES == Uses lines to show the hierarchy of items.</b> <br>
  <b>TVS_LINESATROOT == Uses lines to link items at the root of the tree-view 
  control. This value is ignored if TVS_HASLINES is not also specified.</b> 
</ul>
The tree view control, like other common controls, communicates with the parent 
window via messages. The parent window can send various messages to it and the 
tree view control can send "notification" messages to its parent window. In this 
regard, the tree view control is not different that any window. <br>
When something interesting occurs to it, it sends a <b>WM_NOTIFY </b>message to 
the parent window with accompanying information. 
<ul>
  <b>WM_NOTIFY</b> <br>
  <b>wParam == Control ID, this value is not guaranteed to be unique so we don't 
  use it.</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  Instead, we use hwndFrom or IDFrom member of the NMHDR structure</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  pointed to by lParam</b> <br>
  <b>lParam == Pointer to NMHDR structure. Some controls may pass a pointer to 
  larger</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  structure but it must have a NMHDR structure as its first member.</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  That is, when you have lParam, you can be sure that it points to a</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  NMHDR structure at least.</b> 
</ul>
Next we will examine NMHDR structure. 
<ul>
  <b>NMHDR struct DWORD</b> <br>
  <b>&nbsp;&nbsp;&nbsp; hwndFrom&nbsp;&nbsp;&nbsp; DWORD ?</b> <br>
  <b>&nbsp;&nbsp;&nbsp; idFrom&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD ?</b> <br>
  <b>&nbsp;&nbsp;&nbsp; code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD ?</b> <br>
  <b>NMHDR ends</b> 
</ul>
hwndFrom is the window handle of the control that sends this <b>WM_NOTIFY</b> 
message. <br>
idFrom is the control ID of the control that sends this WM_NOTIFY message. <br>
code is the actual message the control wants to send to the parent window. <br>
Tree view notifications are those with TVN_ at the beginning of the name. Tree 
view messages are those with TVM_, like <b>TVM_CREATEDRAGIMAGE</b>. The tree view 
control sends TVN_xxxx in the code member of NMHDR. The parent window can send 
TVM_xxxx to control it. 
<h4> Adding items to a tree view control</h4>
After you create a tree view control, you can add items to it. You can do this 
by sending <b>TVM_INSERTITEM </b>to it. 
<ul>
  <b>TVM_INSERTITEM</b> <br>
  <b>wParam = 0;</b> <br>
  <b>lParam = pointer to a TV_INSERTSTRUCT;</b> 
</ul>
You should know some terminology at this point about the relationship between 
items in the tree view control. <br>
An item can be parent, child, or both at the same time. A parent item is the item 
that has some other subitem(s) associated with it. At the same time, the parent 
item may be a child of some other item. An item without a parent is called a root 
item. There can be many root items in a tree view control. Now we examine<b> TV_INSERTSTRUCT</b> 
structure 
<ul>
  <b>TV_INSERTSTRUCT STRUCT DWORD</b> <br>
  <b>&nbsp; hParent&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ?</b> <br>
  <b>&nbsp; hInsertAfter&nbsp; DWORD ?</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ITEMTYPE &lt;></b> <br>
  <b>TV_INSERTSTRUCT ENDS</b> 
</ul>
<b>hParent</b> = Handle to the parent item. If this member is the <b>TVI_ROOT</b> 
value or NULL, the item is inserted at the root of the tree-view control. <br>
<b>hInsertAfter</b> = Handle to the item after which the new item is to be inserted 
or one of the following values: 
<ul>
  <li> TVI_FIRST ==> Inserts the item at the beginning of the list.</li>
  <li> TVI_LAST ==> Inserts the item at the end of the list.</li>
  <li> TVI_SORT ==> Inserts the item into the list in alphabetical order.</li>
</ul>
<ul>
  <b>ITEMTYPE UNION</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; itemex TVITEMEX &lt;></b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; item TVITEM &lt;></b> <br>
  <b>ITEMTYPE ENDS</b> 
</ul>
We will use only TVITEM here. 
<ul>
  <b>TV_ITEM STRUCT DWORD</b> <br>
  <b>&nbsp; imask&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>&nbsp; hItem&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>&nbsp; state&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>&nbsp; stateMask&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ?</b> <br>
  <b>&nbsp; pszText&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>&nbsp; cchTextMax&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ?</b> <br>
  <b>&nbsp; iImage&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>&nbsp; iSelectedImage&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ?</b> <br>
  <b>&nbsp; cChildren&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  ?</b> <br>
  <b>&nbsp; lParam&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  DWORD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ?</b> <br>
  <b>TV_ITEM ENDS</b> 
</ul>
This structure is used to send and receive info about a tree view item, depending 
on messages. For example, with <b>TVM_INSERTITEM</b>, it is used to specify the 
attribute of the item to be inserted into the tree view control. With <b>TVM_GETITEM</b>, 
it'll be filled with information about the selected tree view item. <br>
<b>imask </b>is used to specify which member(s) of the <b>TV_ITEM</b> structure 
is (are) valid. For example, if the value in imask is <b>TVIF_TEXT</b>, it means 
only the pszText member is valid. You can combine several flags together. <br>
<b>hItem </b>is the handle to the tree view item. Each item has its own handle, 
like a window handle. If you want to do something with an item, you must select 
it by its handle. <br>
<b>pszText</b> is the pointer to a null-terminated string that is the label of 
the tree view item. <br>
<b>cchTextMax</b> is used only when you want to retrieve the label of the tree 
view item. Because you will supply the pointer to the buffer in pszText, Windows 
has to know the size of the provided buffer. You have to give the size of the 
buffer in this member. <br>
<b>iImage </b>and <b>iSelectedImage</b> refers to the index into an image list 
that contains the images to be shown when the item is not selected and when it's 
selected. If you recall Windows Explorer left pane, the folder images are specified 
by these two members. <br>
In order to insert an item into the tree view control, you must at least fill 
in the hParent, hInsertAfter and you should fill imask and pszText members as 
well. 
<h4> Adding images to the tree view control</h4>
If you want to put an image to the left of the tree view item's label, you have 
to create an image list and associate it with the tree view control. You can create 
an image list by calling <b>ImageList_Create</b>. 
<ul>
  <b>ImageList_Create PROTO cx:DWORD, cy:DWORD, flags:DWORD, \</b> <br>
  <b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  cInitial:DWORD,&nbsp; cGrow:DWORD</b> 
</ul>
This function returns the handle to an empty image list if successful. <br>
<b>cx </b>== width of each image in this image list, in pixels. <br>
<b>cy</b> == height of each image in this image list, in pixels. Every image in 
an image list must be equal to each other in size. If you specify a large bitmap, 
Windows will use cx and cy to *cut* it into several pieces. So you should prepare 
your own image as a strip of pictures with identical dimensions. <br>
<b>flags</b> == specify the type of images in this image list whether they are 
color or monochrome and their color depth. Consult your win32 api reference for 
more detail <br>
<b>cInitial</b> == The number of images that this image list will initially contain. 
Windows will use this info to allocate memory for the images. <br>
<b>cGrow</b> == Amount of images by which the image list can grow when the system 
needs to resize the list to make room for new images. This parameter represents 
the number of new images that the resized image list can contain. <br>
An image list is not a window! It's only an image deposit for use by other windows. 
<br>
After an image list is created, you can add images to it by calling <b>ImageList_Add</b> 
<ul>
  <b>ImageList_Add PROTO himl:DWORD, hbmImage:DWORD, hbmMask:DWORD</b> 
</ul>
This function returns -1 if unsuccessful. <br>
<b>himl </b>== the handle of the image list you want to add images to. It is the 
value returned by a successful call to <b>ImageList_Create</b> <br>
<b>hbmImage</b> == the handle to the bitmap to be added to the image list. You 
usually store the bitmap in the resource and load it with <b>LoadBitmap</b> call. 
Note that you don't have to specify the number of images contained in this bitmap 
because this information is inferred from cx and cy parameters passed to <b>ImageList_Create 
</b>call. <br>
<b>hbmMask</b> == Handle to the bitmap that contains the mask. If no mask is used 
with the image list, this parameter is ignored. <br>
Normally, we will add only two images to the image list for use with the tree 
view control: one that is used when the tree view item is not selected, and the 
other when the item is selected. <br>
When the image list is ready, you associate it with the tree view control by sending 
<b>TVM_SETIMAGELIST </b>to the tree view control. 
<ul>
  <b>TVM_SETIMAGELIST</b> <br>
  <b>wParam = type of image list to set. There are two choices:</b> 
  <ul>
    <li> <b>TVSIL_NORMAL Set the normal image list, which contains the selected 
      and unselected images for the tree-view item.</b></li>
    <li> <b>TVSIL_STATE Set the state image list, which contains the images for 
      tree-view items that are in a user-defined state.</b></li>
  </ul>
  <b>lParam = Handle to the image list</b> 
</ul>
<h4> Retrieve the info about tree view item</h4>
You can retrieve the information about a tree view item by sending <b>TVM_GETITEM</b> 
message. 
<ul>
  <b>TVM_GETITEM</b> <br>
  <b>wParam = 0</b> <br>
  <b>lParam = pointer to the TV_ITEM structure to be filled with the information</b> 
</ul>
Before you send this message, you must fill imask member with the flag(s) that 
specifies which member(s) of <b>TV_ITEM</b> you want Windows to fill. And most 
importantly, you must fill hItem with the handle to the item you want to get information 
from. And this poses a problem: How can you know the handle of the item you want 
to retrieve info from? Will you have to store all tree view handles? <br>
The answer is quite simple: you don't have to. You can send <b>TVM_GETNEXTITEM</b> 
message to the tree view control to retrieve the handle to the tree view item 
that has the attribute(s) you specified. For example, you can query the handle 
of the first child item, the root item, the selected item, and so on. 
<ul>
  <b>TVM_GETNEXTITEM</b> <br>
  <b>wParam = flag</b> <br>
  <b>lParam = handle to a tree view item (only necessary for some flag values)</b> 
</ul>
The value in wParam is very important so I present all the flags below: 
<ul>
  <ul>
    <li> <b>TVGN_CARET</b> Retrieves the currently selected item.</li>
    <li> <b>TVGN_CHILD</b> Retrieves the first child item of the item specified 
      by the hitem parameter</li>
    <li> <b>TVGN_DROPHILITE</b> Retrieves the item that is the target of a drag-and-drop 
      operation.</li>
    <li> <b>TVGN_FIRSTVISIBLE</b> Retrieves the first visible item.</li>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -