⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample03view.cpp

📁 vc百例之三
💻 CPP
字号:
// Sample03View.cpp : implementation of the CSample03View class
//

#include "stdafx.h"
#include "Sample03.h"

#include "Sample03Doc.h"
#include "Sample03View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSample03View

IMPLEMENT_DYNCREATE(CSample03View, CFormView)

BEGIN_MESSAGE_MAP(CSample03View, CFormView)
	//{{AFX_MSG_MAP(CSample03View)
	ON_CBN_SELCHANGE(IDC_COMBO, OnSelchangeCombo)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSample03View construction/destruction

CSample03View::CSample03View()
	: CFormView(CSample03View::IDD)
{
	//{{AFX_DATA_INIT(CSample03View)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_nField = 0;
}

CSample03View::~CSample03View()
{
}

void CSample03View::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSample03View)
	DDX_Control(pDX, IDC_LIST, m_List);
	DDX_Control(pDX, IDC_COMBO, m_Com);
	//}}AFX_DATA_MAP
}

BOOL CSample03View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CFormView::PreCreateWindow(cs);
}

void CSample03View::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

	// 数据源指针
	m_pDB = new CDaoDatabase;
	
	// 数据源路径
	CString sPath;
	GetModuleFileName(NULL, sPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
	sPath.ReleaseBuffer();
	sPath = sPath.Left (sPath.ReverseFind('\\'));
	sPath += "\\DAO演示数据库.mdb";

	// 打开数据源
	try
	{
		m_pDB->Open(sPath);
	}
	catch(CDaoException* e)
	{
		AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
		delete m_pDB;
		e->Delete();
		return;
	}

    // 表定义信息结构对象
	CDaoTableDefInfo tabInfo;

	// 得到表定义个数
	int nTableDefCount = m_pDB->GetTableDefCount();

	// 对表进行枚举
	for (int i = 0; i < nTableDefCount; i++)
	{
		// 得到表定义信息
		m_pDB->GetTableDefInfo(i, tabInfo);
		if (tabInfo.m_lAttributes & dbSystemObject)
			continue;

		// 将表名添加到组合框控件
		m_Com.AddString(tabInfo.m_strName);
	}

	// 记录集指针
	m_pRecordSet = new CDaoRecordset(m_pDB);
}

/////////////////////////////////////////////////////////////////////////////
// CSample03View printing

BOOL CSample03View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSample03View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSample03View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

void CSample03View::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CSample03View diagnostics

#ifdef _DEBUG
void CSample03View::AssertValid() const
{
	CFormView::AssertValid();
}

void CSample03View::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CSample03Doc* CSample03View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSample03Doc)));
	return (CSample03Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSample03View message handlers

void CSample03View::OnSelchangeCombo() 
{
	// 关闭上次打开的记录集
	if (m_pRecordSet->IsOpen())
		m_pRecordSet->Close();
  
	// 清空列表框
    m_List.DeleteAllItems();
    if (m_nField != 0)
	{
		for (long i = 0; i < m_nField; i++)
			m_List.DeleteColumn(0);
	}
   
	// 从组合框得到选中的表名
	m_Com.GetLBText(m_Com.GetCurSel(), m_sGetString);

	// 构造SQL查询语句
	CString strSQL = "SELECT * FROM " + m_sGetString;

	// 用构造的SQL语句打开记录集
	try
	{
		m_pRecordSet->Open(dbOpenDynaset, strSQL);
		m_pRecordSet->m_strFilter.Empty();
		if (m_pRecordSet == NULL)
			return;
	}
	catch (CDaoException *e)
	{   
		AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
		delete m_pRecordSet;
		m_pDB->Close();
		delete m_pDB;
		e->Delete();
		return ;
	}

	// 设置列表框的扩展风格
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
	m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);						

	LV_COLUMN lvColumn;
	lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvColumn.fmt = LVCFMT_LEFT;
	lvColumn.cx = 67;

	// 得到记录集的字段数
	m_nField = m_pRecordSet->GetFieldCount();

	// 对各列进行处理
	for (int i = 0; i < m_nField; i++)
	{    
		// 得到并插入字段名
		CDaoFieldInfo m_fieldinfo;
		m_pRecordSet->GetFieldInfo(i, m_fieldinfo);
		int len = m_fieldinfo.m_strName.GetLength();
		CString temp = m_fieldinfo.m_strName;
		TCHAR* szBuffer = new TCHAR[len + 1];
		strcpy(szBuffer, temp.GetBuffer(len));
		temp.ReleaseBuffer();
		lvColumn.pszText = szBuffer;
	    m_List.InsertColumn(i, &lvColumn);
		delete szBuffer;
	}

	// 滚动记录集
	m_pRecordSet->MoveFirst();
	m_pRecordSet->MoveLast();

	// 得到记录数
    long count =m_pRecordSet->GetRecordCount();
	
	// 显示记录
    GetTableInfo(count, m_nField);
}

void CSample03View::GetTableInfo(long row, long column)
{
	COleVariant varValue;
	
	// 处理各行
    for (long i = 0; i < row; i++)
	{	
		// 用记录光标重定位到第i条记录。
		try
		{
			m_pRecordSet->SetAbsolutePosition(i);
		}
		catch (CDaoException* e)
		{
			AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
			e->Delete();
			return;
		}
		
		// 处理各列
    	for (long j = 0; j < column; j++)
		{   
			// 得到记录集中的值
			try
			{
				m_pRecordSet->GetFieldValue(j, varValue);
			}
			catch (CDaoException* e)
			{
				AfxMessageBox(e->m_pErrorInfo->m_strDescription, MB_ICONEXCLAMATION);
				e->Delete();
				return;
			}
			
			// 将得到的OLE变量转换为字符串变量
			const VARIANT* variant = LPCVARIANT(varValue);
			if (variant->vt & VT_BYREF)
				return;
			CString string;
			switch (variant->vt)
			{  
				case VT_ERROR:
				{	
					string = "Error";					
					break;
				}
				case VT_I2:
				{ 	
					string.Format("%d", variant->iVal);
					break;
				}
				case VT_I4:
				{ 
				    string.Format("%d", variant->lVal);
					break;
				}
				case VT_R4:
				{   
					string.Format("%.2f", variant->fltVal);
                    break;
				}
				case VT_R8:
				{
					string.Format("%.2f", variant->dblVal);
					break;
				}
				case VT_CY:
				{
					COleCurrency c(varValue);
					string = c.Format();//ie. 1.00
					break;
				}
				case VT_DATE:
				{
					COleDateTime t(variant->date);
					string = t.Format("%B %d, %Y");//Day of Week, Month Day, Year
					break;
				}
				case VT_BSTR:
				{
					string = V_BSTRT(&varValue);//convert BSTR to CString
					break;
				}
				case VT_BOOL:
				{
					if (variant->boolVal)
						string = "TRUE";
					else
						string = "FALSE";
					break;
				}
				case VT_UI1:
				{
					string = (CString)((char*)variant->bVal);
					break;
				}
				default:		
					break;
			}
			
			// 设置各个项目
			if (j == 0)		
				m_List.InsertItem(i, string, 0);
			else
				m_List.SetItemText(i, j, string);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -