📄 speeddialdisplayitem.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 "SpeedDialDisplayItem.hpp"
#include "PaintHelper.hpp"
#include "ControlDefinitions.h"
#include "Common.hpp"
#include "Resource.h"
#include "Layout.hpp"
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::SpeedDialDisplayItem_t
Ctor
Parameters:
piRecord: The record associated with this item
------------------------------------------------------------------------------*/
SpeedDialDisplayItem_t::SpeedDialDisplayItem_t(
IVoIPCallerInfoRecord *piRecord
)
{
//CComPtr assign does copy
m_cpRecord = piRecord;
}
SpeedDialDisplayItem_t::~SpeedDialDisplayItem_t()
{
}
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::GetRecord
Returns the record this item encapsulates
------------------------------------------------------------------------------*/
HRESULT SpeedDialDisplayItem_t::GetRecord(
IVoIPCallerInfoRecord** ppiRecord
)
{
if (ppiRecord == NULL)
{
ASSERT(FALSE);
return E_POINTER;
}
if (m_cpRecord == NULL)
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
//caller is responsible for releasing this reference
(*ppiRecord) = m_cpRecord;
(*ppiRecord)->AddRef();
return S_OK;
}
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::Draw
Draws the display item
Parameters:
hdc: the device context to draw into
prc: the rectangle to draw into
fSelected: TRUE if this item is selected
Returns (HRESULT): indicating success or failure
------------------------------------------------------------------------------*/
HRESULT SpeedDialDisplayItem_t::Draw(
HDC hdc,
const RECT * prc,
BOOL fSelected,
BOOL fTopItem
)
{
//check params
if (m_cpRecord == NULL)
{
ASSERT(FALSE);
return E_INVALIDARG;
}
RECT rcDraw = *prc;
WCHAR Buffer[MAX_PATH] = L"";
//Draw the background of the item
HRESULT hr = DrawBand(
hdc,
prc,
fSelected,
fTopItem
);
if (FAILED(hr))
{
return hr;
}
PaintHelper_t paint;
hr = paint.Attach(hdc);
if (FAILED(hr))
{
return hr;
}
//create a pen for drawing the vertical line separator
ce::auto_hpen SeparatorPen = ::CreatePen(
PS_SOLID,
0,
PHGetColor(fSelected ? phcDisplayItemSelectedBorderColor : phcDisplayItemBorderColor)
);
if (! SeparatorPen)
{
ASSERT(FALSE);
return CommonUtilities_t::GetErrorFromWin32();
}
//adjust the rectangle for drawing the speed dial index
rcDraw.left += Layout_t::SpeedDialIndexLeftOffset();
rcDraw.right = rcDraw.left + Layout_t::SpeedDialIndexWidth();
GetSubItemText(TextSpeedDial, Buffer, _countof(Buffer));
//validate the buffer
if (! Buffer[0])
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
//initialize the HDC
paint.SetTextColor(
PHGetColor(fSelected ? phcDisplayItemSelectedTextColor : phcDisplayItemTextColor)
);
paint.SetBkMode(TRANSPARENT);
paint.SetFont(
PHGetFont(phfBigNumbersText)
);
paint.SetPen(SeparatorPen);
//Prepare to draw the separator line
POINT rgPts[] =
{
rcDraw.right, rcDraw.top,
rcDraw.right, rcDraw.bottom
};
hr = paint.DrawText(
Buffer,
-1,
&rcDraw,
DT_NOPREFIX | DT_VCENTER | DT_LEFT | DT_SINGLELINE
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
//Draw the vertical line separating the speed dial index from the rest of the text
hr = CommonUtilities_t::ToHR(Polyline(
paint,
rgPts,
_countof(rgPts)
));
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
//prepare the rect for drawing the caller id and number
rcDraw.left = rcDraw.right + Layout_t::SpeedDialMarginWidth();
rcDraw.right = prc->right - Layout_t::SpeedDialMarginWidth();
rcDraw.top += Layout_t::SpeedDialMarginHeight();
rcDraw.bottom -= Layout_t::SpeedDialMarginHeight();
//draw the caller id (at the top)
GetSubItemText(TextName, Buffer, _countof(Buffer));
if (Buffer[0])
{
paint.SetFont(PHGetFont(phfStandardTextBold));
hr = paint.DrawText(
Buffer,
-1,
&rcDraw,
DT_TOP | DT_LEFT | DT_NOPREFIX | DT_SINGLELINE
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
}
//draw the uri at the bottom
GetSubItemText(TextUri, Buffer, _countof(Buffer));
if (Buffer[0])
{
paint.SetFont(PHGetFont(phfStandardText));
hr = paint.DrawText(
Buffer,
-1,
&rcDraw,
DT_BOTTOM | DT_LEFT | DT_SINGLELINE | DT_NOPREFIX
);
if (FAILED(hr))
{
ASSERT(FALSE);
goto exit;
}
}
exit:
//restore the DC
paint.End();
return hr;
}
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::GetHeight
Returns the height of the item
------------------------------------------------------------------------------*/
BOOL SpeedDialDisplayItem_t::GetHeight(
UINT * pfHeight
)
{
PREFAST_ASSERT(pfHeight);
*pfHeight = Layout_t::SpeedDialHeight();
return TRUE;
}
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::GetText
Get the main text of the item (which is the URI in this case)
------------------------------------------------------------------------------*/
HRESULT SpeedDialDisplayItem_t::GetText(
WCHAR *wszText,
INT cchText
)
{
//return the URI text
ce::auto_bstr bstrUri;
if (m_cpRecord == NULL)
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
m_cpRecord->get_URI(&bstrUri);
if (bstrUri == NULL || bstrUri[0] == 0)
{
ASSERT(FALSE);
return E_FAIL;
}
StringCchCopy(wszText, cchText, bstrUri);
return S_OK;
}
/*------------------------------------------------------------------------------
SpeedDialDisplayItem_t::GetSubItemText
Gets a specific text member of the item
------------------------------------------------------------------------------*/
HRESULT SpeedDialDisplayItem_t::GetSubItemText(
int idxSubItem,
WCHAR * wszText,
UINT cchText
)
{
PREFAST_ASSERT(wszText);
HRESULT hr = S_OK;
*wszText = 0;
if (m_cpRecord == NULL)
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
switch (idxSubItem)
{
case TextSpeedDial:
{
//cache the speed dial index
INT idxSpeedDial = VOIP_INVALID_SPEED_DIAL_ENTRY;
m_cpRecord->get_SpeedDialEntry(&idxSpeedDial);
//validate the index
if (idxSpeedDial <= 0)
{
ASSERT(FALSE);
return E_INVALIDARG;
}
//always print the text as 2 digits
_snwprintf(wszText, cchText, L"%02d", idxSpeedDial);
wszText[cchText - 1] = 0;
}
break;
case TextName:
{
ce::auto_bstr bstrName;
//Try the friendly name then voip name
m_cpRecord->get_FriendlyName(&bstrName);
if (bstrName == NULL || bstrName[0] == 0)
{
m_cpRecord->get_VoIPName(&bstrName);
}
if (bstrName == NULL || bstrName[0] == 0)
{
bstrName = SysAllocString(
CommonUtilities_t::LoadString(GlobalData_t::s_ModuleInstance, IDS_LABEL_UNKNOWN_CONTACT)
);
}
if (bstrName == NULL)
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
StringCchCopy(
wszText,
cchText,
bstrName
);
}
break;
case TextUri:
{
//format the uri
ce::auto_bstr bstrUri;
m_cpRecord->get_URI(&bstrUri);
if (bstrUri == NULL || bstrUri[0] == 0)
{
ASSERT(FALSE);
return E_UNEXPECTED;
}
StringCchCopy(
wszText,
cchText,
bstrUri
);
}
break;
default:
hr = S_FALSE;
}
//null-terminate the buffer
wszText[cchText - 1] = 0;
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -