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

📄 cardfiledlg.cpp

📁 一个非常好用的ADO封装类,程序员不再需要跟烦人的COM接口打交道,写数据库程序不再麻烦!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CardFileDlg.cpp : implementation file
//

/*
  CardFile main dialog
  10/17/2007 by Data Management Systems

  This sample application is intended to demonstrate the methods
  used to integrate the AxLib library into a project. AxLib is
  part of dbAx and provides a convenient set of classes to use
  ActiveX Data Objects (ADO) in a Visual C++ environment. The
  application developer is spared the details of interfacing
  with COM and handling variant data types.

  Procedure Summary
  1. Include the AxLib source files in your project and add a
     reference to the AxLib.h file (typically in stdafx.h)

  2. In Project Properties/Linker/Input - add a references to
     Rpcrt4.lib in the "Additional Dependencies" field

  3. Derive application specific versions of the CAxRecordset
     and CAxCommand (if neeeded) classes and add the source
     files to the project. If the AxGen utility was used to
     create these files, they will have .hpp as the file
     extension.

  4. At application startup, typically the InitInstance method
     of the main application module, make a call to the global
     AxLib function dbAx::Init(). At program termination make
     a call to the dbAx::Term function to ensure all references
     to any open connections are propery closed.

  For a full discussion of how to use the AxLib library, please
  see the dbAx.chm help file which is included as part of the
  dbAx library group.
*/

#include "stdafx.h"
#include "CardFile.h"
#include "CardFileDlg.h"

#include "AccountDlg.h"
#include "ContactDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace dbAx;

// CAboutDlg dialog used for App About

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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CCardFileDlg dialog




CCardFileDlg::CCardFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCardFileDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCardFileDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_ACCOUNT_LIST, m_ctrlAccountList);
  DDX_Control(pDX, IDC_CONTACT_LIST, m_ctrlContactList);
}

BEGIN_MESSAGE_MAP(CCardFileDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
  ON_NOTIFY(LVN_ITEMCHANGED, IDC_ACCOUNT_LIST, &CCardFileDlg::OnItemChangedAccountList)
  ON_NOTIFY(LVN_ITEMCHANGED, IDC_CONTACT_LIST, &CCardFileDlg::OnItemChangedContactList)
  ON_BN_CLICKED(IDC_ADD_ACCOUNT, &CCardFileDlg::OnBnClickedAddAccount)
  ON_BN_CLICKED(IDC_EDIT_ACCOUNT, &CCardFileDlg::OnBnClickedEditAccount)
  ON_BN_CLICKED(IDC_DELETE_ACCOUNT, &CCardFileDlg::OnBnClickedDeleteAccount)
  ON_BN_CLICKED(IDC_ADD_CONTACT, &CCardFileDlg::OnBnClickedAddContact)
  ON_BN_CLICKED(IDC_EDIT_CONTACT, &CCardFileDlg::OnBnClickedEditContact)
  ON_BN_CLICKED(IDC_DELETE_CONTACT, &CCardFileDlg::OnBnClickedDeleteContact)
  ON_WM_CLOSE()
END_MESSAGE_MAP()


// CCardFileDlg message handlers

BOOL CCardFileDlg::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

	// TODO: Add extra initialization here
  if ( !ConnectToProvider() )
    return (FALSE);

//  if ( !OpenTables() )
  //  return ( FALSE );

  InitListColumns();
  BuildAccountList();

	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CCardFileDlg::OnClose()
{
  //Closing of library objects is ensured as each object
  //goes out of scope, but doing a manual shutdown doesn抰 hurt.

  try
  {
    if ( m_AccountSet._IsOpen() )
      m_AccountSet.Close();

    if ( m_ContactSet._IsOpen() )
      m_ContactSet.Close();

    m_Cn.Close();

    //Cleanup the AxLib library
    dbAx::Term();
  }
  catch ( CAxException *e )
  {
    MessageBox(e->m_szErrorDesc, _T("CardFile Message"), MB_OK);
    delete e;
  }

  CDialog::OnClose();
}

void CCardFileDlg::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 CCardFileDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCardFileDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CCardFileDlg::OnItemChangedAccountList(NMHDR *pNMHDR, LRESULT *pResult)
{
  LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  // TODO: Add your control notification handler code here
  *pResult = 0;

  if ( pNMLV->uNewState == 3 && !m_AccountSet._IsEmpty() )
  {
    m_AccountSet.AbsolutePosition(pNMLV->iItem + 1);
    BuildContactList();
  }
}


void CCardFileDlg::OnItemChangedContactList(NMHDR *pNMHDR, LRESULT *pResult)
{
  LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  // TODO: Add your control notification handler code here
  *pResult = 0;

  if ( pNMLV->uNewState == 3 && !m_ContactSet._IsEmpty() )
    m_ContactSet.AbsolutePosition(pNMLV->iItem + 1);

}

BOOL CCardFileDlg::ConnectToProvider()
{
  //Connection string generated by AxGen. Change the settings as 
  //required for a particular environment
  CString szConnect = _T("Provider=SQLOLEDB.1;Persist Security Info=True;\
                          User ID=CF_User;Password=CardFile;\
                          Data Source=(local)\\SQLEXPRESS;Initial Catalog=CardFile");

//All calls to the AxLib should be wrapped in a try / catch block
  try
  {
    //Call the global Init function from the AxLib library. This will
    //initialize COM and setup the library's connection collection.

⌨️ 快捷键说明

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