MFC - Lession3: Tree Control and List Control

Video MFC - Lession3: Tree Control and List Control

Tree controls and list controls are two commonly used controls in the problem of displaying data, retrieve, manage resources, directories, ...

In this article I will demonstrate how to use a combination of two controls to display the data.

Download sourcecode (316KB)

MFC - Lession3: Tree Control and List Control

TreeListDemo

Content related to the following major components:

- Import project resource

- Add and archive icon with ImageList

- Add and display data on the Tree control and control list

- Handling events on selected items Tree Control and List Control

Interface Design

Create a new project with the name TreeListDemo and design as follows:

MFC - Lession3: Tree Control and List Control

Object Properties Value Variable
Tree Control ID 

Styles> Has buttons

Styles> Has lines

Styles> Lines add root

IDC_TREE1 

Checked

Checked

Checked

List Control ID 

Styles> View

IDC_LIST1 

List

CListCtrl m_cListCtrl
Static Text ID IDC_STATIC_LIST1

When checked the items in the Tree Styles control, you want to look for a change of control type display this on the form. The selection of these values ​​can only aim to help more intuitive tree view.

In this article we will use two ways to work with the control. With the tree control will use GetDltItem function (), while the control list we use ClassWizard to generate control variables to work.

Use CImageList

I will create an object CImageList contains icons to show up in the tree control and position control list.

- First you add the icon to the project by right-clicking on the item icon in ResourceView and select Insert (or press Ctrl + R). In the Insert Resource dialog box opens to select the icon and click Import . You select the 6 icon to import into.

- After the insert, the icon will be identified automatically as IDI_ICON1, IDI_ICON2, IDI_ICON3 ... You can click on each icon and select Properties to change the name if you wish.

- Add layers CTreeListDemoDlg private variable of type CImageList and named m_imageList

- Add layers CTreeListDemoDlg private function with return type void with name InitImageList and write code for the following functions:

void CTreeListDemoDlg::InitImageList()

{

// Tao mang icon

HICON hIcon[6];

 

m_imageList.Create(16, 16, ILC_MASK, 6, 6);

CWinApp* pApp=(CWinApp*)AfxGetApp();

 

hIcon[0] = pApp->LoadIcon(IDI_ICON1);

hIcon[1] = pApp->LoadIcon(IDI_ICON2);

hIcon[2] = pApp->LoadIcon(IDI_ICON3);

hIcon[3] = pApp->LoadIcon(IDI_ICON4);

hIcon[4] = pApp->LoadIcon(IDI_ICON5);

hIcon[5] = pApp->LoadIcon(IDI_ICON6);

 

// Them cac icon vao m_imageList

for (int i = 0; i < 6; i++) {

m_imageList.Add(hIcon[i]);

}

&nbsp;

// Gan m_imageList cho list control

m_cListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);

}

To explain:

- First we have to create an object image by jaw CImageList list :: Create (), this function is defined as follows:

BOOL Create(
int cx,
int cy,
UINT nFlags,
int nInitial,
int nGrow
);

- Cx and cy of which is the size of the icons, put the two values ​​are 16 to create small icons. The parameter indicates the type of image nFlag list creates. I used to value ILC_MASK icon appears no black background. NInitial attributes will determine the number of elements added and the number of elements nGrow that list can change image size to accommodate more. Because we only add the icon to the image list only once, so both will share the set value is 6, which is the icon that we have imported at the beginning.

- To retrieve the handle of the icon in the resource, we first used AfxGetApp () to retrieve the pointer of the current CWinApp object, and use LoadIcon function () of objects to create the array elements hIcon.

- The function CWinApp :: LoadIcon () This request parameter is the ID of the object icon in the resource and returns an HICON.

- After adding the icon to the image list through the loop, you must assign items to list m_imaegList control. For I have created a variable in the design m_cListCtrl should just call SetImageList () to do this.

- Since our initial set attributes Styles> View the list control that list , that is only displaying small icons in the form of SetImageList 2nd parameter () is LVSIL_SMALL, if you want a larger icons replace parameters by LVSIL_NORMAL .

Add node in Tree control

Objects tree node contains controls for different levels, each node is an object of type HTREEITEM. To add a new node to the tree control, you need to call :: InsertItem CTreeCtrl () , it has a few overloads, in this article we will use two versions of the following:

HTREEITEM InsertItem(
LPCTSTR lpszItem,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST
);
HTREEITEM InsertItem(
LPCTSTR lpszItem,
int nImage,
int nSelectedImage,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST
);

Here you have a data type with LPCTSTR name is quite confusing, this is simply a constant pointer to support Unicode strings, content will display on the tree control node. Two last parameter (hParent and hInsertAfter) of the two versions above are optional because they are set by default value, ie you can skip without passing parameters to it.

Parameters hParent parent node only going to add to (the default is TVI_ROOT node that is added to the base of the tree) and hInsertAfter only precede node node will soon be added (defaults TVI_LAST node that is added after the final) .

Parameters nImage index indicates the image icon in the list will be displayed next to the node, and nSelectedImage is the index of the icon in the image list that appears when you select the node (like Windows Explorer when you select a folder in the folder tree folder icon will move from folder to folder shut MOS).

