WIN32 SDK Tree Control 学习

Win32 SDK Tree Control 


  • 准备工作

  1. 创建一个win32项目
  2. ​​添加头文件
#include "commctrl.h"

     3.Tree control属于通用控件,所以需要在代码中加入InitCommonControls();函数,而该函数是动态链接库comctl32.dll中的一个函数,所以需要加上comctl32.lib

  • 创建

  1. 直接使用CreateWindow();函数创建
 HWND  hTreeView = CreateWindow("SysTreeView32", NULL, 
 WS_CHILD | WS_BORDER |WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
 10, 20, 200, 400, hDlg, NULL, NULL, NULL);

返回的是TreeControl的句柄

拓展:树形控件的style

TVS_HASLINES 在父/子结点之间绘制连线
TVS_LINESATROOT 在根/子结点之间绘制连线
TVS_HASBUTTONS 在每一个结点前添加一个按钮,用于表示当前结点是否已被展开
TVS_EDITLABELS 结点的显示字符可以被编辑
TVS_SHOWSELALWAYS 在失去焦点时也显示当前选中的结点
TVS_DISABLEDRAGDROP 不允许Drag/Drop
TVS_NOTOOLTIPS 不使用ToolTip显示结点的显示字符

     2.直接在资源文件里添加一个对话框,然后添加tree control

它的句柄可以通过GetDlgItem(hDlg, IDC_TREE1);函数获得

对话框中的树形控件style设置

 

  • WM_NOTIFY消息

     跟其他通用控件一样,树形控件也是通过windows的消息机制实现通信的,当事件发生时,会向Tree所在的父窗口发送一个WM_NOTIFY消息。

WM_NOTIFY 
    idCtrl = (int) wParam; 
    pnmh = (LPNMHDR) lParam; // 指向一个NMHDR结构体的指针

Sent by a common control to its parent window when an event has occurred in the control or the control requires some kind of information.

//当一个事件发生时或者控件需要一些各种各样的的信息时,通用控件会发送一个WM_NOTIFY消息给它的父窗口。

  • The return value is ignored except for notification messages that specify otherwise.

// 返回值除了指定的通知消息之外会被忽略。

idCtrl

Identifier of the common control sending the message. This identifier is not guaranteed to be unique. An application should use the hwndFrom or idFrom member of the NMHDR structure (passed as the lParam parameter) to identify the control.

//通用控件发送的消息标识符,该标识符不保证唯一,一个应用程序使用结构体NMHDR中的hwndFrom 或者 idForm成员(作为lParam参数传递)来标识控件。

pnmh

Address of an NMHDR structure that contains the notification code and additional information. For some notification messages, this parameter points to a larger structure that has the NMHDR structure as its first member.

//指向一个包含通知码和附加信息的NMHDR结构体的指针,为了一些通知消息,此参数指向一个大的结构体,且该结构体将NMHDR结构体作为它的第一个成员。·

Not all controls will send WM_NOTIFY messages. In particular, the standard Windows controls (edit controls, combo boxes, list boxes, buttons, scroll bars, and static controls) do not send WM_NOTIFY messages. Consult the documentation for the control to determine if it will send any WM_NOTIFY messages and, if it will, which notification codes it will send.

//不是所有的控件都发送WM_NOTIFY消息,尤其是,标准Windows 控件(编辑控件,组合框,列表框,按钮,滚动条,静态文本框)都不会发送WM_NOTIFY消息。 查阅控件的文档,以确定它是否将发送任何WM_NOTIFY消息,并且,如果它将发送,它将发送哪些通知代码。   

  • 添加Item

 创建树形视图完毕后,可以发送TVM_INSERTITEM消息向里面加入项目。

(1)第一种方式,使用系统定义的宏发送消息或者直接自己发送消息

//TVM_INSERTITEM 增加
#define TreeView_InsertItem(hwnd, lpis) \
    (HTREEITEM)SNDMSG((hwnd), TVM_INSERTITEM, 0, (LPARAM)(LPTV_INSERTSTRUCT)(lpis))

