⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 labeledinfodisplayitem.cpp

📁 一个WinCE6。0下的IP phone的源代码
💻 CPP
字号:
//
// 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.
//
#include "LabeledInfoDisplayItem.h"
#include "common.hpp"
#include "PaintHelper.hpp"
#include "Layout.hpp"

const UINT c_siLabel = 0;
const UINT c_siData  = 1;

//Value meaning the height hasn't been determined yet (it still needs to be calculated)
#define UNASSIGNED_HEIGHT (UINT)-1

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::CLabeledInfoDisplayItem
    
    Ctor
------------------------------------------------------------------------------*/
CLabeledInfoDisplayItem::CLabeledInfoDisplayItem()
{
    //MemTrackAlloc();
    
    m_dwCookie = 0;
    m_nHeight  = UNASSIGNED_HEIGHT;
    m_fSupportMultipleLines = FALSE; 
}

CLabeledInfoDisplayItem::~CLabeledInfoDisplayItem()
{
    //MemTrackFree();
}


/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::SetCookie
    
    Set a cookie associated with this item
------------------------------------------------------------------------------*/
HRESULT CLabeledInfoDisplayItem::SetCookie(
    DWORD dwCookie
    )
{
    m_dwCookie = dwCookie;
    return S_OK;
}

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::GetCookie
    
    Get the cookie associated with this item
------------------------------------------------------------------------------*/
DWORD CLabeledInfoDisplayItem::GetCookie()
{
    return m_dwCookie;
}

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::SetDataString
    
    Set the data string associated with this item
------------------------------------------------------------------------------*/
HRESULT CLabeledInfoDisplayItem::SetDataString(
    const WCHAR * c_wszData
    )
{
    if (c_wszData == NULL)
    {
        ASSERT(FALSE);
        return E_POINTER;
    }

    if (! m_wstrData.assign(c_wszData) )
    {
        return E_OUTOFMEMORY;
    }

    return S_OK;
}

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::SetLabel
    
    Set the label associated with this item
------------------------------------------------------------------------------*/
HRESULT CLabeledInfoDisplayItem::SetLabel(
    const WCHAR * c_wszLabel
    )
{
    if (c_wszLabel == NULL)
    {
        ASSERT(FALSE);
        return E_POINTER;
    }

    if (! m_wstrLabel.assign(c_wszLabel) )
    {
        return E_OUTOFMEMORY;
    }

    return S_OK;
}

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::SetMultipleLines
    
    Set the multiple lines flag associated with this item
------------------------------------------------------------------------------*/
HRESULT CLabeledInfoDisplayItem::SetMultipleLines(
    BOOL fSupportMultipleLines
    )
{
    m_fSupportMultipleLines = fSupportMultipleLines; 
    return S_OK;
}


/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::GetHeight
    
    Get the height of the item. Gets the height by calling
    DrawText with DT_CALCRECT specified
    
    Parameters:
        pfHeight: OUT - the height to return
    
    Returns (BOOL): Whether or not we were able to get the height
------------------------------------------------------------------------------*/
BOOL CLabeledInfoDisplayItem::GetHeight(
    UINT * pfHeight
    )
{
    PREFAST_ASSERT(pfHeight);

    HRESULT       hr      = S_OK;
    ce::auto_hdc  hdcDraw;
    RECT          rcDraw  = {0};
    PaintHelper_t paint;

    //if we previously got the height - exit and return the old value
    if (m_nHeight != UNASSIGNED_HEIGHT)
    {
        goto exit;
    }

    //We need text to determine height
    if (m_wstrData[0] == L'\0')
    {
        return FALSE;
    }

    //Create a DC to fake drawing text into
    hdcDraw = CreateCompatibleDC(NULL);
    hr = CommonUtilities_t::ToHR(hdcDraw != NULL);

    if (FAILED(hr))
    {
        goto error;
    }

    hr = paint.Attach(hdcDraw);
    if (FAILED(hr))
    {
        return hr;
    }
    paint.SetFont(PHGetFont(phfStandardText));
    
    rcDraw.right = Layout_t::LabeledInfoDisplayDataWidth();

    //Calculate the rect height using DT_CALCRECT
    hr = paint.DrawText(
        (WCHAR*)(const WCHAR *)m_wstrData, 
        -1,
        &rcDraw,
        DT_LEFT | DT_NOPREFIX | DT_CALCRECT | ((m_fSupportMultipleLines) ? DT_WORDBREAK|DT_EDITCONTROL : DT_SINGLELINE)
        );    
    if (FAILED(hr))
    {
        goto error;
    }    

    //Remember the new height (+ margin)
    ASSERT(rcDraw.bottom > 0);
    m_nHeight = rcDraw.bottom + 2*Layout_t::LabeledInfoDisplayMarginHeight();
    
exit:
    paint.End();
    *pfHeight = m_nHeight;
    return TRUE;

error:
    ASSERT(FALSE);
    return FALSE;
}

