Working with Tree View Control


Creating Tree View Control

#include <afxwin.h>

#include<afxcmn.h>

class myframe:public CFrameWnd

{

private:

CTreeCtrl tree;

public:

myframe()

{

Create(0,”Tree view Control”);

}

int OnCreate(LPCREATESTRUCT I)

{

HTREEITEM lang,opersys,c,cpp,java;

CFrameWnd::OnCreate(I);

tree.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|TVS_HASLINES

|TVS_LINESATROOT |TVS_HASBUTTONS|TVS_SHOWSELALWAYS,

CRect(30,30,300,350),this,1);

lang=tree.InsertItem(“Computer Languages”,TVI_ROOT,TVI_SORT);

c=tree.InsertItem(“C”,lang,TVI_SORT);

tree.InsertItem(“TC”,c);

tree.InsertItem(“QC”,c);

tree.InsertItem(“MSC”,c);

cpp=tree.InsertItem(“C++”,lang,TVI_SORT);

tree.InsertItem(“VC++”,cpp);

tree.InsertItem(“BORLAND C++”,cpp);

java=tree.InsertItem(“Java”,lang,TVI_SORT);

tree.InsertItem(“VJ++”,java);

tree.InsertItem(“Symantec Cafe”,java);

tree.InsertItem(“Sun JDK”,java);

opersys=tree.InsertItem(“OPerating System”,TVI_ROOT,TVI_SORT);

tree.InsertItem(“Win95”,opersys);

tree.InsertItem(“Win98”,opersys);

tree.InsertItem(“WinXp”,opersys);

tree.InsertItem(“Vista”,opersys);

tree.InsertItem(“Win7.0″,opersys);

return 0;

}

int OnNotify(WPARAM w, LPARAM I, LRESULT *r)

{

HTREEITEM h;

CString str;

CWnd ::OnNotify(w,I,r);

NM_TREEVIEW*p=(NM_TREEVIEW*)I;

if(p->hdr.code==TVN_SELCHANGED)

{

h=tree.GetSelectedItem();

str=tree.GetItemText(h);

MessageBox(str,”You have Selected…”);

}

return 1;

}

DECLARE_MESSAGE_MAP()

};

BEGIN_MESSAGE_MAP(myframe,CFrameWnd)

ON_WM_CREATE()

END_MESSAGE_MAP()

class myapp: public CWinApp

{  public:

int InitInstance()

{          myframe *p;

p = new myframe;

p->ShowWindow(3);

m_pMainWnd=p;

return 1;

}

};

myapp a;

Advertisements