//TVM_DELETEITEM 删除
#define TreeView_DeleteItem(hwnd, hitem) \
    (BOOL)SNDMSG((hwnd), TVM_DELETEITEM, 0, (LPARAM)(HTREEITEM)(hitem))

//TVM_DELETEITEM 删除所有
#define TreeView_DeleteAllItems(hwnd) \
    (BOOL)SNDMSG((hwnd), TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT)


 


typedef struct tagTVINSERTSTRUCT {
    HTREEITEM hParent;
    HTREEITEM hInsertAfter;
#if (_WIN32_IE >= 0x0400)
    union
    {
        TVITEMEX itemex;
        TVITEM item;
    } DUMMYUNIONNAME;
#else
    TVITEM item;
#endif
} TVINSERTSTRUCT, FAR *LPTVINSERTSTRUCT;

Contains information used to add a new item to a tree view control. This structure is used with the TVM_INSERTITEM message. The structure is identical to the TV_INSERTSTRUCT structure, but it has been renamed to follow current naming conventions.

//包含用来向树形添加的新项目的信息,这个结构体用来和TVM_INSERTITEM消息一起使用,该结构与TV_INSERTSTRUCT结构相同,但已更名为遵循当前命名约定。

hParent

Handle to the parent item. If this member is the TVI_ROOT value or NULL, the item is inserted at the root of the tree view control.

//父项目的句柄,如果该成员为TVI_ROOT或者是NULL,这个项目将会被该项插入到树视图控件的根项目中。

hInsertAfter

Handle to the item after which the new item is to be inserted, or one of the following values:
TVI_FIRSTInserts the item at the beginning of the list.
TVI_LASTInserts the item at the end of the list.
TVI_SORTInserts the item into the list in alphabetical order.

itemex

Version 4.71. TVITEMEX structure that contains information about the item to add.

item

TVITEM structure that contains information about the item to add.

   用来描述每一个树节点的结构体

typedef struct tagTVITEM{
    UINT      mask;
    HTREEITEM hItem;
    UINT      state;
    UINT      stateMask;
    LPTSTR    pszText;
    int       cchTextMax;
    int       iImage;
    int       iSelectedImage;
    int       cChildren;
    LPARAM    lParam;
} TVITEM, FAR *LPTVITEM;

Specifies or receives attributes of a tree view item. This structure is identical to the TV_ITEM structure, but it has been renamed to follow current naming conventions. New applications should use this structure.

mask

Array of flags that indicate which of the other structure members contain valid data. When this structure is used with the TVM_GETITEM message, the mask member indicates the item attributes to retrieve. This member can be one or more of the following values:

TVIF_CHILDRENThe cChildren member is valid.
TVIF_HANDLEThe hItem member is valid.
TVIF_IMAGEThe iImage member is valid.
TVIF_PARAMThe lParam member is valid.
TVIF_SELECTEDIMAGEThe iSelectedImage member is valid.
TVIF_STATEThe state and stateMask members are valid.
TVIF_TEXTThe pszText and cchTextMax members are valid.

pszText

Address of a null-terminated string that contains the item text if the structure specifies item attributes. If this member is the LPSTR_TEXTCALLBACK value, the parent window is responsible for storing the name. In this case, the tree view control sends the parent window a TVN_GETDISPINFO notification message when it needs the item text for displaying, sorting, or editing and a TVN_SETDISPINFO notification message when the item text changes.

If the structure is receiving item attributes, this member is the address of the buffer that receives the item text.

imask 用来指定TV_ITEM的那些成员变量有效。譬如,如果指定了TVIF_TEXT,这意味着pszText成员变量是有效的。您可以同时指定几个标志位。
hItem 是树型视图项目的句柄。每一个项目都有它自己的句柄,就像窗口一样。如果您想要操作一个项目,就必须选择它的句柄。
pszText 是一个字符串指针。它是项目的标签名。
cchTextMax仅在查询项目的名称时使用。由于在pszText中指定了指针,WINDOWS还要知道该缓冲去的大小。所以您必须给出该值。
iImage 和 iSelectedImage用来指定图象列表以及一个索引号。这样就知道当项目被选中或没被选中时用哪个图象来表示该项目。像资源管理器中左边窗口中的文件夹等小图表就是有这两个参数来决定的。 
为了在树型视图中插入一个项目,您必须至少设定hParent, hInsertAfter,另外您还要设定imask和pszText值。

  • 详细代码