I started adding the node into the tree the following steps:

- Add a function in class CTreeListDemoDlg private functions have type void and called InitTreeControl .

- Write the code for the function InitTreeControl:

void CTreeListDemoDlg::InitTreeCtrl()

{

CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE1);

// Gan image list

pTree->SetImageList(&m_imageList, TVSIL_NORMAL);

HTREEITEM hTree, hCompany;

// index cua icon trong image list

int nSelImage=5;

// root

hTree = pTree->InsertItem("Books",1,nSelImage, TVI_ROOT);

hCompany = pTree->InsertItem("MS Office",2,nSelImage, hTree);

pTree->InsertItem("MS Word", hCompany);

pTree->InsertItem("MS Excel", hCompany);

hCompany = pTree->InsertItem("Programming",3,nSelImage, hTree);

pTree->InsertItem("C#", hCompany);

pTree->InsertItem("C++", hCompany);

pTree->InsertItem("Java", hCompany);

hCompany = pTree->InsertItem("Design",4,nSelImage, hTree);

pTree->InsertItem("CorelDRAW", hCompany);

pTree->InsertItem("Photoshop", hCompany);

// Mo rong node root
pTree->Expand(hTree,TVE_EXPAND);
}

I wrote two functions InitImageList and InitTreeControl to add data to the image list and tree controls, for these functions to be executed, I will call them in the jaw CTreeListDemoDlg :: OnInitDialog ().

BOOL CTreeListDemoDlg::OnInitDialog()
{
[…]
// TODO: Add extra initialization here
InitImageList();
InitTreeCtrl();
return TRUE;  // return TRUE  unless you set the focus to a control
}

Three hours you can run to try and see the results displayed on the tree control.

Links Tree Control and List Control

The purpose of this section is processed to when selecting a node on the tree control, the child nodes of the selected node will be displayed on the control list. You need to add an event by using ClassWizard TVN_SELCHANGED with function names are OnSelchangedTree1 :

void CTreeListDemoDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)

{

NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

// TODO: Add your control notification handler code here

CTreeCtrl* pTree = (CTreeCtrl*) GetDlgItem(IDC_TREE1);

m_cListCtrl.DeleteAllItems();

// node duoc chon tren tree control

HTREEITEM hSelected = pNMTreeView->itemNew.hItem;

if (hSelected != NULL) {

// Kiem tra node dang chon co node con khong

if (pTree->ItemHasChildren(hSelected))

{

HTREEITEM hNextItem;

// Lay node con dau tien cua node dang chon

HTREEITEM hChildItem = pTree->GetChildItem(hSelected);

int count=0;

while (hChildItem != NULL)

{

// Lay text cua node con va insert vao list control

CString strItemText = pTree->GetItemText(hChildItem);

m_cListCtrl.InsertItem(count++,strItemText,0);

// Lay node tiep theo ben duoi hChildItem

hNextItem = pTree->GetNextItem(hChildItem, TVGN_NEXT);

// Cap nhat vi tri cua hChildItem sang node ke tiep

hChildItem = hNextItem;

}

}

}

*pResult = 0;

}

NM_TREEVIEW pNMTreeView a struct type containing itemNew the new node is selected in the tree control.

Read the above code you can imagine what parts methodology. To iterate over all the nodes you must use GetNextItem function () with the second parameter to retrieve the next node TVGN_NEXT same level with node transmissions in (hChildItem). If you do not find any node, this function will return NULL.

Please note insert commands into the control list:

m_cListCtrl.InsertItem (count ++, strItemText, 0);

The first parameter is the position you want to insert, the second parameter is the text display and the last parameter is the index of the icon in the image list. Here I am to be 0 is equivalent to you omit this parameter, which is the default list control will take the first icon to display. I set to 0 so you can change where necessary.

Please test and a few nodes on the tree control, to watch list can control exactly what you predict not.

Events selected item in the list control

This section will make the displayed item is selected in the list on the static text control below. You use ClassWizard to add events LVN_ITEMCHANGED with OnItemChangedList1 name:

void CTreeListDemoDlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
int nSelected = pNMListView->iItem;
CString strItem = m_cListCtrl.GetItemText(nSelected,0);
SetDlgItemText(IDC_STATIC_LIST1, strItem);
*pResult = 0;
}

GetItemText function () of CListCtrl receive 2 parameters: one is the index of the item to retrieve text and two are positioned subitem of that item. If you switch display type of control to Report list, you can see that each item of the list controls have multiple columns, and each column divided many subitem items. Here we set the second parameter is 0 ie retrieve the item text.

In this article I told you that although we created variables to use mechanisms like DDX / DDV for the control list but did not see the use UpdateData function () to link data. The reason is that the variable is not the type of value m_cListCtrl which is type control, therefore the call UpdateData () does not alter its value. M_cListCtrl variable is simply a variable that represents the object for the purpose of calling CListCtrl its functions to manipulate the control.

Epilogue

In the framework of the article I recommend using only the most basic controls Tree and List controls, there are many different functions and options when working with these two controls. You can easily self-study through the MSDN library complete with instructions and definitions.

