📄 tasktree.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
#include "stdafx.h"
#include <windows.h>
#include <objbase.h>
#include <commctrl.h>
#include <initguid.h> // init POOM COM obj - this way we don't need to statically link
#include <aygshell.h>
#include "resource.h"
#include <pimstore.h>
#include "macros.h"
#define MAX_LOADSTRING 255
#define PARENT 0
#define OID 1
#define SUBJECT 2
#define PROP_ARRAY_SIZE 3
// consts
const LPWSTR cszClassName = TEXT("Dialog");
const LPWSTR cszWndName = TEXT("TaskTree");
const LPWSTR cszParent = TEXT("Parent");
const LPWSTR cszOID = TEXT("OID");
const LPWSTR cszSubject = TEXT("Subject");
const LPWSTR cszNoParent = TEXT("No parent");
// Global Variables
static HINSTANCE g_hInst = 0; // instance handle
static HWND g_hWnd = 0; // handle to the app window
static IPOutlookApp2 *g_polApp = NULL; // Declare and initialize a pointer to the IPOutlookApp interface object
static CEPROPID g_rgPropIDs[PROP_ARRAY_SIZE] = {0}; // an array of property IDs for a Task object
static LPWSTR g_rgNamedProps[] =
{
cszParent,
cszOID,
cszSubject,
}; // an array of named properties for a Task object
// Forward declarations
LRESULT CALLBACK DlgMainProc(HWND, UINT, WPARAM, LPARAM);
// Establishes a POOM session
HRESULT InitPOOM2()
{
HRESULT hr = S_OK;
hr = CoInitializeEx(NULL, 0);
CHR(hr);
hr = CoCreateInstance(CLSID_Application, NULL, CLSCTX_INPROC_SERVER, IID_IPOutlookApp2, (LPVOID *) &g_polApp);
CHR(hr);
Error:
return hr;
}
HRESULT DeInitPOOM2(void)
{
HRESULT hr = S_OK;
CBR(g_polApp != NULL);
// This logoff is for safety. (We may have already logged off, but it shouldn't matter do it again anyways.)
hr = g_polApp->Logoff();
Error:
RELEASE_OBJ(g_polApp);
CoUninitialize();
return hr;
}
void ShowHR(HRESULT hr)
{
TCHAR tszMsg[MAX_LOADSTRING] = {0};
//we are not checking if the string gets truncated
//because hr is a 32-bit value and the array buffer size is 255 8/16-bit chars
StringCchPrintf(tszMsg, ARRAYSIZE(tszMsg), _T("HR = 0x%x"),hr);
MessageBox(g_hWnd, tszMsg, _T("HRESULT"), MB_OK);
}
///////////////////////////////////////////////////////////////////////////////
// InitApp
//
// Initializes the application
//
// Arguments:
// [IN] HINSTANCE hInstance - instance handle
// [IN] WNDCLASS *lpWndClass - a pointer to the window class
//
// Return Values:
// int - 0 on success, -1 if Failed to register class
//
HRESULT InitApp( HINSTANCE hInstance, WNDCLASS *lpWndClass)
{
HRESULT hr = S_OK;
HWND hWnd = FindWindow (cszClassName, cszWndName); // Allow only one instance
// Check parameters
CPR(lpWndClass);
CBR(hInstance != NULL);
if (hWnd)
{
SetForegroundWindow(hWnd);
hr = E_FAIL;
goto Error;
}
lpWndClass->style = 0;
lpWndClass->lpfnWndProc = (WNDPROC) DlgMainProc;
lpWndClass->cbClsExtra = 0;
lpWndClass->cbWndExtra = 0;
lpWndClass->hInstance = hInstance;
lpWndClass->hIcon = NULL;
lpWndClass->hCursor = 0;
lpWndClass->hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
lpWndClass->lpszMenuName = NULL;
lpWndClass->lpszClassName = cszClassName;
if ( RegisterClass (lpWndClass) == 0 )
{
DWORD dwError = GetLastError();
if(dwError != ERROR_CLASS_ALREADY_EXISTS)
{
RETAILMSG(TRUE, (L"%s: Failed to register class. Error = 0x%x", dwError));
hr = E_FAIL;
goto Error;
}
}
Error:
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// InitInstance
//
// Initializes the TASKTREE dialog
//
// Arguments:
// [IN] HINSTANCE hInstance - instance handle
// [IN] int nCmdShow - state in which the dialog should first appear
//
// Return Values:
// BOOL - TRUE on success, FALSE if Failed to initialize
//
BOOL InitInstance(HINSTANCE hInstance)
{
g_hInst = hInstance;// Store instance handle in our global variable
if( DialogBox(g_hInst, MAKEINTRESOURCE(IDD_TASKTREE), NULL, (DLGPROC)DlgMainProc) < 0)
{
return FALSE;
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////
// WinMain
//
// Windows program entry point
//
// Arguments:
// [IN] HINSTANCE hInstance - instance handle of the program
// [IN] HINSTANCE hPrevInstance - always 0
// [IN] LPTSTR lpCmdLine - pointer to a string containing command line parameters passed to the program
// [IN] int nCmdShow - state in which the dialog should first appear
//
// Return Values:
// int - 0 on success, -1 if Failed to register class
//
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASS wc;
int rc = 0;
BOOL bRet;
HRESULT hr = S_OK;
BOOL br = FALSE;
INITCOMMONCONTROLSEX icex; // structure that contains information specifying which control classes are registered.
// Initalize application
hr = InitApp(hInstance, &wc);
if(FAILED(hr))
{
return -1;
}
// Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_BAR_CLASSES|ICC_LISTVIEW_CLASSES|ICC_TAB_CLASSES|ICC_TREEVIEW_CLASSES|ICC_UPDOWN_CLASS;
// This function registers specific common controls classes from the common control DLL.
br = InitCommonControlsEx(&icex);
CBR(br);
// Init POOM
hr = InitPOOM2();
CHR(hr);
// Perform application initialization:
br = InitInstance (hInstance);
CBR(br);
// Main message loop
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
goto Error;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Error:
DeInitPOOM2();
UnregisterClass( wc.lpszClassName, wc.hInstance);
if(FAILED(hr))
{
return -1;
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////
// InitSKMenu
//
// Initializes softkey toolbar
//
// Arguments:
// [IN] HWND hDlg - window handle of the dialog
// [IN] UINT nToolBarId - tooldbar ID
//
// Return Values:
// HRESULT - S_OK if toolbar initialization was successful, S_FAIL otherwise, or E_INVALIDARG
//
HRESULT InitSKMenu(HWND hDlg, UINT nToolBarId)
{
HRESULT hr = S_OK;
SHMENUBARINFO mbi = {0};
SHINITDLGINFO shidi = {0};
BOOL br = FALSE;
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hDlg;
mbi.nToolBarId = nToolBarId;
mbi.hInstRes = g_hInst;
br = SHCreateMenuBar(&mbi);
CBR(br);
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
shidi.hDlg = hDlg;
br = SHInitDialog(&shidi);
CBR(br);
Error:
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// InitProps
//
// Initialize property IDs for a PIM item and create a custom property Parent
//
// Arguments:
// [IN] HWND hDlg - window handle of the dialog
// [IN] UINT nToolBarId - tooldbar ID
//
// Return Values:
// HRESULT - S_OK if toolbar initialization was successful, S_FAIL otherwise
//
HRESULT InitProps(void)
{
HRESULT hr = E_FAIL;
LPCWSTR rgTemp[1] = {cszParent};
CBR(g_polApp != NULL);
//Creates a parent property
hr = g_polApp->GetIDsFromNames(1, rgTemp,
PIM_CREATE | CEVT_I4, g_rgPropIDs);
CHR(hr);
g_rgPropIDs[OID] = PIMPR_OID;
g_rgPropIDs[SUBJECT] = PIMPR_SUBJECT;
Error:
return hr;
}
///////////////////////////////////////////////////////////////////////////////
// GetItemByID
//
// Find an item in a tree by OID
//
// Arguments:
// [IN] HTREEITEM hItem - poinyrt to the item in the tree to start searching from
// [IN] long nID - OID for which to search
//
// Return Values:
// HTREEITEM - a pointer to the item if it was found, otherwise NULL
//
HTREEITEM GetItemByID(HTREEITEM hItem,long nID)
{
HWND hwndTV = GetDlgItem(g_hWnd,IDC_TREE_VIEW);
HTREEITEM hItemFound, hItemChild;
int itemFound = -1;
// If hItem is NULL, start search from root item.
if (hItem == NULL)
{
hItem = (HTREEITEM)SendMessage(hwndTV, TVM_GETNEXTITEM,
TVGN_ROOT, 0);
}
while (hItem != NULL)
{
long nItemID = -1;
TVITEM item;
item.hItem = hItem;
item.mask = TVIF_PARAM | TVIF_CHILDREN; // item attributes we want to retreive
if(FALSE == SendMessage(hwndTV, TVM_GETITEM, 0, (LPARAM)&item)) // retreive item with the specified attribs
{
break;
}
nItemID = item.lParam;
// Did we find it?
if (nID == nItemID)
{
itemFound = 0;
break;
}
// Check whether we have child items.
if (item.cChildren > 0)
{
// Recursively traverse child items.
// hItemChild will never turn out to be NULL
// because we can only hit this code if we know
// for sure that there is at least one child
hItemChild = (HTREEITEM)SendMessage(hwndTV, TVM_GETNEXTITEM,
TVGN_CHILD, (LPARAM)hItem);
hItemFound = GetItemByID(hItemChild, nID);
// Did we find it?
if (hItemFound != NULL)
{
itemFound = 1;
break;
}
}
// Go to next sibling item.
hItem = (HTREEITEM)SendMessage(hwndTV, TVM_GETNEXTITEM,
TVGN_NEXT, (LPARAM)hItem);
}
Exit:
if(itemFound == 0)
{
return hItem;
}
else if(itemFound == 1)
{
return hItemFound;
}
else
{
// Not found.
return NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
// AddItemToTree
//
// Insert item into a tree
//
// Arguments:
// [IN] LPWSTR lpszItem - subject of the item to add
// [IN] long nInsertID - OID of the item to add
// [IN] long nParentID - OID of the item's parent
//
// Return Values:
// HTREEITEM - a pointer to the newly inserted item or NULL
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -