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

📄 odbclistview.cpp

📁 VC++高级编程技巧与示例
💻 CPP
字号:
// ODBCListView.cpp : implementation of the CODBCListView class
//

#include "stdafx.h"
#include "ODBCList.h"

#include "ODBCListDoc.h"
#include "ODBCListView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CODBCListView

IMPLEMENT_DYNCREATE(CODBCListView,CListView)

BEGIN_MESSAGE_MAP(CODBCListView,CListView)
	//{{AFX_MSG_MAP(CODBCListView)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CODBCListView construction/destruction

CODBCListView::CODBCListView()
{
	// TODO: add construction code here
//●>
	m_set=NULL;
	m_List=&GetListCtrl();
	m_dwDefaultStyle |= LVS_REPORT;
//<●
}

CODBCListView::~CODBCListView()
{
}

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

	return CListView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CODBCListView drawing

void CODBCListView::OnDraw(CDC* pDC)
{
	CODBCListDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

void CODBCListView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: You may populate your ListView with items by directly accessing
	//  its list control through a call to GetListCtrl().
//★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    if(m_set==NULL)
	{
		AfxMessageBox("尚未初始化ODBC记录集!");
		return;
	}

	if(!m_set->IsOpen())
	{
		m_set->Open();
	}

	//1. 添加列:
	short i,j;
	CODBCFieldInfo Fi;
	CString tmp;
    LV_COLUMN lc;
    lc.mask = LVCF_FMT | LVCF_WIDTH |
    	          LVCF_TEXT | LVCF_SUBITEM;
	for(i=0;i<m_set->m_nFields;i++)
	{
		m_set->GetODBCFieldInfo(i,Fi);
	    lc.iSubItem = i;
		lc.fmt = LVCFMT_CENTER;
	    lc.cx = Fi.m_strName.GetLength()*10+16;
	    lc.pszText=Fi.m_strName.GetBuffer(Fi.m_strName.GetLength());
	    GetListCtrl().InsertColumn(i, &lc);
	}
	//2. 添加行:
	LV_ITEM Item;
	CString str;
	CDBVariant var;
	i=0;
	if(m_set->IsEOF())
	{
		AfxMessageBox("当前视图没有记录!");
		return;
	}

	m_set->MoveFirst();
	while(m_set->IsEOF()==0)
	{
		Item.mask=LVIF_TEXT;
		//第i 行:
	    Item.iItem=i;

		//第一列:
   	    Item.iSubItem=0;
		m_set->GetFieldValue((short)0,str);
		Item.pszText=str.GetBuffer(str.GetLength());
	    m_List->InsertItem(&Item);
		//其它各列:
		for(j=1;j<m_set->m_nFields;j++)
		{
//==>Wrong!
			m_set->GetFieldValue((short)j,str);//获取数据;
			m_List->SetItemText(i,j,str.GetBuffer(str.GetLength()));
		}

		m_set->MoveNext();
		i++;//计数器;
    }
//★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
}

/////////////////////////////////////////////////////////////////////////////
// CODBCListView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CODBCListView diagnostics

#ifdef _DEBUG
void CODBCListView::AssertValid() const
{
	CListView::AssertValid();
}

void CODBCListView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CODBCListView message handlers

void CODBCListView::SetRecordset(CRecordset *pSet,UINT nk)
{
	m_set=pSet;
	m_NKey=nk;
}

void CODBCListView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CListView::OnLButtonDblClk(nFlags, point);
	m_CurSel=m_List->GetSelectionMark();
	m_set->MoveFirst();
	if(m_CurSel!=-1)
	{
		m_set->Move(m_CurSel);
	}
	m_set->GetFieldValue(m_NKey,m_Key);
	AfxMessageBox("当前记录的指定字段取值为:"+m_Key);
}

void CODBCListView::OnDestroy() 
{
	CListView::OnDestroy();
	
	// TODO: Add your message handler code here
	if(m_set->IsOpen())
	{
		m_set->Close();
	}
}

⌨️ 快捷键说明

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