📄 labelededitdisplayitem.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 "LabeledEditDisplayItem.hpp"
#include "PaintHelper.hpp"
#include "Common.hpp"
#include "Layout.hpp"
#include <winuserm.h>
#include "Input.hpp"
EXTERN_C
HRESULT
WINAPI
PHCreateLabeledEditDisplayItem(
const WCHAR* pLabelText,
DWORD EditStyle,
int EditTextLimit,
IVoIPDisplayItem** ppItem,
int DefaultLabelWidth,
UINT DefaultInputMode
)
{
if (!ppItem)
{
ASSERT(0);
return E_INVALIDARG;
}
*ppItem = NULL;
if (DefaultInputMode == (UINT)-1)
{
DefaultInputMode = EIM_NUMBERS;
}
LabeledEditDisplayItem_t* pNewItem;
pNewItem = new LabeledEditDisplayItem_t(
EditStyle,
EditTextLimit,
DefaultLabelWidth,
DefaultInputMode
);
if (!pNewItem)
{
return E_OUTOFMEMORY;
}
HRESULT hr = pNewItem->SetLabelText(pLabelText);
if (SUCCEEDED(hr))
{
*ppItem = pNewItem;
}
else
{
delete pNewItem;
}
return hr;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::LabeledEditDisplayItem_t
Constructor
------------------------------------------------------------------------------*/
LabeledEditDisplayItem_t ::LabeledEditDisplayItem_t (
DWORD EditStyle,
int EditTextLimit,
int DefaultLabelWidth,
UINT DefaultInputMode
)
{
m_LabelWidth = DefaultLabelWidth;
m_EditStyle = EditStyle;
m_EditTextLimit = EditTextLimit;
m_DefaultInputMode = DefaultInputMode;
m_RemovingEditControl = false;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::~LabeledEditDisplayItem_t
Destructor
------------------------------------------------------------------------------*/
LabeledEditDisplayItem_t ::~LabeledEditDisplayItem_t ()
{
}
HRESULT
LabeledEditDisplayItem_t ::QueryInterface(
REFIID riid,
__deref_out void** ppvObject
)
{
if (!ppvObject)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
if (riid == IID_IVoIPDisplayControl)
{
*ppvObject = static_cast<IVoIPDisplayControl*>(this);
}
else if (riid == IID_IVoIPLabeledEditDisplayItem)
{
*ppvObject = static_cast<IVoIPLabeledEditDisplayItem*>(this);
}
else
{
*ppvObject = NULL;
hr = E_NOTIMPL;
}
return hr;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::Draw
Draws the item
------------------------------------------------------------------------------*/
HRESULT LabeledEditDisplayItem_t::Draw(
HDC hdc,
const RECT* pRectangle,
BOOL IsSelected,
BOOL IsTopItem
)
{
if (!pRectangle)
{
return E_INVALIDARG;
}
HRESULT hr = S_OK;
PaintHelper_t paint;
RECT DrawRectangle = *pRectangle;
hr = paint.Attach(hdc);
if (FAILED(hr))
{
return hr;
}
//Draw the band around the item
DrawBand(paint, pRectangle, IsSelected, IsTopItem);
//if there is no text, don't do anymore drawing
ASSERT(!m_LabelText.empty());
if (m_LabelText.length())
{
//paint.SetFont(LoadCachedFont(IDS_FONT_LABELEDIT_LABEL));
paint.SetBkMode(TRANSPARENT);
paint.SetFont(Fonts_t::InformationText());
paint.SetTextColor(
IsSelected ?
Colors_t::DisplayItemSelectedTextColor() :
Colors_t::DisplayItemTextColor()
);
int cxTotal = (m_LabelWidth - 2 - Layout_t::LabeledItemToControlMargin());
UINT DrawTextFlags = DT_LEFT | DT_NOPREFIX;
RECT CalculateRect = {0};
if ((m_LabelWidth == -1) || (
SUCCEEDED(
PaintHelper_t::CalculateTextDimensions(
m_LabelText,
m_LabelText.length(),
Fonts_t::InformationText(),
&CalculateRect,
DT_LEFT | DT_SINGLELINE | DT_NOPREFIX
)
) &&
(RECTWIDTH(CalculateRect) <= cxTotal) &&
(wcschr(m_LabelText, L'\n') == NULL)
))
{
DrawTextFlags |= DT_VCENTER | DT_SINGLELINE;
}
else
{
DrawTextFlags |= DT_WORDBREAK;
DrawRectangle.top += 8;
}
DrawRectangle.left += Layout_t::LabeledItemToControlMargin();
if (m_LabelWidth != -1)
{
DrawRectangle.right = DrawRectangle.left + cxTotal;
}
hr = paint.DrawText(
m_LabelText,
m_LabelText.length(),
&DrawRectangle,
DrawTextFlags
);
}
paint.End();
return hr;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::SetText
Sets the text of the internal edit control
Parameters:
c_wszText: The new text of the control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t ::SetText(
const WCHAR* pBuffer
)
{
if ((HWND)m_Edit)
{
return SetWindowText(m_Edit, pBuffer) ?
S_OK :
CommonUtilities_t::GetErrorFromWin32();
}
return m_DefaultEditText.assign(pBuffer) ? S_OK : E_OUTOFMEMORY;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::GetText
Gets the text from the internal control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t ::GetText(
WCHAR* pBuffer,
int cchBuffer
)
{
return GetSubItemText(SubItemEdit, pBuffer, cchBuffer);
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::GetSubItemText
Gets the text for the appropriate subitem
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::GetSubItemText(
int Index,
WCHAR* pBuffer,
UINT cchBuffer
)
{
if (!pBuffer || (cchBuffer <= 0))
{
ASSERT(0);
return E_INVALIDARG;
}
HRESULT hr;
switch (Index)
{
case SubItemLabel:
hr = StringCchCopyW(pBuffer, cchBuffer, m_LabelText);
break;
case SubItemEdit:
if ((HWND)m_Edit)
{
pBuffer[0] = 0;
GetWindowText(m_Edit, pBuffer, cchBuffer);
hr = S_OK;
}
else
{
hr = StringCchCopyW(pBuffer, cchBuffer, m_DefaultEditText);
}
break;
default:
pBuffer[0] = 0;
hr = S_FALSE;
break;
}
return hr;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::NeedsToolTip
Determines if it is needs Tooltip
------------------------------------------------------------------------------*/
BOOL
LabeledEditDisplayItem_t::NeedsToolTip(
RECT* pRectangle
)
{
if (!pRectangle)
{
ASSERT(0);
return FALSE;
}
if (!(HWND)m_Edit ||
!(GetWindowLong(m_Edit, GWL_STYLE) & ES_READONLY))
{
return FALSE;
}
WCHAR TextBuffer[MAX_PATH];
if (GetWindowText(m_Edit, TextBuffer, _countof(TextBuffer)) < 1)
{
return FALSE;
}
RECT EditRect;
GetWindowRect(m_Edit, &EditRect);
RECT CalculateRect = {0};
PaintHelper_t::CalculateTextDimensions(
TextBuffer,
-1,
Fonts_t::InformationText(),
&CalculateRect,
DT_TOP | DT_LEFT | DT_NOPREFIX
);
return (RECTWIDTH(CalculateRect) >= RECTWIDTH(EditRect));
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::RemoveControls
Removes the internal edit control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t ::RemoveControls(
void
)
{
if ((HWND)m_Edit)
{
m_RemovingEditControl = true;
m_Edit.Destroy();
}
return S_OK;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::ShowControls
Shows the internal edit control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::ShowControls(
BOOL Visible
)
{
if (!(HWND)m_Edit || m_RemovingEditControl)
{
return S_FALSE;
}
ShowWindow(m_Edit, Visible ? SW_SHOW : SW_HIDE);
return S_OK;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::PositionControls
Repositions the internal edit control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::PositionControls(
HWND VirtualListBox,
RECT* pRectangle
)
{
if (!pRectangle)
{
return E_INVALIDARG;
}
HRESULT hr;
RECT CalculateRect = *pRectangle;
hr = PaintHelper_t::CalculateTextDimensions(
m_LabelText,
m_LabelText.length(),
Fonts_t::InformationText(),
&CalculateRect,
DT_LEFT | DT_WORDBREAK | DT_NOPREFIX
);
if (FAILED(hr))
{
return hr;
}
CalculateRect.left = CalculateRect.right + Layout_t::LabeledItemHorzMargin();
CalculateRect.right = pRectangle->right - Layout_t::LabeledItemHorzMargin();
CalculateRect.top = pRectangle->top + Layout_t::LabeledItemVerMargin();
CalculateRect.bottom = pRectangle->bottom - Layout_t::LabeledItemVerMargin();
if (m_LabelWidth != -1)
{
CalculateRect.left = pRectangle->left + m_LabelWidth;
}
if (!(HWND)m_Edit)
{
hr = m_Edit.Create(
WNDCLASS_EDIT,
NULL,
WS_CHILD | ES_AUTOHSCROLL | m_EditStyle,
0,
VirtualListBox,
CalculateRect.left,
CalculateRect.top,
RECTWIDTH(CalculateRect),
RECTHEIGHT(CalculateRect),
reinterpret_cast<void*>(m_DefaultInputMode)
);
if ((HWND)m_Edit)
{
if (m_EditTextLimit > 0)
{
SendMessage(m_Edit, EM_SETLIMITTEXT, m_EditTextLimit, 0);
}
m_Edit.SetCallbackPointer((INT_PTR)this);
SetWindowText(m_Edit, m_DefaultEditText);
Input_SetInputMode(
m_Edit,
m_DefaultInputMode
);
}
}
else
{
hr = MoveWindow(
m_Edit,
CalculateRect.left,
CalculateRect.top,
RECTWIDTH(CalculateRect),
RECTHEIGHT(CalculateRect),
TRUE
) ? S_OK : CommonUtilities_t::GetErrorFromWin32();
}
return hr;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::SetFocus
Sets the focus to the internal edit control (if appropriate)
------------------------------------------------------------------------------*/
BOOL
LabeledEditDisplayItem_t::SetFocus(
void
)
{
if (!(HWND)m_Edit ||
m_RemovingEditControl ||
(GetWindowLong(m_Edit, GWL_STYLE) & ES_READONLY))
{
return FALSE;
}
::SetFocus(m_Edit);
return TRUE;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::SetLabelText
Sets the Label text for the current item
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::SetLabelText(
const WCHAR* pBuffer
)
{
if (!pBuffer)
{
ASSERT(0);
return E_INVALIDARG;
}
return m_LabelText.assign(pBuffer) ? S_OK : E_OUTOFMEMORY;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::SetNumbersOnly
Sets the Number only style of the internal edit control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::SetNumbersOnly(
BOOL NumbersOnly
)
{
bool IsEditControl = ((HWND)m_Edit != NULL);
if (IsEditControl)
{
m_EditStyle = GetWindowLong(m_Edit, GWL_STYLE);
}
if (NumbersOnly)
{
m_EditStyle |= ES_NUMBER;
}
else
{
m_EditStyle &= ~ES_NUMBER;
}
if (!IsEditControl || m_RemovingEditControl)
{
return S_FALSE;
}
SetWindowLong(m_Edit, GWL_STYLE, m_EditStyle);
return S_OK;
}
/*------------------------------------------------------------------------------
LabeledEditDisplayItem_t::SetReadOnly
Sets the read only style of the internal edit control
------------------------------------------------------------------------------*/
HRESULT
LabeledEditDisplayItem_t::SetReadOnly(
BOOL ReadOnly
)
{
if (!(HWND)m_Edit)
{
if (ReadOnly)
{
m_EditStyle |= ES_READONLY;
}
else
{
m_EditStyle &= ~ES_READONLY;
}
return S_FALSE;
}
if (m_RemovingEditControl)
{
return S_FALSE;
}
SendMessage(m_Edit, EM_SETREADONLY, ReadOnly, 0);
return S_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -