欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Win32 树视图控件的基本使用

程序员文章站 2024-03-01 12:21:16
...

首先参阅

    https://blog.csdn.net/bcbobo21cn/article/details/106287676

认识Windows公共控件库的使用;

 

一 创建树视图

首先用如下代码看一下,创建树视图控件是否成功返回句柄;

hTree = CreateWindow("SysTreeView32", NULL, 
 WS_CHILD | WS_BORDER |WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
 10, 20, 200, 400, hwnd, NULL, NULL, NULL);
wsprintf(szBuffer, "0x%X",hTree);
MessageBox(NULL,szBuffer,TEXT("树视图句柄"),0); 

Win32 树视图控件的基本使用

Win32 树视图控件的基本使用

创建成功;但此时树视图是空的;只有一个框;

二 添加第一个结点

代码;

            TvInsertItem.hParent = NULL;
            TvInsertItem.hInsertAfter = TVI_LAST;
            TvInsertItem.item.mask = TVIF_TEXT;
            TvInsertItem.item.pszText = pStrText;        
            //TVI_LAST:insert the item at the end of list
            hitema=SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem));;
  添加结点的操作为:

    定义  TVINSERTSTRUCT 结构体对象TvInsertItem,此结构体代表一个结点;

    为TvInsertItem赋值,.hParent是此结点的父,根结点填为NULL;

    向树视图发送消息,第一个参数是树视图句柄,第四个参数是TvInsertItem结构体地址并转为LPARAM类型;

    创建成功返回的是此次创建的结点的句柄,为 HTREEITEM 类型;

效果如下;

Win32 树视图控件的基本使用

工程;

Win32 树视图控件的基本使用

Win32 树视图控件的基本使用

三 添加更多结点

创建子结点,.hParent成员填为父结点句柄;如果再创建根结点,.hParent填为NULL;

效果如下;

Win32 树视图控件的基本使用

码:

#include <windows.h>
#include <commctrl.h>
#include "resource.h"
 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

HINSTANCE hInst;
TCHAR szClassName[] = TEXT("win32TreeDemo");
HWND hTree;

int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
 
	hInst = hThisInstance;
	
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE (IDC_WIN32TREEDEMO);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    if (!RegisterClassEx (&wincl))
        return 0;
 
    hwnd = CreateWindowEx (
           0,
           szClassName,
           TEXT("win32TreeDemo"),
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           300,
           500,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );
 
    ShowWindow (hwnd, nFunsterStil);
 
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
 
    return messages.wParam;
}
 
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rt;
	int ret;
	char szBuffer[100];
	char* pStrText="根结点AAAA";
	char* pStrText2="子结点AABB";
	char* pStrText3="子结点AACC";
	char* pStrText4="根结点BBBB";
	TVINSERTSTRUCT TvInsertItem, TvInsertItem2, TvInsertItem3, TvInsertItem4;
	HTREEITEM hitema, hitemb, hitemab, hitemac;
				
    switch (message)
    {
			case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{

		    case IDM_ABOUT:
				MessageBox (hwnd, TEXT ("win32TreeDemo v1.0\nCopyright (C) 2020\n by bo"),
                        TEXT ("win32TreeDemo"), MB_OK | MB_ICONINFORMATION);
				break;
			case IDM_EXIT:
				DestroyWindow(hwnd);
				break;
			default:
				return DefWindowProc(hwnd, message, wParam, lParam);	    		
		    }
    		break;
  		case WM_CREATE:
  			hTree = CreateWindow("SysTreeView32", NULL, 
 WS_CHILD | WS_BORDER |WS_VISIBLE | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
 10, 20, 200, 400, hwnd, NULL, NULL, NULL);
 			//wsprintf(szBuffer, "0x%X",hTree);
    		//MessageBox(NULL,szBuffer,TEXT("树视图句柄"),0);        		
			TvInsertItem.hParent = NULL;
			TvInsertItem.hInsertAfter = TVI_LAST;
			TvInsertItem.item.mask = TVIF_TEXT;
			TvInsertItem.item.pszText = pStrText;		
			//TVI_LAST:insert the item at the end of list
			hitema=SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem));;
			
			TvInsertItem2.hParent = hitema;
			TvInsertItem2.hInsertAfter = TVI_LAST;
			TvInsertItem2.item.mask = TVIF_TEXT;
			TvInsertItem2.item.pszText = pStrText2;		
			hitemab=SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem2));;
			
			TvInsertItem3.hParent = hitema;
			TvInsertItem3.hInsertAfter = TVI_LAST;
			TvInsertItem3.item.mask = TVIF_TEXT;
			TvInsertItem3.item.pszText = pStrText3;		
			hitemac=SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem3));;
			
			TvInsertItem4.hParent = NULL;
			TvInsertItem4.hInsertAfter = TVI_LAST;
			TvInsertItem4.item.mask = TVIF_TEXT;
			TvInsertItem4.item.pszText = pStrText4;		
			hitemb=SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)(&TvInsertItem4));;
			
  			break;
    	case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);					
			GetClientRect(hwnd, &rt);			
			
			EndPaint(hwnd, &ps);
			break;
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}

资源文件,头文件;

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

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_WIN32TREEDEMO MENU 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_WIN32TREEDEMO		10101
#define 	IDD_ABOUTBOX	10102