https://yinyangit.wordpress.com

Instructions for teleportation in Mini World: Block Art

Instructions for teleportation in Mini World: Block Art

In the latest Mini World: Block Art update, players can use the new feature - The Transfer Core Block to teleport instantly. Thanks to it, you can reach anywhere in Mini World in a snap.

How to rotate the screen playing PUBG Mobile on GameLoop

How to rotate the screen playing PUBG Mobile on GameLoop

If youre playing PUBG Mobile on GameLoop, you can return to your gaming screen easily without the need for software.

How to build a squad for ranked FIFA Online 4

How to build a squad for ranked FIFA Online 4

The fact that FIFA Online 4 has restricted the squad by Luong has caused the coaches to arrange and change their tactics a lot in FO4 compared to FO3. So the question for gamers now is how to arrange a strong squad but suitable for Lu conditions.

Top best strikers in FIFA Online 4

Top best strikers in FIFA Online 4

When it comes to the worlds top strikers we cannot help but mention names like Ronaldo, Messi or Pele. Below is a list of the players who deserve to stand in the ranks of the best strikers in the world in FIFA Online 4.

How to top up Zing card, buy Zing card Online

How to top up Zing card, buy Zing card Online

Buying Zing cards and loading Zing Online cards is essential for those who are playing VNG games such as Thien Dia Hoi, MU or Vo Lam Truyen Ky ...

Summary of recipes in Mini World: Block Art

Summary of recipes in Mini World: Block Art

Guide recipes in Mini World: Block Art and the use of each dish.

How to mod weapons and items in Mini World: Block Art

How to mod weapons and items in Mini World: Block Art

In Mini World: Block Art, we have all the necessary items to survive as well as create, or simply jubilant in the game just for entertainment, but if you want to change it for less boring bored, Mini World: Block Art has equipped it

Replace Garena accounts avatar

Replace Garena accounts avatar

Changing Garena avatar is the only way for those who are playing games with Garena account can change avatars in their games. Such as Lien Lien Mobile, Operation Legend ...

Mini World: Block Art - Great survival tips you probably didnt know

Mini World: Block Art - Great survival tips you probably didnt know

Here are some tips in Mini World to help you get more out of resources faster

Some Map Code in Mini World: Block Art and how to enter the code

Some Map Code in Mini World: Block Art and how to enter the code

Entering the map code in Mini World: Block Art will help you easily find the type of terrain you want

Change skin, change the appearance for characters in Mini World: Block Art

Change skin, change the appearance for characters in Mini World: Block Art

You can change the costume color yourself according to the models already available in Mini World: Block Art, or buy some of the Mini World costumes to change the appearance of the character.

How to link an international Mobile Legends account to VNG

How to link an international Mobile Legends account to VNG

On November 20, VNG officially released Mobile Legends under the name Mobile Legends: Bang Bang VNG. To continue playing the game, the player must make an international account link with VNG.

How to change the name in the game Asphalt 9: Legends

How to change the name in the game Asphalt 9: Legends

Changing the name in the racing game Asphalt 9: Legends is probably not too important but on the contrary, there are also people who do not like to default the name of the game, so if you are one of them and you need to change the name in Asphalt 9 then follow the sa tutorial

How to build hell portal and find the Black Dragon Boss in Mini World: Block Art

How to build hell portal and find the Black Dragon Boss in Mini World: Block Art

In this is how to build hell gates in Mini World: Block Art, the gates of hell will take you to the Underworld and there will be the Black Dragon Boss waiting for you.

Mobile Legends: How is Bang Bang VNG different from the international version?

Mobile Legends: How is Bang Bang VNG different from the international version?

For those who love the MOBA game - Mobile Legends: Bang Bang, the information about the appearance of the Mobile Legends: Bang Bang Vietnamese game version is not too new.

Instructions on how to change and reset the level 2 password in game FIFA Online 4

Instructions on how to change and reset the level 2 password in game FIFA Online 4

Our instructions below will help you change your level 2 password and conduct a level 2 password reset on FIFA Online 4 game quickly and easily.

How to create a room to play with friends in Mini World: Block Art

How to create a room to play with friends in Mini World: Block Art

With many fun and attractive game modes, you can invite your team to struggle in Mini World: Block Art or join the available game styles on the list to play, if you do not know how to create a room to play with Friends in Mini World, you can t

How to tame the mounts in Mini World: Block Art

How to tame the mounts in Mini World: Block Art

With these ways of taming the mounts in this Mini World Block Art, you will move more quickly and be able to breed them to tame them.

During COVID, Stay Home and Try Online Casinos - Sol Casino Canada

During COVID, Stay Home and Try Online Casinos - Sol Casino Canada

During COVID, almost all people are spending their whole time at home due to lockdowns. It is not easy to adjust to this lifestyle due to the new pandemic. Therefore, most people are looking for solutions to keep themselves busy.

How to delete a Mini World account: Block Art

How to delete a Mini World account: Block Art

How to delete the account Mini World: Block Art will erase all the installation data and character data in the game.