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

📄 mytelbooksdlg.cpp

📁 visual c++ 实例编程
💻 CPP
字号:
// MyTelBooksDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MyTelBooks.h"
#include "MyTelBooksDlg.h"
#include "DlgProxy.h"
#include "Provider.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()

/////////////////////////////////////////////////////////////////////////////
// CMyTelBooksDlg dialog

//这个宏的基类不能改为CExpandingDialog!
IMPLEMENT_DYNAMIC(CMyTelBooksDlg, CDialog);

CMyTelBooksDlg::CMyTelBooksDlg(CWnd* pParent /*=NULL*/)
	: CExpandingDialog(CMyTelBooksDlg::IDD, pParent,
	IDC_DEFAULTBOX,IDC_EXPAND, _T("更多信息"),_T("更多信息"))
{
	//IDC_DEFAULTBOX表示覆盖框的ID,IDC_EXPAND与是否展开右边对应;
	//第一个“更多信息”使控件展开前的提示,接着使展开后的提示
	//{{AFX_DATA_INIT(CMyTelBooksDlg)
	m_strAsitis = _T("");
	m_bExpand = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pAutoProxy = NULL;
	//初始化Image(Bitmap)链
	m_pImageList = new CImageList();
}

CMyTelBooksDlg::~CMyTelBooksDlg()
{
	// If there is an automation proxy for this dialog, set
	//  its back pointer to this dialog to NULL, so it knows
	//  the dialog has been deleted.
	if (m_pAutoProxy != NULL)
		m_pAutoProxy->m_pDialog = NULL;
	//删除Bitmap链
	if( m_pImageList )
		delete m_pImageList;
}

void CMyTelBooksDlg::DoDataExchange(CDataExchange* pDX)
{
	//修改基类
	CExpandingDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyTelBooksDlg)
	DDX_Control(pDX, IDC_LIST_TREE, m_ctrlTree);
	DDX_Text(pDX, IDC_AS_IT_IS, m_strAsitis);
	DDX_Check(pDX, IDC_EXPAND, m_bExpand);
	//}}AFX_DATA_MAP
}

//修改基类
BEGIN_MESSAGE_MAP(CMyTelBooksDlg, CExpandingDialog)
	//{{AFX_MSG_MAP(CMyTelBooksDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(ID_DONE, OnDone)
	ON_NOTIFY(TVN_SELCHANGED, IDC_LIST_TREE, OnSelchangedListTree)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyTelBooksDlg message handlers

