mailset.cpp

来自「WIndows mobile 5.0 pocket pc sdk sample 」· C++ 代码 · 共 992 行 · 第 1/2 页

CPP
992
字号
    RELEASE_OBJ(pDOM)
    RELEASE_OBJ(pMailNode)
    RELEASE_OBJ(pNode)
    RELEASE_OBJ(pSibling)
    RELEASE_OBJ(pNodeMap)

    VariantClear(&vt);

    return hr;
}


///////////////////////////////////////////////////////////////////////////////
//
// EnumerateStore
//
// This function enumerates all the CEMAPI stores and adds them to a combobox
// 
// pSession: IN parameter containing a pointer to the MAPI session
// hwndCombo: IN parameter containing the window handle of the combo box to populate
//
// Returns HRESULT
//

HRESULT EnumerateStores(IMAPISession *pSession, HWND hwndCombo)
{
    HRESULT hr;
    LONG lr;
    IMAPITable *pMsgStoresTable = NULL;
    SRowSet *psrs = NULL;
    IMsgStore *pStore = NULL;
    ULONG cValues = 0;
    SPropValue *pspv = NULL;
    SizedSPropTagArray(1, spta) = { 1, PR_DISPLAY_NAME }; 
    
    // Get the message stores table.
    hr = pSession->GetMsgStoresTable(MAPI_UNICODE, &pMsgStoresTable);
    CHR(hr);

    // Enumerate each row in the table
    while (TRUE)
    {
        hr = pMsgStoresTable->QueryRows (1, 0, &psrs);
        CHR(hr);
        
        // Did we hit the end of the table?
        if (psrs->cRows != 1)
        {
            break;
        }
        
        // Open the message store for this service
        hr = pSession->OpenMsgStore (NULL, 
                                       psrs->aRow[0].lpProps[0].Value.bin.cb, 
                                       (ENTRYID *) psrs->aRow[0].lpProps[0].Value.bin.lpb, 
                                       NULL, 
                                       0, 
                                       &pStore);
        CHR(hr);

        // Read in the name of this store
        hr = pStore->GetProps((SPropTagArray *) &spta, MAPI_UNICODE, &cValues, &pspv);
        CHR(hr);
        
        if ((cValues == 1) && (pspv[0].ulPropTag == spta.aulPropTag[0]))
        {
            // Add this string to the dropdown combobox 
            lr = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM) pspv[0].Value.lpszW);
            if (lr != -1)
            {
                // We own this store now -- set the pointer to NULL so we don't free it
                SendMessage(hwndCombo, CB_SETITEMDATA, lr, (LPARAM) pStore);
                pStore = NULL;
            }
        }

        // Free this memory so we can move to the next item in the table
        MAPIFreeBuffer(pspv);
        FreeProws(psrs);  
        psrs = NULL;
        RELEASE_OBJ(pStore)
    }

    // Set focus on the first item in the combobox
    SendMessage(hwndCombo, CB_SETCURSEL, 0, 0);
    
Error:
    RELEASE_OBJ(pStore)
    RELEASE_OBJ(pMsgStoresTable)
    
    FreeProws(psrs);
    return hr;
}


///////////////////////////////////////////////////////////////////////////////
//
// SetPropertyOnStore
//
// This function sets a property on a store
// 
// psi: IN parameter pointing to a STOREINFO structure
// upv: IN parameter holding the property to set
//
// Returns HRESULT
//

HRESULT SetPropertyOnStore(STOREINFO *psi, __UPV upv)
{
    HRESULT hr;
    SPropValue spv = { 0 };

    // Call SetProps on the appropriate store to set this property
    spv.ulPropTag = psi->ulPropTag;
    spv.Value = upv;
    hr = psi->pStore->SetProps(1, &spv, NULL);
    CHR(hr);

Error:
    return hr;
}

///////////////////////////////////////////////////////////////////////////////
//
// LayoutPropControls
//
// Stretch the controls in the edit property dialog
// 
// hDlg: IN parameter containing the window handle of the dialog
//
// No return value
//

void LayoutPropControls(HWND hDlg)
{
    RECT rc;
    RECT rcDlg;
    HWND hwndControl;

    GetWindowRect(hDlg, &rcDlg);

    // Reposition the label describing the property
    hwndControl = GetDlgItem(hDlg, IDC_DISPLAYNAME);
    GetWindowRect(hwndControl, &rc);
    SetWindowPos(hwndControl, NULL, 0, 0, rcDlg.right - (2 * (rc.left - rcDlg.left)), rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);

    // Reposition the edit box
    hwndControl = GetDlgItem(hDlg, IDC_EDIT);
    GetWindowRect(hwndControl, &rc);
    SetWindowPos(hwndControl, NULL, 0, 0, rcDlg.right - (2 * (rc.left - rcDlg.left)), rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);
}

///////////////////////////////////////////////////////////////////////////////
//
// LayoutStoreControls
//
// Stretch the controls in the main dialog
// 
// hDlg: IN parameter containing the window handle of the dialog
//
// No return value
//

