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

📄 allmanagerdlg.cpp

📁 c++编程代码,你看到后会与我联系啊,我非常高兴能在这学习
💻 CPP
字号:
// AllManagerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AllManager.h"
#include "AllManagerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAllManagerDlg dialog

CAllManagerDlg::CAllManagerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAllManagerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAllManagerDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	nField = 0;
}

void CAllManagerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAllManagerDlg)
	DDX_Control(pDX, IDC_RECORDLIST, m_recordlist);
	DDX_Control(pDX, IDC_COMBOXTABLE, m_comTable);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAllManagerDlg, CDialog)
	//{{AFX_MSG_MAP(CAllManagerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DISPLAY, OnDisplay)
	//}}AFX_MSG_MAP
	ON_WM_DESTROY()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAllManagerDlg message handlers

BOOL CAllManagerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// 下面是自己添加的初始化代码
	m_pDB = new CDaoDatabase;

	// 和数据库AllManager97.mdb建立连接
	m_pDB->Open("AllManager97.mdb");
    
    CDaoTableDefInfo tabInfo;
	
	// CDaoDatabase的成员函数GetTableDefCount可以获得当前数据库的表的个数
	int nTableDefCount = m_pDB->GetTableDefCount();
	for (int i = 0; i < nTableDefCount; i++)
	{
		m_pDB->GetTableDefInfo(i,tabInfo);
		
		// 如果是数据库提供的系统表,则不把表的名称插入到下拉框中
		if (tabInfo.m_lAttributes & dbSystemObject)
			continue;
		m_comTable.AddString(tabInfo.m_strName);
	}

	m_comTable.SetWindowText("请选择数据库表");

	m_pRecordSet = new CDaoRecordset(m_pDB);

	return TRUE;  
}

void CAllManagerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAllManagerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAllManagerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAllManagerDlg::OnDisplay() 
{ 
	m_comTable.GetWindowText(csTableName);
	if(csTableName == "请选择数据库表")
    {
		AfxMessageBox("请先选择数据库表");
		return ;
	}
	
	
	if(m_pRecordSet->IsOpen())
		m_pRecordSet->Close();
	
    m_recordlist.DeleteAllItems();
    if(nField != 0)
	{
		for(long i=0; i < nField; i++)
		{
			m_recordlist.DeleteColumn(0);
		} 
	}
    
	m_comTable.GetLBText(m_comTable.GetCurSel(), csTableName);
	
	CString strSQL = "SELECT * FROM " + csTableName;
	
	m_pRecordSet -> Open(dbOpenDynaset, strSQL);
	
	m_pImageList = new CImageList();
	m_pImageList->Create(IDB_VCD, 24, 1, RGB(255,0,0));
	m_recordlist.SetImageList(m_pImageList, LVSIL_SMALL);	
	
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_TRACKSELECT;
	m_recordlist.SetExtendedStyle(dwExStyle);
		
	
	LV_COLUMN lvColumn;
	lvColumn.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvColumn.fmt = LVCFMT_LEFT;	
	lvColumn.cx = 67;
	nField=m_pRecordSet->GetFieldCount();
	
	for(int i = 0; i < 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_recordlist.InsertColumn(i, &lvColumn);		
		delete szBuffer;
	}
	
	DisplayRecord();
	

	delete m_pImageList;
}

void CAllManagerDlg::DisplayRecord()
{
	COleVariant varValue;

	if(m_pRecordSet->IsEOF())
		return;
	m_pRecordSet->MoveFirst();
	m_recordlist.DeleteAllItems();

	int i=0;
	while(!m_pRecordSet->IsEOF())
	{
		
		for(long j = 0; j < m_pRecordSet->GetFieldCount(); j++)
		{   
			m_pRecordSet->GetFieldValue(j, varValue);
			
			CString csText = ChangeType(varValue);

			if(j == 0)		
				m_recordlist.InsertItem(i, csText, 0);
			else
				m_recordlist.SetItemText(i, j, csText);
		}

		i++;
		m_pRecordSet->MoveNext();
	}

}

CString CAllManagerDlg::ChangeType(COleVariant var)
{
	const VARIANT * variant = LPCVARIANT(var);
	CString csTemp;
	switch(variant->vt)
	{  
	case VT_ERROR:
		{	
			csTemp = "Error";					
			break;
		}
	case VT_I2:
		{ 	
			csTemp.Format("%d", variant->iVal);
			break;
		}
	case VT_I4:
		{ 
			csTemp.Format( "%d", variant->lVal);
			break;
		}
	case VT_R4:
		{   
			csTemp.Format( "%.2f", variant->fltVal);
			
			break;}
		
	case VT_R8:
		{	
			csTemp.Format( "%.2f", variant->dblVal);
			break;
		}
	case VT_CY:
		{	
			COleCurrency c(var);
			csTemp = c.Format();//ie. 1.00
			break;
		}
	case VT_DATE:
		{	
			COleDateTime t(variant->date);
			csTemp = t.Format( "%B %d, %Y" );//Day of Week, Month Day, Year
			
			break;
		}
	case VT_BSTR:
		{   
			csTemp = V_BSTRT( &var );//convert BSTR to CString
			
			break;
		}
	case VT_BOOL:
		{	
			if(variant->boolVal)
				csTemp = "TRUE";
			else
				csTemp =  "FALSE";
			break;
		}
	case VT_UI1:
		{
			csTemp = (CString)((char*)variant->bVal);
			break;
		}
		
	default:{
		
		break;
			}
		
	}
	return csTemp;
}

void CAllManagerDlg::OnDestroy()
{
	CDialog::OnDestroy();

	delete m_pDB;
	delete m_pRecordSet;
}

⌨️ 快捷键说明

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