BOOL CMyTelBooksDlg::OnInitDialog()
{
	//修改基类
	CExpandingDialog::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
	InitImageList();//装载位图
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyTelBooksDlg::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 CMyTelBooksDlg::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 CMyTelBooksDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// Automation servers should not exit when a user closes the UI
//  if a controller still holds on to one of its objects.  These
//  message handlers make sure that if the proxy is still in use,
//  then the UI is hidden but the dialog remains around if it
//  is dismissed.

void CMyTelBooksDlg::OnClose() 
{
	if (CanExit())
		CDialog::OnClose();
}

void CMyTelBooksDlg::OnOK() 
{
	if (CanExit())
		CDialog::OnOK();
}

void CMyTelBooksDlg::OnCancel() 
{
	if (CanExit())
		CDialog::OnCancel();
}

BOOL CMyTelBooksDlg::CanExit()
{
	// If the proxy object is still around, then the automation
	//  controller is still holding on to this application.  Leave
	//  the dialog around, but hide its UI.
	if (m_pAutoProxy != NULL)
	{
		ShowWindow(SW_HIDE);
		return FALSE;
	}

	return TRUE;
}

void CMyTelBooksDlg::OnDone() 
{
	//声明行集对象
	CCommand<CAccessor<CProvider>,CRowset,CNoMultipleResults> table;
	CDataSource source;//数据源对象
	CSession   session;//到数据源的连接

	//弹出文件选择对话框
	CString strSel = "Text Files(*.txt)|*.txt|其他文件(*.*)|*.*||";
	CFileDialog dlg( TRUE, 
					 "*.txt", 
					 "*.txt",OFN_ALLOWMULTISELECT |OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
					 strSel );
	if( dlg.DoModal() == IDCANCEL ) return;
	CString strPath = dlg.GetPathName ();


	//到注册表查找服务器
	if (source.Open("TelProvider.TelPrvd.1", NULL, NULL, NULL,
		NULL) != S_OK)
	{
		AfxMessageBox(" 没有安装对应的数据提供程序!");
		return;
	}
	
	if (session.Open(source) != S_OK)
		return;

	CDBPropSet propset(DBPROPSET_ROWSET);
	propset.AddProperty(DBPROP_IRowsetLocate, true);

	//把文件名传给服务器,返回的行集数据存在table中
	if (table.Open(session,strPath, &propset) != S_OK)
	{
		AfxMessageBox("打开记录集时出错!");
		return;
	}
	//确保刷新时上次的所有结点均被删除
	m_ctrlTree.DeleteAllItems();	
	if( strAsitisAry.GetSize() )
		strAsitisAry.RemoveAll();

	//插入根结点
	HTREEITEM hParent = InsItemToTree(NULL,CString("联系名录"),0);	
	CBookmark<4> tempBookmark;
	ULONG ulCount=0;
	while (table.MoveNext() == S_OK)
	{
		//插入联系人结点
		HTREEITEM hTemp = NULL;
		HTREEITEM hSubTemp = NULL;
		hTemp = InsItemToTree(hParent,table.szName,1);
		m_ctrlTree.SetItemData(hTemp,ulCount);
		hSubTemp = InsItemToTree(hTemp,table.szEMail,2);
		m_ctrlTree.SetItemData(hSubTemp,ulCount);
		hSubTemp = InsItemToTree(hTemp,table.szTele,3);
		m_ctrlTree.SetItemData(hSubTemp,ulCount);
		hSubTemp = InsItemToTree(hTemp,table.szCompany,4);
		m_ctrlTree.SetItemData(hSubTemp,ulCount);
		hSubTemp = InsItemToTree(hTemp,table.szVocation,5);
		m_ctrlTree.SetItemData(hSubTemp,ulCount);
		strAsitisAry.Add(table.szAs_it_is);
		//第一次运行时,“个人写真”插入第一个人的相关信息
		if( ulCount == 0 )
		{
			m_strAsitis = table.szAs_it_is;
			UpdateData(FALSE);
		}
				
		ulCount++;
	}
}

void CMyTelBooksDlg::InitImageList()
{
	CBitmap	 bitmap;
	m_pImageList->Create( IDB_CONNECT, 20,  1, RGB(0,255,0) );	
	m_ctrlTree.SetImageList(m_pImageList, TVSIL_NORMAL);
}

HTREEITEM CMyTelBooksDlg::InsItemToTree( HTREEITEM hParent, 
										 CString strItemt,
										 int nSelectedImage)
{
	TV_INSERTSTRUCT	tvstruct;	
	tvstruct.hParent = hParent;
	tvstruct.hInsertAfter = TVI_LAST;
	tvstruct.item.iImage = nSelectedImage;
	tvstruct.item.iSelectedImage = nSelectedImage;
	tvstruct.item.pszText = strItemt.GetBuffer(256);
	tvstruct.item.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_TEXT;
    HTREEITEM hNewItem = m_ctrlTree.InsertItem(&tvstruct);
	return hNewItem;
}

void CMyTelBooksDlg::OnSelchangedListTree(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	//获得当前结点一起ItemData数据
	HTREEITEM hCurItem = m_ctrlTree.GetSelectedItem();
	int  nItemData = m_ctrlTree.GetItemData(hCurItem);
	//从数组中取出对应信息
	m_strAsitis = strAsitisAry.GetAt(nItemData);
	UpdateData(FALSE);//更新右边文本

	*pResult = 0;
}

⌨️ 快捷键说明

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