📄 simcontact.cpp
字号:
//{
// int wmId, wmEvent;
// PAINTSTRUCT ps;
// HDC hdc;
//
//#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
// static SHACTIVATEINFO s_sai;
//#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
//
// switch (message)
// {
// case WM_COMMAND:
// wmId = LOWORD(wParam);
// wmEvent = HIWORD(wParam);
// // Parse the menu selections:
// switch (wmId)
// {
//#ifndef WIN32_PLATFORM_WFSP
// case IDM_HELP_ABOUT:
// DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
// break;
//#endif // !WIN32_PLATFORM_WFSP
//#ifdef WIN32_PLATFORM_WFSP
// case IDM_OK:
// DestroyWindow(hWnd);
// break;
//#endif // WIN32_PLATFORM_WFSP
//#ifdef WIN32_PLATFORM_PSPC
// case IDM_OK:
// SendMessage (hWnd, WM_CLOSE, 0, 0);
// break;
//#endif // WIN32_PLATFORM_PSPC
// default:
// return DefWindowProc(hWnd, message, wParam, lParam);
// }
// break;
// case WM_CREATE:
//#ifdef SHELL_AYGSHELL
// SHMENUBARINFO mbi;
//
// memset(&mbi, 0, sizeof(SHMENUBARINFO));
// mbi.cbSize = sizeof(SHMENUBARINFO);
// mbi.hwndParent = hWnd;
// mbi.nToolBarId = IDR_MENU;
// mbi.hInstRes = g_hInst;
//
// if (!SHCreateMenuBar(&mbi))
// {
// g_hWndMenuBar = NULL;
// }
// else
// {
// g_hWndMenuBar = mbi.hwndMB;
// }
//
//#ifndef WIN32_PLATFORM_WFSP
// // Initialize the shell activate info structure
// memset(&s_sai, 0, sizeof (s_sai));
// s_sai.cbSize = sizeof (s_sai);
//#endif // !WIN32_PLATFORM_WFSP
//#endif // SHELL_AYGSHELL
// break;
// case WM_PAINT:
// hdc = BeginPaint(hWnd, &ps);
//
// // TODO: Add any drawing code here...
//
// EndPaint(hWnd, &ps);
// break;
// case WM_DESTROY:
//#ifdef SHELL_AYGSHELL
// CommandBar_Destroy(g_hWndMenuBar);
//#endif // SHELL_AYGSHELL
// PostQuitMessage(0);
// break;
//
//#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
// case WM_ACTIVATE:
// // Notify shell of our activate message
// SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
// break;
// case WM_SETTINGCHANGE:
// SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
// break;
//#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
//
// default:
// return DefWindowProc(hWnd, message, wParam, lParam);
// }
// return 0;
//}
//
//#ifndef WIN32_PLATFORM_WFSP
//// Message handler for about box.
//INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
//{
// switch (message)
// {
// case WM_INITDIALOG:
//#ifdef SHELL_AYGSHELL
// {
// // Create a Done button and size it.
// SHINITDLGINFO shidi;
// shidi.dwMask = SHIDIM_FLAGS;
// shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
// shidi.hDlg = hDlg;
// SHInitDialog(&shidi);
// }
//#endif // SHELL_AYGSHELL
//
// return (INT_PTR)TRUE;
//
// case WM_COMMAND:
//#ifdef SHELL_AYGSHELL
// if (LOWORD(wParam) == IDOK)
//#endif
// {
// EndDialog(hDlg, LOWORD(wParam));
// return (INT_PTR)TRUE;
// }
// break;
//
// case WM_CLOSE:
// EndDialog(hDlg, message);
// return (INT_PTR)TRUE;
//
//#ifdef _DEVICE_RESOLUTION_AWARE
// case WM_SIZE:
// {
// DRA::RelayoutDialog(
// g_hInst,
// hDlg,
// DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
// }
// break;
//#endif
// }
// return (INT_PTR)FALSE;
//}
//#endif // !WIN32_PLATFORM_WFSP
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
#include "stdafx.h"
#define INITGUID
#include "windows.h"
#include <initguid.h>
#include <pimstore.h>
#undef INITGUID
#include "pimstoreEx.h"
#include "Oleauto.h"
#define PHONE_FIRST _T("1111111")
#define PHONE_SECOND _T("2222222")
#define PHONE_THIRD _T("3333333")
#define PHONE_RESTRICTION_SECOND _T("[MobileTelephoneNumber] = \"2222222\"")
#define PHONE_RESTRICTION_NOTSECOND _T("[MobileTelephoneNumber] <> \"2222222\"")
#define PHONE_RESTRICTION_NOTTHIRD _T("[MobileTelephoneNumber] <> \"3333333\"")
#define SIMONLY_RESTRICTION _T("[ContactType] = 1")
#define NOSIM_RESTRICTION _T("[ContactType] <> 1")
//#pragma comment(lib, "pimstore.lib")
//#pragma comment(lib,"coredll.lib")
//#pragma comment(lib,"ole32.lib")
//#pragma comment(lib,"Oleaut32.lib")
//#pragma comment(lib,"aygshell.lib")
//#pragma comment(lib,"phone.lib")
//#pragma comment(lib,"uuid.lib")
// Helpful macros
#define ARRAYSIZE(s) (sizeof(s) / sizeof(s[0]))
#define RELEASE(x) if ((x) != NULL) { (x)->Release(); (x) = NULL; }
#define _ErrorLabel Error
#define CHRA(hResult) \
if (FAILED(hResult)) { ASSERT(SUCCEEDED(hResult)); hr = (hResult); goto _ErrorLabel;}
// Helper functions
HRESULT ShowCollection(__in IPOutlookItemCollection * polItems, __in LPWSTR szTitle);
HRESULT CreateContactByType(__in IPOutlookApp *polApp,
int olItemType,
__in BSTR szName,
__in BSTR szTitle);
void TRACE(
LPTSTR lpszFormat,
...)
{
TCHAR szBuf[1024];
if(SUCCEEDED(StringCchVPrintf((LPTSTR)szBuf, ARRAYSIZE(szBuf), lpszFormat, (LPSTR)(&lpszFormat + 1))))
{
OutputDebugString((LPCTSTR)szBuf);
}
}
/////////////////////////////////////////////////////////////////////////////
// WinMain
/////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
HRESULT hr = S_OK;
IPOutlookApp *polApp = NULL;
IFolder * pFolder = NULL;
IPOlItems3 * polItemsBase = NULL,
* polItemsBase2 = NULL;
IPOutlookItemCollection * polDefaultCollection = NULL,
* polDefaultCollection2 = NULL,
* polRestrictedNotSecondPhone = NULL,
* polRestrictedOnlyFirstPhone = NULL,
* polRestrictedSimOnly = NULL,
* polRestrictedSecondPhone = NULL,
* polRestrictedNoSim = NULL;
// Logon to POOM
CoInitializeEx(NULL, COINIT_MULTITHREADED);
hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER, IID_IPOutlookApp, (LPVOID *) &polApp);
CHRA(hr);
ASSERT(polApp);
hr = polApp->Logon(NULL);
CHRA(hr);
// Create 3 device contacts
hr = CreateContactByType(polApp, olContactItem, _T("Device Contact 1"), PHONE_FIRST);
CHRA(hr);
hr = CreateContactByType(polApp, olContactItem, _T("Device Contact 2"), PHONE_SECOND);
CHRA(hr);
hr = CreateContactByType(polApp, olContactItem, _T("Device Contact 3"), PHONE_THIRD);
CHRA(hr);
// Create 2 SIM contacts
hr = CreateContactByType(polApp, olSimContactItem, _T("Sim Contact 1"), PHONE_FIRST);
CHRA(hr);
hr = CreateContactByType(polApp, olSimContactItem, _T("Sim Contact 2"), PHONE_SECOND);
CHRA(hr);
// Sim contacts take a while to create and it isn't synchronous
Sleep(10000);
// Get the default contacts collection and display it - no SIM contacts
hr = polApp->GetDefaultFolder(olFolderContacts, &pFolder);
CHRA(hr);
hr = pFolder->get_Items(&polDefaultCollection);
CHRA(hr);
hr = ShowCollection(polDefaultCollection, _T("Default contacts collection:"));
CHRA(hr);
// Include SIM contacts in the collection and display it - Device and SIM contacts
hr = polDefaultCollection->QueryInterface(IID_IPOlItems3, (void**)&polItemsBase);
CHRA(hr);
hr = polItemsBase->IncludeSimContacts();
CHRA(hr);
hr = ShowCollection(polItemsBase, _T("Including SIM contacts:"));
CHRA(hr);
// Calling IncludeSimContacts again has no effect, but does not fail - same as previous
hr = polItemsBase->IncludeSimContacts();
CHRA(hr);
hr = ShowCollection(polItemsBase, _T("Including SIM contacts second time:"));
CHRA(hr);
// Restrict to just the SIM contacts and display it - SIM contacts only
hr = polItemsBase->Restrict(SIMONLY_RESTRICTION, &polRestrictedSimOnly);
CHRA(hr);
hr = ShowCollection(polRestrictedSimOnly, _T("Only SIM contacts:"));
CHRA(hr);
// Restrict to just the device contacts again and display it - no SIM contacts
hr = polItemsBase->Restrict(NOSIM_RESTRICTION, &polRestrictedNoSim);
CHRA(hr);
hr = ShowCollection(polRestrictedNoSim, _T("No SIM contacts:"));
CHRA(hr);
// Restrict to just the 2nd contacts and display it - Only #2 contacts
hr = polItemsBase->Restrict(PHONE_RESTRICTION_SECOND, &polRestrictedSecondPhone);
CHRA(hr);
hr = ShowCollection(polRestrictedSecondPhone, _T("Only contacts with the second phone number:"));
CHRA(hr);
// Restriction placed over default SIM restriction - only #1 device contact
hr = pFolder->get_Items(&polDefaultCollection2);
CHRA(hr);
hr = polDefaultCollection2->Restrict(PHONE_RESTRICTION_NOTSECOND, &polRestrictedNotSecondPhone);
CHRA(hr);
hr = polRestrictedNotSecondPhone->Restrict(PHONE_RESTRICTION_NOTTHIRD, &polRestrictedOnlyFirstPhone);
CHRA(hr);
hr = ShowCollection(polRestrictedOnlyFirstPhone, _T("Only device contacts with the first phone number:"));
CHRA(hr);
// Include SIMs on already restricted set - Only #1 contacts
hr = polRestrictedOnlyFirstPhone->QueryInterface(IID_IPOlItems3, (void**)&polItemsBase2);
CHRA(hr);
hr = polItemsBase2->IncludeSimContacts();
CHRA(hr);
hr = ShowCollection(polItemsBase2, _T("All contacts with the first phone number:"));
CHRA(hr);
// TODO: Show how SIM phone numbers are formatted upon Save
// TODO: Show how to check for unsupported properties in a SIM contact
// TODO: Show how property getters and setters fail for unsupported properties in SIM contact
// TODO: Remove the contacts that were added here
Error:
RELEASE(polDefaultCollection);
RELEASE(polDefaultCollection2);
RELEASE(polRestrictedSimOnly);
RELEASE(polRestrictedNoSim);
RELEASE(polRestrictedSecondPhone);
RELEASE(polItemsBase);
RELEASE(pFolder);
RELEASE(polApp);
return !SUCCEEDED(hr);
}
/////////////////////////////////////////////////////////////////////////////
// Function: ShowCollection
// polItems - In IPOlItems3 collection to display
// szCollectionTitle - In title for the collection
/////////////////////////////////////////////////////////////////////////////
HRESULT ShowCollection(__in IPOutlookItemCollection * polItems, __in LPWSTR szCollectionTitle)
{
HRESULT hr = S_OK;
int i, cItems;
BSTR szName = NULL, szPhone = NULL;
IDispatch * pDispatch = NULL;
IContact * pContact = NULL;
hr = polItems->get_Count(&cItems);
CHRA(hr);
TRACE(_T("\nSim Contacts: %s"), szCollectionTitle);
for(i = 1; i <= cItems; i++)
{
hr = polItems->Item(i, &pDispatch);
CHRA(hr);
hr = pDispatch->QueryInterface(IID_IContact, (void **)&pContact);
CHRA(hr);
SysFreeString(szName);
hr = pContact->get_FirstName(&szName);
CHRA(hr);
SysFreeString(szPhone);
hr = pContact->get_MobileTelephoneNumber(&szPhone);
CHRA(hr);
long id = 0;
hr = pContact->get_Oid(&id);
CHRA(hr);
TRACE(_T("Sim Contacts: %s - %s\n"), szName, szPhone);
RELEASE(pDispatch);
RELEASE(pContact);
}
Error:
RELEASE(pDispatch);
RELEASE(pContact);
SysFreeString(szName);
SysFreeString(szPhone);
return hr;
}
/////////////////////////////////////////////////////////////////////////////
// Function: CreateContactByType
// polApp - In pOutlook application to use
// olItemType - In type of contact to create (olContactItem or olSimContactItem)
// szName - In name for the contact
// szHomePhone - The home phone for the contact
/////////////////////////////////////////////////////////////////////////////
HRESULT CreateContactByType(__in IPOutlookApp *polApp,
int olItemType,
__in BSTR szName,
__in BSTR szHomePhone)
{
HRESULT hr = S_OK;
IDispatch * pDispatch = NULL;
IContact * pContact = NULL;
hr = polApp->CreateItem(olItemType, &pDispatch);
CHRA(hr);
hr = pDispatch->QueryInterface(IID_IContact, (void **)&pContact);
CHRA(hr);
hr = pContact->put_FirstName(szName);
CHRA(hr);
hr = pContact->put_MobileTelephoneNumber(szHomePhone);
CHRA(hr);
hr = pContact->Save();
CHRA(hr);
Error:
RELEASE(pContact);
RELEASE(pDispatch);
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -