📄 treelist.cpp
字号:
// TreeList.cpp : 实现文件
//
#include "stdafx.h"
#include "ProjectManage.h"
#include "TreeList.h"
#include "ADODB.h"
// CTreeList 对话框
IMPLEMENT_DYNAMIC(CTreeList, CDialog)
CTreeList::CTreeList(CWnd* pParent /*=NULL*/)
: CDialog(CTreeList::IDD, pParent)
{
}
CTreeList::~CTreeList()
{
}
void CTreeList::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE1, m_oTreeclass);
}
BEGIN_MESSAGE_MAP(CTreeList, CDialog)
ON_WM_SIZE()
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, &CTreeList::OnTvnSelchangedTree)
END_MESSAGE_MAP()
// CTreeList 消息处理程序
void CTreeList::OnTvnSelchangedTree(NMHDR *pNMHDR, LRESULT *pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CString curtext;
HTREEITEM hCurItem;
hCurItem=m_oTreeclass.GetSelectedItem();
curtext=m_oTreeclass.GetItemText(hCurItem);
m_classname=curtext;
CString value,svalue;
this->m_Storage.OpenSql("RegionTree","mod_id,mod_parent","where mod_name='"+curtext+"'");
try
{
m_tempid=0;
if(!(this->m_Storage.IsEof()))
{
this->m_Storage.GetFieldValue(0,value);
this->m_Storage.GetFieldValue(1,svalue);
m_tempid=(long)((_variant_t)value);
Curpar=VariantToCString((_variant_t)svalue);
}
}
catch(_com_error e)
{
CString errormessage;
MessageBox("读取单选按钮出错!",m_classname);
}
UpdateData(false);
CProjectManageApp* app=(CProjectManageApp*)AfxGetApp();
CProjectManageView* pView=app->GetMyView();
if(m_tempid!=1 && value!="")
{
pView->LoadData(" where ProjectTab.mod_id="+value+"");
}
*pResult = 0;
}
void CTreeList::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
CWnd * pWnd=GetDlgItem(IDC_TREE1);
if(pWnd!=NULL&&IsWindow(pWnd->m_hWnd))
pWnd->MoveWindow(0,0,cx,cy);
}
BOOL CTreeList::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
AddTree();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CTreeList::AddTree()
{
TVINSERTSTRUCT tvInsert;
HTREEITEM hParent;
CString sroot;
tvInsert.hParent = NULL;
tvInsert.hInsertAfter = NULL;
tvInsert.item.mask = TVIF_TEXT;
this->m_Storage.OpenSql("RegionTree","mod_name","where mod_id=1");
try
{
this->m_Storage.MoveFirst();
CString value;
if(!this->m_Storage.IsEof())
{
this->m_Storage.GetFieldValue(0,value);
sroot=value;
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("读取类别树出错:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
if(sroot!="")
{
tvInsert.item.pszText = _T("项目列表");
hParent = m_oTreeclass.InsertItem(&tvInsert);
AddSubTree("0",hParent);
}
m_oTreeclass.Expand(hParent,TVE_EXPAND);
}
void CTreeList::AddSubTree(CString ParTree, HTREEITEM hPartItem)
{
HTREEITEM hCurrent;
CString sql;
CString curID;
_RecordsetPtr m_pTRecordset;
sql="SELECT * FROM RegionTree where mod_PARENT=";
sql=sql+ParTree+"";
try
{
m_pTRecordset.CreateInstance("ADODB.Recordset");
m_pTRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pDBCon->GetActiveConnection(),true),adOpenDynamic,adLockOptimistic,adCmdText);
m_pTRecordset->MoveFirst();
if(!m_pTRecordset->adoEOF)
{
while(!m_pTRecordset->adoEOF)
{
hCurrent = m_oTreeclass.InsertItem((LPCTSTR)(_bstr_t)(m_pTRecordset->GetCollect("mod_NAME")), hPartItem, NULL);
curID=VariantToCString(m_pTRecordset->GetCollect("mod_ID"));
if (TreeSumRecordCount(curID)>0)
AddSubTree(VariantToCString(m_pTRecordset->GetCollect("mod_ID")),hCurrent);
if (!m_pTRecordset->adoEOF)
m_pTRecordset->MoveNext();
}
}
m_pTRecordset->Close();
}
catch(_com_error e)
{
CString errormessage;
MessageBox("读取类别子树出错!",ParTree);
}
}
int CTreeList::TreeSumRecordCount(CString strFieldValue)
{
int Sum=0;
CString sql;
_RecordsetPtr m_pRecordset;
sql="SELECT * FROM RegionTree where mod_parent =";
sql=sql+strFieldValue+"";
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)theApp.m_pDBCon->GetActiveConnection(),true),adOpenDynamic,adLockPessimistic,adCmdText);
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
while(!m_pRecordset->adoEOF)
{
Sum+=1;
m_pRecordset->MoveNext();
}
}
m_pRecordset->Close();
}
catch(_com_error e)
{
CString stemp;
stemp.Format("计算类别总数出错:%s",e.ErrorMessage());
AfxMessageBox(stemp);
}
return Sum;
}
CString CTreeList::VariantToCString(VARIANT var)
{
CString strValue;
_variant_t var_t;
_bstr_t bst_t;
time_t cur_time;
CTime time_value;
COleCurrency var_currency;
switch(var.vt)
{
case VT_EMPTY:strValue=_T("");break;
case VT_UI1:strValue.Format ("%d",var.bVal);break;
case VT_I2:strValue.Format ("%d",var.iVal );break;
case VT_I4:strValue.Format ("%d",var.lVal);break;
case VT_R4:strValue.Format ("%f",var.fltVal);break;
case VT_R8:strValue.Format ("%f",var.dblVal);break;
case VT_CY:
var_currency=var;
strValue=var_currency.Format(0);
break;
case VT_BSTR:
var_t=var;
bst_t=var_t;
strValue.Format ("%s",(const char*)bst_t);
break;
case VT_NULL:strValue=_T("");break;
case VT_DATE:
cur_time=var.date;
time_value=cur_time;
strValue=time_value.Format("%A,%B%d,%Y");
break;
case VT_BOOL:strValue.Format ("%d",var.boolVal );break;
default:strValue=_T("");break;
}
return strValue;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -