Karim H
Karim H

Reputation: 1631

How could i create a TreeView like Microsoft apps

Hi im training to develop an app but i want to add a tree with a specific appearance i succeed in doing that but the appearance of what i have is not what my clients want the app is developed using MFC and C++.

My app looks like this:

enter image description here

I want it to look like this:

enter image description here

enter image description here

enter image description here

The code I'm using:

CRect rectDummy(0,0,200,600);
//rectDummy.SetRectEmpty();

CTreeCtrl m_wndClassView;

const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

if (!m_wndClassView.Create(dwViewStyle, rectDummy, &m_wndView, 2))
{
    TRACE0("Failed to create Class View\n");
    return -1;      // fail to create
}


HTREEITEM hRoot = m_wndClassView.InsertItem(_T("System Summary"), 0, 0);
m_wndClassView.SetItemState(hRoot, TVIS_BOLD, TVIS_BOLD);

HTREEITEM hClass = m_wndClassView.InsertItem(_T("Hardwre Resources"), 1, 1, hRoot);
hClass = m_wndClassView.InsertItem(_T("Components"), 1, 1, hRoot);
hClass = m_wndClassView.InsertItem(_T("Software Enviroment"), 1, 1, hRoot);

I prefer the solution to be in MFC/C++ if possible and not using a third party libraries.

Upvotes: 3

Views: 535

Answers (2)

user1593881
user1593881

Reputation:

Add dependency to the uxtheme.lib library, include the <Uxtheme.h> header, call the SetWindowTheme() function after you've created the control:

SetWindowTheme(hYourTreeviewHandle, L"Explorer", NULL);

which will give your treeview control a Windows Explorer look and feel. Things are much more involved as you need to supply your own image list and also utilize the TreeView_SetImageList() macro to attach the image lists to the treeview:

TreeView_SetImageList(hYourTreeviewHandle, hYourImagesHandle, TVSIL_NORMAL);

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 613202

You need a couple of things to get this look:

  1. Make sure that your application manifest specifies comctl32 v6.
  2. Add a call to SetWindowTheme(hTreeView, L"explorer", NULL) after you create the tree view. Here hTreeView is the window handle of the tree view.

Upvotes: 6

Related Questions