forms.cpp

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

CPP
909
字号
    }

    if (rgetProps)
    {
        MAPIFreeBuffer(rgetProps);
    }

    return hr;
}





// **************************************************************************
//
// CFormProvider - utility member methods
//
// **************************************************************************


// **************************************************************************
// Function Name: Init
// 
// Purpose: Initialize the CFormProvider object
//
// Arguments:
//    IN LPCWSTR pszMsgClass - Message class for this form.
//
// Return Values:
//    S_OK if the initialization completes successfully.
//    MAPI_E_NOT_ENOUGH_MEMORY if a memory allocation fails.
//    Also, any valid return value from MAPILogonEx(), OpenMsgStore(), or
//    GetIDsFromNames() may be returned if one of those calls fails.
//
// Side effects:
//    None.
// 
// Description:  
//    Initializes the CFormProvider object, caching off the message class and
//    optionally initializing the property tags for .
HRESULT WINAPI CFormProvider::Init(LPCWSTR pszMsgClass)
{
    int cchMsgClass = 0;

    // It's possible that the same dll will be used to handle multiple message types,
    // (eg note.dll may be used to read/compose both e-mail and SMS messages). We'll
    // need to know later in the calls to instances of the form provider, what class 
    // of message that instance handles. So store the message class for the message 
    // here, to examine it later where necessary.
    // The only place this is called is from FormFactoryEx(), which has already
    // aborted if pszMsgClass was NULL.
    ASSERT (NULL != pszMsgClass);
    ASSERT (m_pszMessageClass == NULL);

    cchMsgClass = lstrlen(pszMsgClass) + 1;

    if ((m_pszMessageClass = new WCHAR[cchMsgClass]) == NULL)
    {
        DEBUGMSG(1, (TEXT("CFormProvider::Init Out of memory\n")));
        return MAPI_E_NOT_ENOUGH_MEMORY;
    }
    
    StringCchCopy(m_pszMessageClass, cchMsgClass, pszMsgClass);

    return S_OK;
}


// **************************************************************************
// Function Name: CleanUpReadForm
// 
// Purpose: Do cleanup of any already-open read form handles.
//
// Arguments:
//    None.
//
// Return Values:
//    S_OK is always returned
//
// Side effects:
//    All MAPI handles are released.
// 
// Description:  
//    Releases the reference held on the message, message store, and MAPI
//    session.
HRESULT CFormProvider::CleanUpReadForm()
{
    // Release the message and message store
    RELEASE_OBJ(m_pMsg);
    RELEASE_OBJ(m_pMsgStore);
    
    // Release session object last
    RELEASE_OBJ(m_pSession);

    return S_OK;
}



// **************************************************************************
// Function Name: CMsgForm constructor
// 
// Purpose: Constructor initialization for the CMsgForm object.
//
// Arguments:
//    None.
//
// Return Values:
//    None.
//
// Side effects:  
//    None.
// 
// Description:  
//    Initializes the CMsgForm object.  The refcount is initialized to 1
//    to reflect the reference held by the code that created it via a new().
CMsgForm::CMsgForm(
    IMessageFormHostEx* pHost,
    LPMESSAGE pMsg,
    BOOL fCompose) :
    m_cRef(1),
    m_pHost(pHost),
    m_pMsg(pMsg),
    m_wType(0),
    m_pMsgBase(NULL),
    m_pView(NULL),
    m_fCompose(fCompose)

{
    // Inbox created a message for us to fill already.
    if (NULL != m_pMsg)
    {
        m_pMsg->AddRef();
    }
}


// **************************************************************************
// Function Name: ~CMsgForm destructor
// 
// Purpose: Destructor deinitialization for the CMsgForm object.
//
// Arguments:
//    None.
//
// Return Values:
//    None.
//
// Side effects:  
//    None.
// 
// Description:  
//    Deinitializes the CMsgForm object.  All member variables are
//    released and cleared, and all memory allocated is freed.
CMsgForm::~CMsgForm()
{
    RELEASE_OBJ(m_pMsg);
}

