如何创建树视图控件
要创建树视图控件,请使用 CreateWindowEx 函数,并为窗口类指定 WC_TREEVIEW 值。 在加载公共控件 DLL 时,树视图窗口类会在应用程序的地址空间中注册。 要确保 DLL 已加载,请使用 InitCommonControls 函数。
需要了解的事项
技术
先决条件
- C/C++
- Windows 用户界面编程
说明
创建树视图控件的实例
以下示例创建一个树视图控件,该控件的大小适合父窗口的工作区。 它还使用应用程序定义的函数将图像列表与控件相关联,并将项添加到控件。
// Create a tree-view control.
// Returns the handle to the new control if successful,
// or NULL otherwise.
// hwndParent - handle to the control's parent window.
// lpszFileName - name of the file to parse for tree-view items.
// g_hInst - the global instance handle.
// ID_TREEVIEW - the resource ID of the control.
HWND CreateATreeView(HWND hwndParent)
{
RECT rcClient; // dimensions of client area
HWND hwndTV; // handle to tree-view control
// Ensure that the common control DLL is loaded.
InitCommonControls();
// Get the dimensions of the parent window's client area, and create
// the tree-view control.
GetClientRect(hwndParent, &rcClient);
hwndTV = CreateWindowEx(0,
WC_TREEVIEW,
TEXT("Tree View"),
WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES,
0,
0,
rcClient.right,
rcClient.bottom,
hwndParent,
(HMENU)ID_TREEVIEW,
g_hInst,
NULL);
// Initialize the image list, and add items to the control.
// InitTreeViewImageLists and InitTreeViewItems are application-
// defined functions, shown later.
if (!InitTreeViewImageLists(hwndTV) ||
!InitTreeViewItems(hwndTV))
{
DestroyWindow(hwndTV);
return FALSE;
}
return hwndTV;
}
注解
创建树视图控件时,还可以向其发送 WM_SETFONT 消息以设置文本要使用的字体。 在插入任何项之前,应发送此消息。 默认情况下,树视图使用图标标题字体。 尽管可以使用自定义绘图自定义每项的字体,但树视图控件使用由 WM_SETFONT 消息指定的字体尺寸来确定间距和布局。
相关主题