#include <windows.h>
#include "resource.h"
#include "commctrl.h"

BOOL CALLBACK DialogProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);
HTREEITEM MY_INSERT_ITEM_DLG(HWND hDlg, HTREEITEM hParent, char* pStrText);
HTREEITEM MY_INSERT_ITEM_CREATEWND(HWND hTree, HTREEITEM hParent, char* pStrText);

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,DialogProc);
	
	return 0;
}

BOOL CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	TVINSERTSTRUCT TreeInsertItem;
	
	switch (uMsg)
	{
	      case WM_INITDIALOG:
		      {   //对话框创建方式
			      InitCommonControls();
				  HTREEITEM hRoot1 = MY_INSERT_ITEM_DLG(hDlg, NULL, "头结点1");
				  MY_INSERT_ITEM_DLG(hDlg,hRoot1,"结点一");
				  MY_INSERT_ITEM_DLG(hDlg, hRoot1, "结点二");
				  MY_INSERT_ITEM_DLG(hDlg, hRoot1, "结点三");
				  HTREEITEM hRoot2 = MY_INSERT_ITEM_DLG(hDlg, NULL, "头结点2");
				  MY_INSERT_ITEM_DLG(hDlg, hRoot2, "结点一");
				  MY_INSERT_ITEM_DLG(hDlg, hRoot2, "结点二");
				  MY_INSERT_ITEM_DLG(hDlg, hRoot2, "结点三");
				  //CreateWindow
				  HWND  hTreeView = CreateWindow("SysTreeView32", NULL, WS_CHILD | WS_BORDER | WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT, 10, 20, 200, 400, hDlg, NULL, NULL, NULL);
				  HTREEITEM hRoot3 = MY_INSERT_ITEM_CREATEWND(hTreeView, NULL, "头结点3");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot3, "结点一");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot3, "结点二");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot3, "结点三");
				  HTREEITEM hRoot4 = MY_INSERT_ITEM_CREATEWND(hTreeView, NULL, "头结点4");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot4, "结点一");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot4, "结点二");
				  MY_INSERT_ITEM_CREATEWND(hTreeView, hRoot4, "结点三");
		      }
		      break;
		  case WM_NOTIFY:
		      {

		      }
			  break;
		  case WM_CLOSE:
		      {
			      EndDialog(hDlg,WM_CLOSE);
		      }
		      break;
		  default:
			  return FALSE;
	}

	return TRUE;
}

HTREEITEM MY_INSERT_ITEM_DLG(HWND hDlg, HTREEITEM hParent, char* pStrText)
{
	if (NULL!=pStrText)
	{
		TVINSERTSTRUCT TvInsertItem;
		TvInsertItem.hParent = hParent;
		TvInsertItem.hInsertAfter = TVI_LAST;
		TvInsertItem.item.mask = TVIF_TEXT;
		TvInsertItem.item.pszText = pStrText;
		//TVI_LAST:insert the item at the end of list
		//TVI_SORT 
		return (HTREEITEM)SendMessage(GetDlgItem(hDlg, IDC_TREE1), TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem));;
	}
}

HTREEITEM MY_INSERT_ITEM_CREATEWND(HWND hTree, HTREEITEM hParent, char* pStrText)
{
	if (NULL != pStrText)
	{
		TVINSERTSTRUCT TvInsertItem;
		TvInsertItem.hParent = hParent;
		TvInsertItem.hInsertAfter = TVI_LAST;
		TvInsertItem.item.mask = TVIF_TEXT;
		TvInsertItem.item.pszText = pStrText;
		//TVI_LAST:insert the item at the end of list
		//TVI_SORT 
		return (HTREEITEM)SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem));;
	}
}
  • 效果图

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值