记录:树形控件的一些使用方法(和sql server 结合显示)

来源:互联网 发布:图图软件 编辑:程序博客网 时间:2024/06/14 18:21

void CTFromViewView::OnInitialUpdate()
{

//框架生成代码
CFormView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
//ua
m_tree.DeleteAllItems(); // Clear the tree item
//改变树形控件的样式
m_tree.ModifyStyle(0,TVS_HASLINES|TVS_HASBUTTONS|TVS_SHOWSELALWAYS|TVS_LINESATROOT);

TV_INSERTSTRUCT tvinsert;
//指定是根节点
tvinsert.hParent=NULL;
tvinsert.hInsertAfter=TVI_LAST;
tvinsert.item.mask=TVIF_TEXT;
tvinsert.item.pszText="郑州市";
//插入一个根节点
HTREEITEM hRoot=m_tree.InsertItem(&tvinsert);

//分别对路口,分组,车道信息指定那个啥
_RecordsetPtr pAcrossRs,pGroupRs,pDrivewayRs;
//临时变量
_variant_t vTemp;
TCHAR buffer[30];
//树节点
HTREEITEM hAcrossItem,hGroupItem,hDrivewayItem;

//少用了几个try块。直接 CreatInstance()
m_pCommand.CreateInstance(__uuidof(Command));
pAcrossRs.CreateInstance(__uuidof(Recordset));
pGroupRs.CreateInstance(__uuidof(Recordset));
pDrivewayRs.CreateInstance(__uuidof(Recordset));

//查询所有的路口
m_strSQL="SELECT DISTINCT acrossname FROM cnet_view_preview";
try
{
pAcrossRs->Open("SELECT DISTINCT acrossname FROM cnetstar_view_preview",
   _variant_t((IDispatch *)theApp.m_pConnection,true), // Get IDispatch pointer
   adOpenStatic,
   adLockOptimistic,
   adCmdText);
}
catch(_com_error e)
{
CString strErrorMsg;
strErrorMsg.Format("Error message:%s",e.ErrorMessage());
AfxMessageBox(strErrorMsg);
}
while(!pAcrossRs->adoEOF)
{
// 获取字段信息 :路口名称
//
vTemp=pAcrossRs->GetCollect("acrossname");
if(vTemp.vt!=VT_NULL)
   m_acrossName=(LPCTSTR)_bstr_t(vTemp);
tvinsert.hParent=hRoot;
tvinsert.hInsertAfter=TVI_LAST;

wsprintf(buffer,"%s",m_acrossName);
tvinsert.item.pszText=buffer;
//把路口信息插入树中
    hAcrossItem=m_tree.InsertItem(&tvinsert);
//根据路口信息 查询 分组信息 的 sql 语句
m_strSQL="SELECT DISTINCT groupname FROM cnetstar_view_preview WHERE acrossname='"+m_acrossName+"'";
pGroupRs->Open((_variant_t)m_strSQL,
   _variant_t((IDispatch*)theApp.m_pConnection,true),
   adOpenDynamic,
   adLockOptimistic,
   adCmdText);
while(!pGroupRs->adoEOF)
{
   //获取所有关联该路口 的 分组信息
   vTemp=pGroupRs->GetCollect("groupname");
   if(vTemp.vt!=VT_NULL)
    m_groupName=(LPCTSTR)_bstr_t(vTemp);
   tvinsert.hInsertAfter=TVI_LAST;
   tvinsert.hParent=hAcrossItem;
  
   wsprintf(buffer,"%s",m_groupName);
   tvinsert.item.pszText=buffer;
   //插入分组信息
   hGroupItem=m_tree.InsertItem(&tvinsert);
   //产生根据分组信息查询 该分组 车道信息 的查询语句(可扩充车道摄像机信息)
   m_strSQL="SELECT DISTINCT drivewayname FROM cnetstar_view_preview WHERE groupname='"+m_groupName+"'";
   pDrivewayRs->Open(_variant_t(m_strSQL),
    _variant_t((IDispatch*)theApp.m_pConnection,true),
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
   //为每个分组插入一个全景节点
   CString strPanarama(_T("全景摄像机"));
   tvinsert.hParent=hGroupItem;
  
   wsprintf(buffer,"%s",strPanarama);
   tvinsert.item.pszText=buffer;
   m_tree.InsertItem(&tvinsert);
   while(!pDrivewayRs->adoEOF)
   {
    //获取车道名称
    vTemp=pDrivewayRs->GetCollect("drivewayname");
    if(vTemp.vt!=VT_NULL)
     m_drivewayName=(LPCTSTR)_bstr_t(vTemp);
   
    wsprintf(buffer,"%s",m_drivewayName);
    tvinsert.item.pszText=buffer;
    tvinsert.hParent=hGroupItem;
    tvinsert.hInsertAfter=TVI_LAST;
    //插入车道信息
    hDrivewayItem=m_tree.InsertItem(&tvinsert);
    pDrivewayRs->MoveNext();
   }
   m_tree.Expand(hGroupItem,TVE_EXPAND);
   //关闭车道记录集
   pDrivewayRs->Close();
   pGroupRs->MoveNext();
}
m_tree.Expand(hAcrossItem,TVE_EXPAND);
//关闭分组记录集
pGroupRs->Close();
pAcrossRs->MoveNext();
}
//关闭路口记录集
pAcrossRs->Close();
//释放资源
pDrivewayRs=NULL;
pGroupRs=NULL;
pAcrossRs=NULL;

// 展开节点
m_tree.Expand(hRoot,TVE_EXPAND);

//在区域内显示全景 ---该为大恒卡显示全景 yyk 08/05/20
//ShowDSPPanorama(m_rectDsp);
ShowPanorama();
}

 

要用 ado 的方式连接 sql server ,那么步骤在第一篇里面有,只贴部分代码:

//ua
if(!AfxOleInit()) // Initialize COM library
{
   AfxMessageBox("初始化COM库失败");
}
HRESULT hr;
try
{
   hr=m_pConnection.CreateInstance("ADODB.Connection"); // Create Connection object
   if(SUCCEEDED(hr))
   {
    // Connection String   ;DataSource=local
    CString strConncetion=_T("Provider=SQLOLEDB;"
     "Server=192.168.16.110;"
     "database=tITS;"
     //"InitialCatalog=tITS.mdf;"
     "User ID=sa;Password=123;");
    // Open
    hr=m_pConnection->Open((_bstr_t)strConncetion,"","",adModeUnknown);
    if(FAILED(hr))
    {
     AfxMessageBox("Failed to open database");
     return FALSE;
    }
   }
  
}
catch(_com_error e)
{  
   CString strErrorMessage;
  
   strErrorMessage.Format("链接数据库错误,错误信息:/r/t%s",e.ErrorMessage());
   AfxMessageBox(strErrorMessage);
  
   return FALSE;
}

原创粉丝点击