MFC之Tree Control控制項

2021-08-21 19:28:35 字數 3981 閱讀 7302

tree control顧名思義,樣子自然是長的像樹了,非常適合用來顯示有樹形結構的資訊.

大部分時候我們都是用靜態方式建立控制項,因為這最簡單.直接從toolbox中拖乙個控制項放到dialog中就行了.然後要以在properties頁面設定一些屬性.如果要在**中對控制項進行操作,可以這樣得到控制項指標,getdlgitem(ctrl_id) .如果想更靈活點可以給控制項繫結乙個變數.

ctreectrl m_treeshow;

ddx_control(pdx, idc_tree1, m_treeshow);

動態建立有兩個要注意的地方.

1.怎麼指定控制項位置和大小.通過crect rect(2,2, 4,4,)這樣直接的數字指定是很不靈活的,假如頁面上控制項一多,並且後面又想加入新控制項了,會非常混亂不好維護.

比較好的乙個方法是在頁面上弄乙個static text 或picture control,然後把它們的visiable設為false.調整好它們的位置和大小.然後獲取它們的crect作為動態建立控制項的crect.

2.怎麼指定控制項id.動態建立時需要顯式指定乙個id,我們知道id只是乙個數字.但是關鍵是確保同乙個頁面上的id不能相同.你當然可以簡單的定義乙個巨集.但這樣不太好.乙個比較簡單的方法是在string table中新增一行,用那個id.這裡不是真的需要string裡面的資訊,而只是這個id.當然使用規範還是要遵守,caption裡面也隨便填點東西別為空.

ctreectrl* m_ptree;

m_ptree = new ctreectrl;

crect rect;

getdlgitem(idc_space)->getwindowrect(&rect); //idc_spce 是乙個static text的id

screentoclient(rect);  //螢幕座標轉化成視窗座標,貌似不能像這樣

//getdlgitem(idc_space)->getclientrect(rect)直接獲取視窗座標的,這樣獲得到位置是視窗的原點位置.

m_ptree->create(ws_visible | ws_tabstop | ws_child | ws_border

| tvs_hasbuttons | tvs_linesatroot | tvs_haslines

| tvs_disabledragdrop | tvs_notooltips | tvs_editlabels

, rect, this, ids_tree);  // ids_tree是在string table中隨便指定的一id

htreeitem hroot;

hroot = m_ptree->insertitem(_t("china"),tvi_root); //插入乙個根節點

介面上多整幾個圖示會比較好看點.那怎麼往tree control中新增呢?

首先要是構造乙個類cimagelist,往裡面新增,常用的有ico, png, bmp這三種格式.新增它們的方式有點不同

cimagelist* pimglist;

pimglist = new cimagelist;

pimglist -> create(16,16, ilc_color32 | ilc_mask, 0, 1);

//ico

//bmp

cbitmap bmp;

bmp.loadbitmapw(idb_bitmap1);

pimglist->add(&bmp, rgb(0,0,0));

//png

cpngimage png;

png.load(idb_png1);

cbitmap pngbmp;

pngbmp.attach(png.detach());

pimglist->add(&pngbmp, rgb(0,0,0));

//將cimagelist與tree control繫結

m_ptree-> setimagelist(pimglist, tvsil_normal); //這樣一繫結,之後插入的節點如果不顯式指定圖示將預設使用cimagelist中第乙個圖示

htreeitem hroot;

hroot = m_ptree->insertitem( _t("china"),tvi_root);

m_ptree->insertitem( _t("china"), 1,1,hroot, tvi_last) ; //顯式指定pimglist中第二圖示

先所控制項繫結到如下變數

ctreectrl m_treeshow;

建立乙個以中國為根結點,然後一些省和城市為子結點的樹形結構資訊.

//設定樹形控制項樣式

m_treeshow.modifystyle(null,tvs_hasbuttons | tvs_linesatroot |tvs_haslines);

//htreeitem相當於是乙個結點的控制代碼

htreeitem hroot;

hreeitem hchild;

hroot = m_treeshow.insertitem( "中國", tvi_root); //插入根結點

hchild = m_treeshow.insertitem("湖南省",hroot);

hchild = m_treeshow.insertitem("長沙市",hchild);

hchild = m_treeshow.insertitem("岳麓區",hchild);

hchild = m_treeshow.insertitem("廣東省",hroot);

要刪除節點那肯定要想辦法怎麼找到節點才行.

htreeitem hitem, hselected;

hselected = m_treeshow.getselecteditem();

m_treeshow.deleteitem( hselected); //刪除選擇的節點,如果節點下面還有子節點也會被一起刪掉.

hitem = m_treeshow.getrootitem(); //整棵樹的根結點

hitem = m_treeshow.getchilditem(hselected); //獲得選擇節點的子節點

hitem = m_treeshow.getparentitem(hselected); //獲得選擇節點的子節點

hitem = m_treeshow.getprevsiblingitem(hselected); //獲得選擇節點的上一兄弟結點

hitem = m_treeshow.getnextsiblingitem(hselected); //獲得選擇節點的下一兄弟結點

m_treeshow.deleteallitems(); //刪除所有節點

預設情況下節點都是摺疊起來的.如果只展開某個結點下面一層的內容.

htreeitem hitem, hroot;

hroot = m_treeshow.getrootitem();

m_treeshow.expand ( hroot, tve_expand); //只展開根結點下面一層.

expandallnode( hroot , m_treeshow);

那如果要展開所選結點下面所有節點咋整呢

void expandallnode(htreeitem hitem, ctreectrl& m_treeshow){

htreeitem hchild = m_treeshow.getchilditem(hitem); //獲得第乙個子節點

while(hchild){

m_treeshow.expand(hitem,tve_expand); //展開當前節點

expandtreenode(hchild, m_treeshow); //遞迴展開第乙個子節點下所有節點

hchild = m_treeshow.getnextsiblingitem(hchild); //獲得第二個子節點

MFC中Tree Control的使用

一 範例 樹控制項使用 1 設定圖示 準備hicon圖示 hicon icons 4 icons icons icons icons cimagelist list 必須儲存住這個集合 寫到.h做成員屬性 建立集合 list.create 30,30,ilc color32,4,4 新增具體的 fo...

VC中Tree Control的使用

vc中tree control的使用 邵盛松 2010年11月29日星期一 一 tree control的初始化 htreeitem htreeitemparent null htreeitem htreeitemchild null cstring strparentdata l cstring ...

VC中Tree Control的使用

vc中tree control的使用 邵盛松 2010年11月29日星期一 一 tree control的初始化 htreeitem htreeitemparent null htreeitem htreeitemchild null cstring strparentdata l cstring ...