📄 popupmenuitem.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 "PopupMenuItem.hpp"
#include "MenuButton.hpp"
#include "Debug.hpp"
/*------------------------------------------------------------------------------
PopupMenuItem_t::PopupMenuItem_t
Constructor
------------------------------------------------------------------------------*/
PopupMenuItem_t::PopupMenuItem_t(
)
{
TRACE(ZONE_COMMON_CTOR);
m_pText = NULL;
m_Id = 0;
m_IsInputPanelItem = false;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::~PopupMenuItem_t
Destructor
------------------------------------------------------------------------------*/
PopupMenuItem_t::~PopupMenuItem_t(
)
{
TRACE(ZONE_COMMON_CTOR);
if (m_pText)
{
TrackFree(m_pText);
}
//this item is responsible for destroying the flyout menu
m_FlyoutMenu.Destroy();
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::Initialize
Initializes menu item with text and command id
------------------------------------------------------------------------------*/
HRESULT
PopupMenuItem_t::Initialize(
UINT Id,
const WCHAR* pText
)
{
//validate the parameters
if ((Id == 0) || ! pText)
{
ASSERT(FALSE);
return E_INVALIDARG;
}
HRESULT hr = SetText(pText, wcslen(pText));
if (FAILED(hr))
{
return hr;
}
m_Id = Id;
return S_OK;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::IsEnabled
Determines whether this menu item is enabled or not
------------------------------------------------------------------------------*/
bool
PopupMenuItem_t::IsEnabled(
void
)
{
return (m_State & MF_GRAYED) ? false : true;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::IsChecked
Determines whether to display check mark next to this menu item or not
------------------------------------------------------------------------------*/
bool
PopupMenuItem_t::IsChecked(
void
)
{
return (m_State & MF_CHECKED) ? true : false;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::IsInputMenuItem
Determines whether this is the Input menu item
------------------------------------------------------------------------------*/
bool
PopupMenuItem_t::IsInputMenuItem(
void
)
{
return m_IsInputPanelItem;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::SetPopupMenu
Sets popup menu for this menu item
------------------------------------------------------------------------------*/
void
PopupMenuItem_t::SetPopupMenu(
PopupMenu_t& Menu
)
{
m_FlyoutMenu = (HWND)Menu;
return;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::SetText
Sets the text for this menu item
------------------------------------------------------------------------------*/
HRESULT
PopupMenuItem_t::SetText(
const WCHAR* pNewText,
int TextLength
)
{
WCHAR* pText = NULL;
m_IsInputPanelItem = (0 == wcscmp(MenuButtonImpl_t::INPUT_BUTTON, pNewText));
if (m_IsInputPanelItem)
{
goto exit;
}
//add an additional character for the null-terminator
TextLength++;
//allocate the text block and copy the string
pText = reinterpret_cast<WCHAR*>(TrackAlloc(sizeof(WCHAR)*TextLength));
if (!pText)
{
return E_OUTOFMEMORY;
}
//we know there is enough room...
StringCchCopyW(pText, TextLength, pNewText);
exit:
if (m_pText)
{
TrackFree(m_pText);
}
m_pText = pText;
return S_OK;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::SetId
Sets the command id for this menu item
------------------------------------------------------------------------------*/
void
PopupMenuItem_t::SetId(
UINT Id
)
{
m_Id = Id;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::SetState
Sets the state for this menu item (checked/unchecked or enabled/grayed)
------------------------------------------------------------------------------*/
void
PopupMenuItem_t::SetState(
UINT State
)
{
if (State == 0)
{
m_State &= ~(MF_GRAYED | MF_CHECKED);
}
else
{
m_State |= State;
}
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::GetText
Returns text for this menu item
------------------------------------------------------------------------------*/
const WCHAR*
PopupMenuItem_t::GetText(
)
{
return m_pText;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::GetId
Returns command id for this menu item
------------------------------------------------------------------------------*/
UINT
PopupMenuItem_t::GetId(
)
{
return m_Id;
}
/*------------------------------------------------------------------------------
PopupMenuItem_t::GetPopupMenu
Returns popup menu for this menu item
------------------------------------------------------------------------------*/
HWND
PopupMenuItem_t::GetPopupMenu(
)
{
return m_FlyoutMenu;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -