Step1:创建对话框程序,在stdafx.h中增加头文件afxdb.h(因为需要数据库相关类),然后添加一个新类CUserSet,派生自CRecordset,数据源根据实际数据库配置
Step2:对话框头文件CMyDialog.h中添加成员变量theUserSet(注意:要添加UserSet.h),并添加成员变量CImageList类型成员变量m_ImageList.
Step3:对话框面板中拖入一个树形控件,并绑定类变量m_Tree;同时拖入一个Button按钮,响应函数为OnQuery()
step4:在资源面板中插入三个ICON资源,自行绘制。
Step5:在BOOL CMyDialog::OnInitDialog()中输入代码:
m_ImageList.Create(16,16,ILC_COLOR24 | ILC_MASK,0,1);//创建 m_ImageList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON1)));//添加图标资源 m_ImageList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON2))); m_ImageList.Add(LoadIcon(AfxGetResourceHandle(),MAKEINTRESOURCE(IDI_ICON3))); m_Tree.SetImageList(&m_ImageList,TVSIL_NORMAL);//绑定树控件
Step6:在Button按钮的响应函数(OnQuery())中添加代码
void CMyDlg::OnQuery() { if(theUserSet.IsOpen()) { theUserSet.Close(); } theUserSet.Open(); CString userTreeHeader[3]={"医生","售药员","管理员"}; HTREEITEM userItem[3]; for(int i=0;i<3;i++) { //三个用户根结点; userItem[i]=m_Tree.InsertItem(userTreeHeader[i],0,0,TVI_ROOT); m_Tree.SetItemData(userItem[i],(DWORD)i); } HTREEITEM thePoint; for(i=0;i<3;i++) { theUserSet.m_strFilter="UserClassification='"+userTreeHeader[i]+"'"; theUserSet.Requery(); for(int j=0;;j++) { if(theUserSet.IsEOF()) { break; } if(theUserSet.m_UserAccount=="gm") //当前用户 { HTREEITEM userSpecifics=m_Tree.InsertItem(theUserSet.m_UserAccount+"(当前用户)",2,2,userItem[i]); m_Tree.SetItemData(userSpecifics,(DWORD)j); thePoint=userSpecifics;//标识点 } else { HTREEITEM userSpecifics=m_Tree.InsertItem(theUserSet.m_UserAccount,1,1,userItem[i]); m_Tree.SetItemData(userSpecifics,(DWORD)j); } theUserSet.MoveNext(); } } m_Tree.Select(thePoint,TVGN_CARET); }