?? mytelbooksdlg.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 + -