// **************************************************************************
// Function Name: QueryInterface
// 
// Purpose: Standard COM QueryInterface method.
//
// Arguments:
//    IN REFIID iid - specifies interface to retrieve
//    IN OUT void** ppvObject - address to store the retrieved interface.
//          Set to NULL if the interface requested is not supported.
//
// Return Values:
//    S_OK if the interface requested is available.
//    MAPI_E_INTERFACE_NOT_SUPPORTED if the requested interface is not
//          supported.  Currently, only IUnknown and IFormProvider are
//          supported.
//
// Side effects:  
//    On a successful query, the refcount will be incremented by 1.
// 
// Description:  
//    Returns an AddRef()-ed reference to either the IUnknown interface or
//    the IFormProvider interface, both of which are merely casts of the this
//    pointer.  Querying for any other interface will result in a NULL
//    ppvObject and a return value of E_NOINTERFACE.
HRESULT CMsgForm::QueryInterface(
    REFIID iid,
    void** ppvObject)
{
    // Any interface on this object is the object pointer
    if( iid == IID_IUnknown )
    {
        *ppvObject = this;
    }
    else if( iid == IID_IMessageFormEx )
    {
        *ppvObject = (IMessageFormEx*) this;
    }
    else
    {
        *ppvObject = NULL;
        return(MAPI_E_INTERFACE_NOT_SUPPORTED);
    }

    // Increment the refcount and return success
    AddRef();
    return S_OK;
}


// **************************************************************************
// Function Name: AddRef
// 
// Purpose: Standard COM AddRef method.
//
// Arguments:
//    None.
//
// Return Values:
//    The new refcount on the object is returned.  This value is meant to be
//    used for diagnostic and testing purposes only.
//
// Side effects:  
//    The refcount will be incremented by 1.
// 
// Description:  
//    Increments the refcount by 1.
ULONG CMsgForm::AddRef()
{
    return InterlockedIncrement( (long*) &m_cRef );
}


// **************************************************************************
// Function Name: Release
// 
// Purpose: Standard COM Release method.
//
// Arguments:
//    None.
//
// Return Values:
//    The new refcount on the object is returned.  This value is meant to be
//    used for diagnostic and testing purposes only.
//
// Side effects:  
//    The refcount will be decremented by 1.  If the resulting refcount is 0,
//    the destructor for the object will be called.
// 
// Description:  
//    Decrements the refcount by 1.
ULONG CMsgForm::Release()
{
    if ( InterlockedDecrement( (long*) &m_cRef ) == 0 )
    {
        delete this;
        return 0;
    }
    return m_cRef;
}


// **************************************************************************
// Function Name: CloseForm
// 
// Purpose: IMessageForm::CloseForm implementation.
//
// Arguments:
//    IN BOOL fSave = FALSE - Not used
//
// Return Values:
//    S_OK is always returned.
//
// Side effects:
//    None.
// 
// Description:  
//    Allows the host or the view to close the form.
HRESULT CMsgForm::CloseForm(
    BOOL fSave)
{
    // Now tell the host that the form is being closed down, in case it wasn't
    // the host that initiated the close. It doesn't matter if we tell the host 
    // and it already knows. If the host had AddRef's this form, then it will
    // Release it beneath this.
    m_pHost->FormClosing(this, m_pMsg, m_eMsgStatus);
    SendMessage(m_hwndParent, WM_CLOSE, 0, 0);
    return S_OK;
}