void LayoutStoreControls(HWND hDlg)
{
    RECT rc;
    RECT rcDlg;
    HWND hwndControl;

    GetWindowRect(hDlg, &rcDlg);

    // Reposition the combobox listing the store names
    hwndControl = GetDlgItem(hDlg, IDC_STORECOMBO);
    GetWindowRect(hwndControl, &rc);
    SetWindowPos(hwndControl, NULL, 0, 0, rcDlg.right - (2 * (rc.left - rcDlg.left)), rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);

    // Reposition the combobox listing the properties
    hwndControl = GetDlgItem(hDlg, IDC_COMBO);
    GetWindowRect(hwndControl, &rc);
    SetWindowPos(hwndControl, NULL, 0, 0, rcDlg.right - (2 * (rc.left - rcDlg.left)), rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);

    // Reposition the label describing the properties
    hwndControl = GetDlgItem(hDlg, IDC_DISPLAYNAME);
    GetWindowRect(hwndControl, &rc);
    SetWindowPos(hwndControl, NULL, 0, 0, rcDlg.right - (2 * (rc.left - rcDlg.left)), rc.bottom - rc.top, SWP_NOMOVE | SWP_NOZORDER);
}

///////////////////////////////////////////////////////////////////////////////
//
// SetPropertyDescription
//
// Sets the description of a property based on the property currently chosen in the combobox
// 
// hDlg: IN parameter containing the window handle of the dialog
//
// No return value
//

void SetPropertyDescription(HWND hDlg)
{
    UINT i;
    STOREINFO *psi;
    
    // Find the currently selected item in the combobox
    i = SendMessage(GetDlgItem(hDlg, IDC_COMBO), CB_GETCURSEL, 0, 0);
    if (i >= 0)
    {
        // Get the STOREINFO for this item and change the description being shown
        psi = (STOREINFO *) SendMessage(GetDlgItem(hDlg, IDC_COMBO), CB_GETITEMDATA, i, 0);
        if (psi)
        {
            SetDlgItemText(hDlg, IDC_DISPLAYNAME, psi->pwszDisplayName);
        }
    }
}

///////////////////////////////////////////////////////////////////////////////
//
// EditPropProc
//
// Dialog procedure for the Edit Property dialog
// 
// hDlg: IN parameter containing the window handle of the dialog
// msg: IN parameter containing the message
// wParam: IN parameter containing message-specific value
// lParam: IN parameter containing message-specific value
//
// Returns int
//

int WINAPI EditPropProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    SHINITDLGINFO shidi;
    static STOREINFO *s_psi;
    TCHAR szValue[MAX_PATH];
    __UPV upv = { 0 };
    ULONG rgTags[] = { 1, 0 };
    ULONG cItems = 0;
    LPSPropValue pid = NULL;
    HRESULT hr = S_OK;

    // Switch based on the message
    switch (msg)
    {
        case WM_INITDIALOG:
            // Make this full-screen
            ZeroMemory(&shidi, sizeof(shidi));

            // Size the dialog correctly and add the "Done" button
            // There is no menu associated with this dialog
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);

            // Layout the edit control
            LayoutPropControls(hDlg);
            
            // Set information based on the setting info
            s_psi = (STOREINFO *) lParam;
            CBR(s_psi != NULL);

            // What is the current setting on the store (if any)?
            rgTags[1] = s_psi->ulPropTag;
            hr = s_psi->pStore->GetProps((LPSPropTagArray) rgTags, MAPI_UNICODE, &cItems, &pid);          
            if (SUCCEEDED(hr) && (pid[0].ulPropTag == rgTags[1]))
            {
                // Looks like we have something here -- let's use it
                if (s_psi->vt == vtString)
                {
                    StringCchCopy(szValue, MAX_PATH, pid[0].Value.lpszW);
                }
                else if (s_psi->vt == vtBoolean)
                {
                    StringCchPrintf(szValue, MAX_PATH, TEXT("%d"), pid[0].Value.b);
                }
                else if (s_psi->vt == vtInteger)
                {
                    StringCchPrintf(szValue, MAX_PATH, TEXT("%d"), pid[0].Value.l);
                }
            }
            else
            {
                // No current setting -- use the default
                StringCchCopy(szValue, MAX_PATH, s_psi->pwszDefault);
            }

            MAPIFreeBuffer(pid);

            // Set UI elements
            SetWindowText(GetDlgItem(hDlg, IDC_DISPLAYNAME), s_psi->pwszDisplayName);
            SetWindowText(GetDlgItem(hDlg, IDC_EDIT), szValue);
            SendMessage(GetDlgItem(hDlg, IDC_EDIT), EM_LIMITTEXT, 0, ARRAYSIZE(szValue) - 1);
            SetFocus(GetDlgItem(hDlg, IDC_EDIT));
            break;

        case WM_SIZE:
            // Relayout the controls in this dialog in case the dialog changed (e.g. for a screen rotation)
            LayoutPropControls(hDlg);
            break;
            
        case WM_COMMAND:
            if (LOWORD(wParam) == IDOK)
            {
                // Set a property on the store based on what they chose
                GetWindowText(GetDlgItem(hDlg, IDC_EDIT), szValue, ARRAYSIZE(szValue));

                // Convert to string, boolean, or integer
                if (s_psi->vt == vtString)
                {
                    upv.lpszW = szValue;
                }
                else if (s_psi->vt == vtBoolean)
                {
                    upv.b = (unsigned short int) _ttoi(szValue);
                }
                else if (s_psi->vt == vtInteger)
                {
                    upv.l = _ttoi(szValue);
                }

                // Now set this property in the store
                SetPropertyOnStore(s_psi, upv);

                // And close the dialog
                EndDialog(hDlg, 0);
            }
            break;

        default:
            break;
    }

