2007年5月26日土曜日

Win32怎么做?

对Win32程序来说,插入树控件项也不难,只需要向树控件发送TVM_INSERTITEM消息即可。下面是用Win32写出的等价代码(为了简明起见,去掉了对控件项图像的支持)在主窗口过程中加入如下代码:

HWND hwndTree;

TVINSERTSTRUCT tvinsert;

switch (message)

{

case WM_CREATE:

{ hwndTree = CreateWindow(WC_TREEVIEW, _T("TreeView"),

WS_CHILD | WS_VISIBLE | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,

0,0,200,100, hWnd,(HMENU)1, hInst,NULL);

tvinsert.hParent = NULL;

tvinsert.hInsertAfter = TVI_LAST;

tvinsert.item.mask = TVIF_TEXT;

tvinsert.item.hItem = NULL;

tvinsert.item.state = 0;

tvinsert.item.stateMask = 0;

tvinsert.item.cchTextMax = 6;

tvinsert.item.cChildren = 0;

tvinsert.item.lParam = 0;

// top level

tvinsert.item.pszText = _T("Parent1");

HTREEITEM hDad = (HTREEITEM)SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

tvinsert.item.pszText = _T("Parent2");

HTREEITEM hMom = (HTREEITEM)SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);



// second level

tvinsert.hParent = hDad;

tvinsert.item.pszText = _T("Children1-1");

SendMessage(hwndTree,TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

tvinsert.item.pszText = _T("Children1-2");

SendMessage(hwndTree,TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);



// second level

tvinsert.hParent = hMom;

tvinsert.item.pszText = _T("Children2-1");



SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

tvinsert.item.pszText = _T("Children2-2");

SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

tvinsert.item.pszText = _T("Children2-3");

HTREEITEM hOther = (HTREEITEM)SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

// third level

tvinsert.hParent = hOther;

tvinsert.item.pszText = _T("Children2-3-1");



SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);

tvinsert.item.pszText = _T("Children2-3-2");

SendMessage(hwndTree, TVM_INSERTITEM , 0 ,(LPARAM)&tvinsert);



break;

}

case: WM_COMMAND:





break;

other case …

… …

return 0;

}

此外还要添加 InitCommonControls();函数,它的头文件为:commctrl.h,在工程设置链接选项卡中的“对象/库模块”中添加comctrl32.lib。这样便可顺利通过编译和链接。

0 件のコメント: