数据库 树形控件 视图分割

来源:互联网 发布:手机迅雷显示网络异常 编辑:程序博客网 时间:2024/05/16 06:27

关于数据库

 

l         stdafx.h添加:

#import "C:/Program Files/Common Files/System/ado/msado15.dll" rename_namespace("ADOBS") rename("EOF","adoEOF") rename("BOF","adoBOF")

using namespace ADOBS;

 

l         新建CDataManage

头文件:

#pragma once

 

class CDataManage

{

private:

    _ConnectionPtr p_Con;

    _RecordsetPtr p_Record;

    _CommandPtr p_Com;

public:

    CDataManage(void);

public:

    ~CDataManage(void);

public:

    bool InitADO(void);

public:

    bool ConnectionGatabase(LPTSTR ConStr);

};

 

源文件:

#include "StdAfx.h"

#include "DataManage.h"

 

CDataManage::CDataManage(void)

{

}

 

CDataManage::~CDataManage(void)

{

    if(p_Com!=NULL)

       p_Com.Release();

    if(p_Record!=NULL)

       p_Record.Release();

    if(p_Con!=NULL)

       p_Con.Release();

 

}

 

bool CDataManage::InitADO(void)

{

    try

    {

       //初始化数据库连接对象

       p_Con.CreateInstance("ADODB.Connection");

       p_Record.CreateInstance("ADODB.Recordset");

       p_Com.CreateInstance("ADODB.Command");

    }

    catch (_com_error & e)

    {

       AfxMessageBox(e.Description());

       return false;

    }

    return true;

}

 

bool CDataManage::ConnectionGatabase(LPTSTR ConStr)

{

    try

    {

       //如果连接已经打开则关闭

       if(p_Con->State==adStateOpen)

           p_Con->Close();

       p_Con->ConnectionString=ConStr;

       p_Con->Open(L"",L"",L"",-1);

    }

    catch (_com_error &e)

    {

       return false;

    }

    return true;

}

 

树视图设计

l         新建CCustomTree类派生与CTreeView

l         CCustomTree定义成员变量

    CStringArray m_Treenodes;//记录节点文本

    CImageList m_imagelist;//图像列表

l        OnInitialUpdate()方法中创建图像列表,添加图标,创建和设置字体,修改树视图风格,添加节点,展开用函数:this->GetTreeCtrl().Expand(),设置背景this->GetTreeCtrl().SetBkColor(),设置文本颜色:this->GetTreeCtrl().SetTextColor()。设置字体this->SetFont();

 

void CCustomTree::OnInitialUpdate()

{

    CTreeView::OnInitialUpdate();

 

    // TODO: Add your specialized code here and/or call the base class

    m_Treenodes.Add(L"功能");

    m_Treenodes.Add(L"功能");

    m_imagelist.Create(16,16,ILC_COLOR24|ILC_MASK,5,5);

    m_imagelist.Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));

    this->GetTreeCtrl().SetImageList(&m_imagelist,TVSIL_NORMAL);

    this->GetTreeCtrl().DeleteAllItems();

    //添加树节点

    HTREEITEM hroot;

    CString tempstr;

    hroot=this->GetTreeCtrl().InsertItem(L"功能列表",0,0);

    for (int i=0;i<m_Treenodes.GetSize();i++)

    {

       this->GetTreeCtrl().InsertItem(m_Treenodes[i],0,0,hroot);

    }

}

分割视图

客户区有树视图和普通视图两部分组成,因此需要分割框架窗口。具体步骤如下:

(1)       在框架类中定义:CSplitterWnd m_splitter;

(2)       改写框架类的虚方法OnCreateClient()

(3)       注意2点:一,对于视图类的头文件包含的时候首先包含和它关联的文档类;二,不需要载调用CFrameWnd::OnCreateClient

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)

{

    // TODO: Add your specialized code here and/or call the base class

    m_splitter.CreateStatic(this,1,2);

    m_splitter.CreateView(0,1,RUNTIME_CLASS(CCaptureView),CSize(20,100),pContext);

    m_splitter.CreateView(0,0,RUNTIME_CLASS(CCustomTree),CSize(200,1000),pContext);

 

    return true;//CFrameWnd::OnCreateClient(lpcs, pContext);

}

原创粉丝点击