/*------------------------------------------------------------------------------
    CLabeledInfoDisplayItem::Draw
    
    Draws the display item
------------------------------------------------------------------------------*/
HRESULT CLabeledInfoDisplayItem::Draw(
    HDC hdc, 
    const RECT * prc, 
    BOOL fSelected,
    BOOL fTopItem
    )
{
    PREFAST_ASSERT(prc);
    
    HRESULT  hr     = S_OK;
    RECT     rcDraw = *prc;
    
    UINT  dtFlags  = 0;

    //Update the rect to reflect the margins

    rcDraw.top    += Layout_t::LabeledInfoDisplayMarginHeight();
    rcDraw.bottom -= Layout_t::LabeledInfoDisplayMarginHeight();
    rcDraw.left   += Layout_t::LabeledInfoDisplayMarginWidth();
    rcDraw.right  -= Layout_t::LabeledInfoDisplayMarginWidth();
    
    //Draw the band around the item
    hr = DrawBand(
        hdc, 
        prc, 
        fSelected, 
        fTopItem
        );

    PaintHelper_t   paint;
    hr = paint.Attach(hdc);
    if (FAILED(hr))
    {
        return hr;
    }

    //if we have text to draw, draw it!
    if (m_wstrLabel[0])
    {        
        paint.SetFont(
            PHGetFont(phfStandardText)
            );        
        
        paint.SetBkMode(TRANSPARENT);        

        paint.SetTextColor(
            PHGetColor(fSelected ? phcDisplayItemSelectedTextColor : phcDisplayItemTextColor)
            );
        
        rcDraw.right -= Layout_t::LabeledInfoDisplayDataWidth();
        
        hr = paint.DrawText(
            (const WCHAR *)m_wstrLabel, 
            -1, 
            &rcDraw,
            DT_TOP | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE
            );
        
        if (FAILED(hr))
        {
            goto exit;
        }
    }
        
    if (m_wstrData[0])
    {
        //Set up the data rect
        rcDraw.left  = prc->right - Layout_t::LabeledInfoDisplayDataWidth();
        rcDraw.right = prc->right;

        dtFlags = DT_NOPREFIX | DT_LEFT;
        if (m_fSupportMultipleLines)
        {
            dtFlags |= DT_WORDBREAK | DT_EDITCONTROL;
        }
        else
        {
            dtFlags |= DT_TOP | DT_SINGLELINE | DT_END_ELLIPSIS;
        }        

        hr = paint.DrawText(
            (WCHAR*)(const WCHAR *)m_wstrData, 
            -1,
            &rcDraw,
            dtFlags
            );
        
        if (FAILED(hr))
        {
            goto exit;
        }
    }

    exit:
        
    paint.End();
    return hr;
}

HRESULT CLabeledInfoDisplayItem::GetText(
    WCHAR * wszBuffer, 
    INT cchBuffer
    )
{
    return GetSubItemText(c_siData, wszBuffer, cchBuffer);
}

HRESULT CLabeledInfoDisplayItem::GetSubItemText(
    int   idxSubItem, 
    WCHAR* pBuffer, 
    UINT   BufferLen
    )
{
    switch (idxSubItem)
    {
    case c_siLabel:
        
        StringCchCopy (
            pBuffer, 
            BufferLen, 
            m_wstrLabel
            );
        return S_OK;

    case c_siData:
        
        StringCchCopy (
            pBuffer, 
            BufferLen, 
            m_wstrData
            );

        return S_OK;

    default:
        return S_FALSE;
    }
}

BOOL CLabeledInfoDisplayItem::NeedsToolTip(
    RECT *prc
    )
{
    if (! prc)
    {
        ASSERT(FALSE);
        return FALSE;
    }

    //multi-line does not get tool tips
    if (m_fSupportMultipleLines)
    {   
        return FALSE;
    }
    
    WCHAR Buffer[100]  = L"";
    GetText(Buffer, _countof(Buffer));
    if (! Buffer[0])
    {
        return FALSE;
    }

    RECT rcDraw = {0};
    HRESULT hr = PaintHelper_t::CalculateTextDimensions(
        Buffer, 
        -1, 
        PHGetFont(phfStandardText), 
        &rcDraw, 
        DT_TOP | DT_LEFT | DT_SINGLELINE | DT_NOPREFIX
        );
    if (FAILED(hr))
    {
        ASSERT(FALSE);
        return FALSE;
    }

    //add space for the label
    rcDraw.right += Layout_t::LabeledInfoDisplayDataWidth();    
    
    return (RECTWIDTH(rcDraw) > RECTWIDTH(*prc));
}

⌨️ 快捷键说明

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