// **************************************************************************
// Function Name: SetMessage
// 
// Purpose: IMessageForm::SetMessage implementation.
//
// Arguments:
//    IN WORD wType - Form type (Read, Reply, Reply All, Forward).
//    IN LPMESSAGE pMsg - Message to initialize to.
//
// Return Values:
//    S_OK is always returned.
//
// Side effects:
//    None.
// 
// Description:  
//    Allows the host set or change the message for this form.
HRESULT CMsgForm::SetMessage(
    WORD wType,
    LPMESSAGE pMsg)
{
    HRESULT hr = S_OK;

    // Type is Read, Reply, Reply All, Forward.
    m_wType = wType;

    if (NULL != pMsg)
    {
        pMsg->AddRef();
    }

    // If we have a message already loaded in the form, we're done with it now.
    RELEASE_OBJ(m_pMsgBase);
    
    // Now store the new message to be loaded in the form.
    m_pMsgBase = pMsg;

    // Pass the notification on up to the view for it to update as well.
    if (m_pView)
    {
        m_pView->SetMessage(wType, pMsg);
    }

    return hr;
}


// **************************************************************************
// Function Name: PreTranslateMsg
// 
// Purpose: IMessageForm::PreTranslateMsg implementation.
//
// Arguments:
//    IN MSG* pMsg - Message from the message pump.
//    IN OUT BOOL* pfProcessed - Was the message processed?
//
// Return Values:
//    S_OK is always returned.
//
// Side effects:
//    None.
// 
// Description:  
//    Allows the form to process window messages pumped by the UI.  Currently
//    al messages are just passed on to the view for it to process.
HRESULT CMsgForm::PreTranslateMsg(
    MSG* pMsg,
    BOOL* pfProcessed)
{
    *pfProcessed = m_pView && m_pView->PreTranslateMsg(pMsg);

    return S_OK;
}

HRESULT CMsgForm::DoAction(FORMACTIONS actForm, DWORD dwValue, BOOL* pfProcessed)
{
    switch (actForm)
    {
        case FORMACT_POSTPONE:
        {
            CloseForm();
            break;
        }

        case FORMACT_DELETE:
        {
            m_pHost->DoAction(this, m_pMsg, IMessageFormHostEx::HOSTACT_DELETE, NULL);
            break;
        }

        case FORMACT_REPLY:
        {
            m_pHost->DoAction(this, m_pMsg, IMessageFormHostEx::HOSTACT_FORWARD, NULL);
            break;
        }
    }

    return S_OK;    
}

HRESULT CMsgForm::GetMenuCapabilities(DWORD dwFlags, DWORD* pdwEnable)
{
    *pdwEnable = (MESSAGEFORMHOST2_CMDBARCAP_REPLY & dwFlags); 
    return S_OK;
}


// **************************************************************************
//
// CMsgForm - utility member methods
//
// **************************************************************************


// **************************************************************************
// Function Name: Init
// 
// Purpose: Initialize the CMsgForm object
//
// Arguments:
//    IN CFormProvider* pFormProvider - Form provider that created the form.
//    IN HWND hwndForm - HWND of the form frame
//    IN OUT HWND* phwndBody - HWND of the form-created body
//    IN MESSAGEFIELDS* pMsgFields - Message field flags
//
// Return Values:
//    S_OK if the initialization completes successfully.
//    MAPI_E_NOT_ENOUGH_MEMORY if a memory allocation fails.
//
// Side effects:
//    None.
// 
// Description:  
//    Initializes the CMsgForm object, caching off the message class and
//    optionally initializing the property tags for .
HRESULT CMsgForm::Init(
    CFormProvider* pFormProvider,
    HWND            hwndForm,   // HWND of the form frame
    HWND*           phwndBody,  // HWND of the form-created body
    MESSAGEFIELDS*  pMsgFields)
{
    HRESULT hr = S_OK;

    m_eMsgStatus = IMessageFormHostEx::CMS_NOT_AVAILABLE;
    m_hwndParent = hwndForm;

    // Create the view associated with this form.
    m_pView = new CMsgView(pFormProvider, this, m_pMsg, m_pMsgBase, m_fCompose);
    CPR(m_pView);
    
    // Create the new view window, and prefill its fields if necessary.
    // Note that if it has already been created, the view will simply
    // show itself.
    CBR(m_pView->Create(hwndForm, phwndBody));

    hr = m_pView->Init(pMsgFields);
    CHR(hr);

Error:
    // Everything worked ok.
    return hr;
}

⌨️ 快捷键说明

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