Error:
    if (FAILED(hr) && (msg == WM_INITDIALOG))
    {
        EndDialog(hDlg, -1);
    }

    return FALSE;
}

///////////////////////////////////////////////////////////////////////////////
//
// MainDlgProc
//
// Dialog procedure for the main dialog proc
// 
// hDlg: IN parameter containing the window handle of the dialog
// msg: IN parameter containing the message
// wParam: IN parameter containing message-specific value
// lParam: IN parameter containing message-specific value
//
// Returns int
//

int WINAPI MainDlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HRESULT hr = S_OK;
    static IMAPISession *pSession;
    SHINITDLGINFO shidi;
    UINT i;
    STOREINFO *psi;
    SHMENUBARINFO cbi = { 0 };
    
    switch (msg)
    {
        case WM_INITDIALOG:
            // Size the dialog correctly
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);

            // Add our menu bar
            cbi.cbSize = sizeof(SHMENUBARINFO);
            cbi.hwndParent = hDlg;
            cbi.nToolBarId = IDR_TOOLBAR_BUTTONS;
            cbi.hInstRes = g_hInst;
            SHCreateMenuBar(&cbi);
            
            // Layout the controls nicely
            LayoutStoreControls(hDlg);
            
            // Logon to MAPI
            hr = MAPILogonEx(0, NULL, NULL, 0, &pSession);
            CHR(hr);
            
            // Enumerate available stores and add them to the combobox
            hr = EnumerateStores(pSession, GetDlgItem(hDlg, IDC_STORECOMBO));
            CHR(hr);
            
            // Now enumerate store-specific XML options that are available
            hr = AddStoreOptions(GetDlgItem(hDlg, IDC_COMBO));
            CHR(hr);

            // Set the label to the currently selected item
            SetPropertyDescription(hDlg);
            break;

        case WM_SIZE:
            // Relayout the controls
            LayoutStoreControls(hDlg);
            break;
            
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                // Hit the ok button
                case IDOK:
                    // Deliberate fall through
                    
                // Hit the done Softkey
                case IDM_DONE:
                    EndDialog(hDlg, 0);
                    break;

                // If the selection in the combobox changed, update the property description underneath it
                case IDC_COMBO:
                    if (HIWORD(wParam) == CBN_SELENDOK)
                    {
                        SetPropertyDescription(hDlg);
                    }
                    break;

                // Bring up a dialog based on the type of the item which is selected in the combobox
                case IDM_CHANGE:
                    // Get the current selection
                    i = SendMessage(GetDlgItem(hDlg, IDC_COMBO), CB_GETCURSEL, 0, 0);
                    if (i >= 0)
                    {
                        psi = (STOREINFO *) SendMessage(GetDlgItem(hDlg, IDC_COMBO), CB_GETITEMDATA, i, 0);
                        if (psi)
                        {
                            // Determine which store is selected
                            i = SendMessage(GetDlgItem(hDlg, IDC_STORECOMBO), CB_GETCURSEL, 0, 0);
                            if (i >= 0)
                            {
                                psi->pStore = (IMsgStore *) SendMessage(GetDlgItem(hDlg, IDC_STORECOMBO), CB_GETITEMDATA, i, 0);
                                if (psi->pStore)
                                {
                                    // Bring up the entry dialog
                                    DialogBoxParam(g_hInst, MAKEINTRESOURCE(IDD_ENTRY), hDlg, EditPropProc, (LPARAM) psi);
                                }
                            }
                        }
                    }
                    break;
            }
            break;

        case WM_DESTROY:
            // Delete stores off the combo box
            FreeStores(GetDlgItem(hDlg, IDC_STORECOMBO));
            FreeStoreOptions(GetDlgItem(hDlg, IDC_COMBO));

            RELEASE_OBJ(pSession)
            break;
    }

Error:
    if (FAILED(hr) && (msg == WM_INITDIALOG))
    {
        EndDialog(hDlg, -1);
    }

    return FALSE;
}

///////////////////////////////////////////////////////////////////////////////
//
// WinMain
//
// Entry point into this executable -- just pops up a dialog that does all the work
// 
// hInstance: IN parameter, the HINSTANCE of this program
// hPrevInstance: IN parameter, the previous HINSTANCE
// lpCmdLine: IN parameter, command-line parameter
// nCmdShow: IN parameter, information on showing the window
//
// Returns int
//

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    CoInitializeEx(NULL, 0);
    
    // Start up the main dialog, please
    g_hInst = hInstance;
    DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, MainDlgProc);

    CoUninitialize();
    return 0;
}

⌨️ 快捷键说明

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