83上位机VC MFC树视图复选框功能添加
83上位机VC MFC树视图复选框功能添加
功能展示
在树状视图的节点前面添加复选框的话,可以很方便用户对相关节点的检索操作,如何在树状视图上添加复选框功能?当前例程实现了这一功能,凡是用户选中的的树状视图上的节点,都可以被添加到右侧的编辑框中,效果如图 要点提示 树状视图控件有个CHECKBOXES属性,可以实现在树状节点前复选框,然后使用GetCheck()函数获取选中的状态; 实现功能 1.新建基于对话框的应用程序 2.添加两变量 CImageList m_TreeImageList; CStringm_sTex;//保存树状各节点文本信息;拖拽树状视图控件并关联变量CTreeCtrl m_Tree;添加rich编辑框控件,关联变量CRichEditCtrl m_Edit;;添加一按钮控件关联函数OnAdd() ;再添加两函数 void AddtoTree(HTREEITEM node,int UPID);//自定义函数 void AddFromTree(HTREEITEM m_node);//向编辑框添加节点数据
三个函数体为 - void CGkbc8Dlg::OnAdd()
- {
- HTREEITEM item;
- item = m_Tree.GetRootItem();
- m_sTex="";
- if(m_Tree.GetCheck(item)!=0)
- {
- m_sTex+=m_Tree.GetItemText(item);
- m_sTex+="\n";
- m_Edit.SetWindowText(m_sTex);
- }
- AddFromTree(item);
- }
复制代码- void CGkbc8Dlg::AddFromTree(HTREEITEM node)
- {
- HTREEITEM child;
- node = m_Tree.GetChildItem(node);
- if (node != NULL)
- {
- while (node!= NULL)
- {
- if(m_Tree.GetCheck(node)!=0)
- {
- m_sTex+=m_Tree.GetItemText(node); m_sTex+="\n"; m_Edit.SetWindowText(m_sTex);
- }
- child = node; AddFromTree(child); node = m_Tree.GetNextItem(node,TVGN_NEXT);
- }
- }
- }
- void CGkbc8Dlg::AddtoTree(HTREEITEM node,int UPID)
- {
- HTREEITEM CHILDROOT;
- for(int i=0;i<a_upid.GetSize();i++)
- {
- if(UPID==atoi(a_upid.GetAt(i)))
- {
- switch(atoi(a_lx.GetAt(i)))
- {
- case 1:
- CHILDROOT = m_Tree.InsertItem(a_name.GetAt(i),2,2,node);
- AddtoTree(CHILDROOT,atoi(a_id.GetAt(i))); break;
- case 2:
- CHILDROOT = m_Tree.InsertItem(a_name.GetAt(i),1,1,node);
- AddtoTree(CHILDROOT,atoi(a_id.GetAt(i))); break;
- case 3:
- CHILDROOT = m_Tree.InsertItem(a_name.GetAt(i),3,3,node);
- AddtoTree(CHILDROOT,atoi(a_id.GetAt(i))); break;
- }
- }
- }
- }
复制代码- 最后是初始化,从数据库加载数据
- m_TreeImageList.Create(16,16,ILC_MASK,4,1);
- m_TreeImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
- m_TreeImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON2));
- m_TreeImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON3));
- m_Tree.SetImageList(&m_TreeImageList,LVSIL_NORMAL);
- HTREEITEM ROOT;
- ROOT=m_Tree.InsertItem("工控编程",0,0);
- //初始化数据库
- _ConnectionPtr m_pConnection;//Connection对象的指针
- _RecordsetPtr m_pRecordset;//Recordset对象的指针
- ::CoInitialize(NULL);//初始化OLE/COM库环境
- m_pConnection.CreateInstance("ADODB.Connection");//创建connection对象
- _bstr_t strConnect="uid=;pwd=;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=database.mdb;";//设置连接字符串
- m_pConnection->Open(strConnect,"","",adModeUnknown);//SERVER和UID,PWD的设置根据实际情况来设置
- _bstr_t vSQL;
- vSQL="SELECT*FROM fuxan ORDER BY id";//设置SQL语句
- m_pRecordset.CreateInstance(__uuidof(Recordset));//创建记录集对象
- m_pRecordset->Open(vSQL,m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);//取得表中的记录
- //加载数据
- a_id.RemoveAll();
- a_upid.RemoveAll();
- a_lx.RemoveAll();
- a_name.RemoveAll();
- while(m_pRecordset->adoEOF==0)
- {
- a_id.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("id"));
- a_upid.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("upid"));
- a_lx.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("lx"));
- a_name.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("name"));
- m_pRecordset->MoveNext();
- }
- m_pRecordset->Close();
- m_pConnection->Close(); //将从数据库加载的数据添加到树状列表中
- AddtoTree(ROOT,0);
- m_Tree.Expand(ROOT,TVE_EXPAND);//展开
复制代码
数据库的操作得动态调用动态数据数据库文件#import "C:\Program Files\CommonFiles\System\ado\msado15.dll" no_namespacerename("EOF","adoEOF")rename("BOF","adoBOF") 另外CStringArraya_id; CStringArray a_upid; CStringArray a_lx; CStringArray a_name;为全局变量;
图标得自己添加几个 rich edit编辑框的使用还得调用 AfxInitRichEdit();进行初始化操作;
我们来演示下功能实现过程
|