www.gusucode.com > 获取本机通讯薄的内容C++源码程序 > 获取本机通讯薄的内容/GetEmailSrc/GetEmailSrc/GetEmailDlg.cpp
////////////////////////////////////////////////////////////////////////////// //类名:CGetEmailDlg //功能:读取通讯薄内容(类型、呢称、名字、EMAIL) //编写:徐景周(jingzhou_xu@163.net) //组织:未来工作室(Future Studio) //日期:2003.4.21 ///////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "GetEmail.h" #include "GetEmailDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // 导入读取Outlook2000中通讯薄内容所需库 #import "e:\Program Files\Microsoft Office\Office\mso9.dll" named_guids #import "e:\Program Files\Microsoft Office\Office\MSOUTL9.olb" \ no_namespace exclude("_IRecipientControl", "_DRecipientControl") // Outlook Express中通讯薄部分 #include <wab.h> // 通讯薄头文件 // 内部涵数声明 typedef HRESULT (WINAPI *fWABOpen)(LPADRBOOK*,LPWABOBJECT*,LPWAB_PARAM,DWORD); ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; CXPButton m_OK; //}}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) DDX_Control(pDX, IDOK, m_OK); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGetEmailDlg dialog CGetEmailDlg::CGetEmailDlg(CWnd* pParent /*=NULL*/) : CDialog(CGetEmailDlg::IDD, pParent) { //{{AFX_DATA_INIT(CGetEmailDlg) // 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); } void CGetEmailDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGetEmailDlg) DDX_Control(pDX, ID_OUTLOOK2000, m_Outlook); DDX_Control(pDX, IDOK, m_OK); DDX_Control(pDX, IDCANCEL, m_Cancel); DDX_Control(pDX, IDC_LIST1, m_ListEmail); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGetEmailDlg, CDialog) //{{AFX_MSG_MAP(CGetEmailDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_CTLCOLOR() ON_BN_CLICKED(ID_OUTLOOK2000, OnOutlook2000) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGetEmailDlg message handlers BOOL CGetEmailDlg::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_tooltip.Create(this); m_tooltip.AddTool(GetDlgItem(IDOK), _T("<b><ct=0x0000FF><al_c>读取</b><br><ct=0x00AA00><hr=100%></ct><br>读取Outlook Express中通讯薄内容"),IDI_PINFORMATION); m_tooltip.AddTool(GetDlgItem(ID_OUTLOOK2000), _T("<b><ct=0x0000FF><al_c>读取</b><br><ct=0x00AA00><hr=100%></ct><br>读取Outlook2000中通讯薄内容"),IDI_PINFORMATION); m_tooltip.AddTool(GetDlgItem(IDCANCEL), _T("退出")); m_tooltip.AddTool(GetDlgItem(IDC_LIST1), _T("<b><ct=0x0000FF><al_c>显示</b><br><ct=0x00AA00><hr=100%></ct><br>通讯薄内容显示列表"),IDI_PINFORMATION); // 显示图标或位图及字体属性 m_tooltip.SetNotify(); m_tooltip.SetDefaultFont(); // 设置背景效果及渐变色 m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_BEGIN, RGB(255, 255, 255)); m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_MID,RGB(240, 247, 250)); m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_END, RGB(192, 192, 200)); // m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_BEGIN, RGB(255, 255, 223)); // m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_MID,RGB(192, 192, 172)); // m_tooltip.SetColor(CPPToolTip::PPTOOLTIP_COLOR_BK_END, RGB(128, 128, 112)); m_tooltip.SetEffectBk(14, 10); // 多行显示提示信息 m_tooltip.SetBehaviour(PPTOOLTIP_MULTIPLE_SHOW); // 程序初始启动时,窗体位于最前方 SetWindowPos(&wndTopMost,0,0,0,0, SWP_NOMOVE | SWP_NOSIZE); return TRUE; // return TRUE unless you set the focus to a control } void CGetEmailDlg::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 CGetEmailDlg::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 CGetEmailDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } // 读取Outlook Express通讯薄内容(类型、呢称、名字、EMAIL) void CGetEmailDlg::OnOK() { HRESULT hRes; LPADRBOOK lpAdrBook; LPWABOBJECT lpWABObject; LPWAB_PARAM lpWABParam = NULL; DWORD Reserved2 = NULL; HINSTANCE hinstLib; hinstLib = LoadLibrary("D:\\Program Files\\Common Files\\System\\wab32"); fWABOpen procWABOpen; if (hinstLib != NULL) { // 获取"Wab32.dll"内部涵数WABOpen的进程地址 procWABOpen = (fWABOpen) GetProcAddress(hinstLib, "WABOpen"); if (procWABOpen != NULL) { hRes = (procWABOpen)(&lpAdrBook,&lpWABObject,NULL,Reserved2); _ASSERTE(hRes == S_OK); if (hRes != S_OK) exit(1); ULONG lpcbEntryID; ENTRYID *lpEntryID; hRes = lpAdrBook->GetPAB( &lpcbEntryID, &lpEntryID ); _ASSERTE(hRes == S_OK); if (hRes != S_OK) exit(2); ULONG ulFlags = MAPI_BEST_ACCESS; ULONG ulObjType = NULL; LPUNKNOWN lpUnk = NULL; hRes = lpAdrBook->OpenEntry( lpcbEntryID, lpEntryID, NULL, ulFlags, &ulObjType, &lpUnk ); ulFlags = NULL; if (ulObjType == MAPI_ABCONT) { IABContainer *lpContainer = static_cast <IABContainer *>(lpUnk); LPMAPITABLE lpTable = NULL; hRes = lpContainer->GetContentsTable( ulFlags, &lpTable ); _ASSERT(lpTable); ULONG ulRows; hRes = lpTable->GetRowCount(0,&ulRows); _ASSERTE(hRes == S_OK); SRowSet *lpRows; hRes = lpTable->QueryRows( ulRows, // 获取所有行 0, &lpRows ); m_ListEmail.ResetContent(); for(ULONG i=0;i<lpRows->cRows;i++) { SRow *lpRow = &lpRows->aRow[i]; CString strTemp; for(ULONG j=0;j<lpRow->cValues;j++) { SPropValue *lpProp = &lpRow->lpProps[j]; if (lpProp->ulPropTag == PR_DISPLAY_NAME_A) strTemp = strTemp + " 名字: " + (char *)lpProp->Value.lpszA; if (lpProp->ulPropTag == PR_EMAIL_ADDRESS_A) strTemp = strTemp + " Email: " + (char *)lpProp->Value.lpszA; if (lpProp->ulPropTag == PR_NICKNAME_A) strTemp = strTemp + " 呢称: " + (char *)lpProp->Value.lpszA; if (lpProp->ulPropTag == PR_ADDRTYPE_A) strTemp = strTemp + " 类型: " + (char *)lpProp->Value.lpszA; } m_ListEmail.AddString(strTemp); lpWABObject->FreeBuffer(lpRow); } lpWABObject->FreeBuffer(lpRows); } } FreeLibrary(hinstLib); // 读取成功后,置读取按钮无效 CButton* pBtn = (CButton*)GetDlgItem(IDOK); pBtn->EnableWindow(FALSE); } } // 读取Outlook2k中通讯薄内容(名字、Email) void CGetEmailDlg::OnOutlook2000() { _ApplicationPtr pApp; _ItemsPtr pItems; MAPIFolderPtr pFolder; _ContactItemPtr pContact; HRESULT hr; try { hr=pApp.CreateInstance(__uuidof(Application)); if (FAILED(hr)) { MessageBox("Outlook实例创建失败","错误",MB_OK); return; } // 获取默认Outlook中联系人文件夹 pFolder=pApp->GetNamespace(_bstr_t("MAPI"))->GetDefaultFolder(olFolderContacts); if (pFolder==NULL) { MessageBox("没有发现默认的Outlook联系人文件夹","错误!"); return; } else // 否则自行选择Outlook中一指定文件夹 { pFolder=pApp->GetNamespace(_bstr_t("MAPI"))->PickFolder(); if (pFolder==NULL) return; if (pFolder->GetDefaultItemType()!=olContactItem) // 不是联系人 { MessageBox("选择不是联系人文件夹","错误"); return; } } pItems=pFolder->GetItems(); if (pItems==NULL) { MessageBox("不能得到联系人条目","错误"); return; } pContact=pItems->GetFirst(); m_ListEmail.ResetContent(); while(1) { if (pContact==NULL) break; CString strTemp; strTemp=(char *)pContact->GetFullName(); strTemp=strTemp + "<"; strTemp=strTemp + (char *)pContact->GetEmail1Address(); strTemp=strTemp + ">"; m_ListEmail.AddString(strTemp); pContact=pItems->GetNext(); } } catch(_com_error &e) { MessageBox((char *)e.Description()); } } // 消息传递 BOOL CGetEmailDlg::PreTranslateMessage(MSG* pMsg) { // 为阴影工具提示加入鼠标事件传递 m_tooltip.RelayEvent(pMsg); return CDialog::PreTranslateMessage(pMsg); } ////////////////////////////////////////////////////////////////////////////// //名称:OnCtlColor //功能:设置各控件前景、背景色 //作者:徐景周(jingzhou_xu@163.net) //组织:未来工作室(Future Studio) //日期:2003.4.21 ///////////////////////////////////////////////////////////////////////////// HBRUSH CGetEmailDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(nCtlColor==CTLCOLOR_LISTBOX) { //pDC->SetBkMode(TRANSPARENT); pDC->SetTextColor(RGB(0,0,0)); pDC->SetBkColor(RGB(233,233,220)); HBRUSH b=CreateSolidBrush(RGB(233,233,220)); return b; } else if(nCtlColor==CTLCOLOR_SCROLLBAR) { } else if(nCtlColor==CTLCOLOR_EDIT) { } else if(nCtlColor==CTLCOLOR_STATIC) { } else if(nCtlColor==CTLCOLOR_DLG) { } return hbr; }