📄 atluser.h
字号:
// Windows Template Library - WTL version 8.0
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// This file is a part of the Windows Template Library.
// The use and distribution terms for this software are covered by the
// Microsoft Permissive License (Ms-PL) which can be found in the file
// Ms-PL.txt at the root of this distribution.
#ifndef __ATLUSER_H__
#define __ATLUSER_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLAPP_H__
#error atluser.h requires atlapp.h to be included first
#endif
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CMenuItemInfo
// CMenuT<t_bManaged>
// CAcceleratorT<t_bManaged>
// CIconT<t_bManaged>
// CCursorT<t_bManaged>
// CResource
//
// Global functions:
// AtlMessageBox()
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// AtlMessageBox - accepts both memory and resource based strings
inline int AtlMessageBox(HWND hWndOwner, ATL::_U_STRINGorID message, ATL::_U_STRINGorID title = (LPCTSTR)NULL, UINT uType = MB_OK | MB_ICONINFORMATION)
{
ATLASSERT(hWndOwner == NULL || ::IsWindow(hWndOwner));
LPTSTR lpstrMessage = NULL;
if(IS_INTRESOURCE(message.m_lpstr))
{
for(int nLen = 256; ; nLen *= 2)
{
ATLTRY(lpstrMessage = new TCHAR[nLen]);
if(lpstrMessage == NULL)
{
ATLASSERT(FALSE);
return 0;
}
int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(message.m_lpstr), lpstrMessage, nLen);
if(nRes < nLen - 1)
break;
delete [] lpstrMessage;
lpstrMessage = NULL;
}
message.m_lpstr = lpstrMessage;
}
LPTSTR lpstrTitle = NULL;
if(IS_INTRESOURCE(title.m_lpstr) && LOWORD(title.m_lpstr) != 0)
{
for(int nLen = 256; ; nLen *= 2)
{
ATLTRY(lpstrTitle = new TCHAR[nLen]);
if(lpstrTitle == NULL)
{
ATLASSERT(FALSE);
return 0;
}
int nRes = ::LoadString(ModuleHelper::GetResourceInstance(), LOWORD(title.m_lpstr), lpstrTitle, nLen);
if(nRes < nLen - 1)
break;
delete [] lpstrTitle;
lpstrTitle = NULL;
}
title.m_lpstr = lpstrTitle;
}
int nRet = ::MessageBox(hWndOwner, message.m_lpstr, title.m_lpstr, uType);
delete [] lpstrMessage;
delete [] lpstrTitle;
return nRet;
}
///////////////////////////////////////////////////////////////////////////////
// CMenu
#if (WINVER >= 0x0500)
#ifndef MII_SIZEOF_STRUCT
#define MII_SIZEOF_STRUCT(structname, member) (((int)((LPBYTE)(&((structname*)0)->member) - ((LPBYTE)((structname*)0)))) + sizeof(((structname*)0)->member))
#endif
#define MENUITEMINFO_SIZE_VERSION_400A MII_SIZEOF_STRUCT(MENUITEMINFOA, cch)
#define MENUITEMINFO_SIZE_VERSION_400W MII_SIZEOF_STRUCT(MENUITEMINFOW, cch)
#ifdef UNICODE
#define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400W
#else
#define MENUITEMINFO_SIZE_VERSION_400 MENUITEMINFO_SIZE_VERSION_400A
#endif // !UNICODE
#endif // (WINVER >= 0x0500)
class CMenuItemInfo : public MENUITEMINFO
{
public:
CMenuItemInfo()
{
memset(this, 0, sizeof(MENUITEMINFO));
cbSize = sizeof(MENUITEMINFO);
#if (WINVER >= 0x0500)
// adjust struct size if running on older version of Windows
if(AtlIsOldWindows())
{
ATLASSERT(cbSize > MENUITEMINFO_SIZE_VERSION_400); // must be
cbSize = MENUITEMINFO_SIZE_VERSION_400;
}
#endif // (WINVER >= 0x0500)
}
};
// forward declarations
template <bool t_bManaged> class CMenuT;
typedef CMenuT<false> CMenuHandle;
typedef CMenuT<true> CMenu;
template <bool t_bManaged>
class CMenuT
{
public:
// Data members
HMENU m_hMenu;
// Constructor/destructor/operators
CMenuT(HMENU hMenu = NULL) : m_hMenu(hMenu)
{ }
~CMenuT()
{
if(t_bManaged && m_hMenu != NULL)
DestroyMenu();
}
CMenuT<t_bManaged>& operator =(HMENU hMenu)
{
Attach(hMenu);
return *this;
}
void Attach(HMENU hMenuNew)
{
ATLASSERT(::IsMenu(hMenuNew));
if(t_bManaged && m_hMenu != NULL && m_hMenu != hMenuNew)
::DestroyMenu(m_hMenu);
m_hMenu = hMenuNew;
}
HMENU Detach()
{
HMENU hMenu = m_hMenu;
m_hMenu = NULL;
return hMenu;
}
operator HMENU() const { return m_hMenu; }
bool IsNull() const { return (m_hMenu == NULL); }
BOOL IsMenu() const
{
return ::IsMenu(m_hMenu);
}
// Create/destroy methods
BOOL CreateMenu()
{
ATLASSERT(m_hMenu == NULL);
m_hMenu = ::CreateMenu();
return (m_hMenu != NULL) ? TRUE : FALSE;
}
BOOL CreatePopupMenu()
{
ATLASSERT(m_hMenu == NULL);
m_hMenu = ::CreatePopupMenu();
return (m_hMenu != NULL) ? TRUE : FALSE;
}
BOOL LoadMenu(ATL::_U_STRINGorID menu)
{
ATLASSERT(m_hMenu == NULL);
m_hMenu = ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
return (m_hMenu != NULL) ? TRUE : FALSE;
}
#ifndef _WIN32_WCE
BOOL LoadMenuIndirect(const void* lpMenuTemplate)
{
ATLASSERT(m_hMenu == NULL);
m_hMenu = ::LoadMenuIndirect(lpMenuTemplate);
return (m_hMenu != NULL) ? TRUE : FALSE;
}
#endif // !_WIN32_WCE
BOOL DestroyMenu()
{
if (m_hMenu == NULL)
return FALSE;
BOOL bRet = ::DestroyMenu(m_hMenu);
if(bRet)
m_hMenu = NULL;
return bRet;
}
// Menu Operations
BOOL DeleteMenu(UINT nPosition, UINT nFlags)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::DeleteMenu(m_hMenu, nPosition, nFlags);
}
BOOL TrackPopupMenu(UINT nFlags, int x, int y, HWND hWnd, LPCRECT lpRect = NULL)
{
ATLASSERT(::IsMenu(m_hMenu));
#ifndef _WIN32_WCE
#if (WINVER >= 0x0500)
x = _FixTrackMenuPopupX(x, y);
#endif // !(WINVER >= 0x0500)
return ::TrackPopupMenu(m_hMenu, nFlags, x, y, 0, hWnd, lpRect);
#else // CE specific
lpRect;
return ::TrackPopupMenuEx(m_hMenu, nFlags, x, y, hWnd, NULL);
#endif // _WIN32_WCE
}
BOOL TrackPopupMenuEx(UINT uFlags, int x, int y, HWND hWnd, LPTPMPARAMS lptpm = NULL)
{
ATLASSERT(::IsMenu(m_hMenu));
#if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
x = _FixTrackMenuPopupX(x, y);
#endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
return ::TrackPopupMenuEx(m_hMenu, uFlags, x, y, hWnd, lptpm);
}
#if (WINVER >= 0x0500) && !defined(_WIN32_WCE)
// helper that fixes popup menu X position when it's off-screen
static int _FixTrackMenuPopupX(int x, int y)
{
POINT pt = { x, y };
HMONITOR hMonitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
if(hMonitor == NULL)
{
HMONITOR hMonitorNear = ::MonitorFromPoint(pt, MONITOR_DEFAULTTONEAREST);
if(hMonitorNear != NULL)
{
MONITORINFO mi = { 0 };
mi.cbSize = sizeof(MONITORINFO);
if(::GetMonitorInfo(hMonitorNear, &mi) != FALSE)
{
if(x < mi.rcWork.left)
x = mi.rcWork.left;
else if(x > mi.rcWork.right)
x = mi.rcWork.right;
}
}
}
return x;
}
BOOL GetMenuInfo(LPMENUINFO lpMenuInfo) const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuInfo(m_hMenu, lpMenuInfo);
}
BOOL SetMenuInfo(LPCMENUINFO lpMenuInfo)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::SetMenuInfo(m_hMenu, lpMenuInfo);
}
#endif // (WINVER >= 0x0500) && !defined(_WIN32_WCE)
// Menu Item Operations
BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem = 0, LPCTSTR lpszNewItem = NULL)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::AppendMenu(m_hMenu, nFlags, nIDNewItem, lpszNewItem);
}
BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, LPCTSTR lpszNewItem)
{
ATLASSERT(::IsMenu(m_hMenu));
ATLASSERT(::IsMenu(hSubMenu));
return ::AppendMenu(m_hMenu, nFlags | MF_POPUP, (UINT_PTR)hSubMenu, lpszNewItem);
}
#ifndef _WIN32_WCE
BOOL AppendMenu(UINT nFlags, UINT_PTR nIDNewItem, HBITMAP hBmp)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::AppendMenu(m_hMenu, nFlags | MF_BITMAP, nIDNewItem, (LPCTSTR)hBmp);
}
BOOL AppendMenu(UINT nFlags, HMENU hSubMenu, HBITMAP hBmp)
{
ATLASSERT(::IsMenu(m_hMenu));
ATLASSERT(::IsMenu(hSubMenu));
return ::AppendMenu(m_hMenu, nFlags | (MF_BITMAP | MF_POPUP), (UINT_PTR)hSubMenu, (LPCTSTR)hBmp);
}
#endif // !_WIN32_WCE
UINT CheckMenuItem(UINT nIDCheckItem, UINT nCheck)
{
ATLASSERT(::IsMenu(m_hMenu));
return (UINT)::CheckMenuItem(m_hMenu, nIDCheckItem, nCheck);
}
UINT EnableMenuItem(UINT nIDEnableItem, UINT nEnable)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::EnableMenuItem(m_hMenu, nIDEnableItem, nEnable);
}
#ifndef _WIN32_WCE
BOOL HiliteMenuItem(HWND hWnd, UINT uIDHiliteItem, UINT uHilite)
{
ATLASSERT(::IsMenu(m_hMenu));
return ::HiliteMenuItem(hWnd, m_hMenu, uIDHiliteItem, uHilite);
}
int GetMenuItemCount() const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuItemCount(m_hMenu);
}
UINT GetMenuItemID(int nPos) const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuItemID(m_hMenu, nPos);
}
UINT GetMenuState(UINT nID, UINT nFlags) const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuState(m_hMenu, nID, nFlags);
}
int GetMenuString(UINT nIDItem, LPTSTR lpString, int nMaxCount, UINT nFlags) const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuString(m_hMenu, nIDItem, lpString, nMaxCount, nFlags);
}
int GetMenuStringLen(UINT nIDItem, UINT nFlags) const
{
ATLASSERT(::IsMenu(m_hMenu));
return ::GetMenuString(m_hMenu, nIDItem, NULL, 0, nFlags);
}
#ifndef _ATL_NO_COM
BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const
{
USES_CONVERSION;
ATLASSERT(::IsMenu(m_hMenu));
ATLASSERT(bstrText == NULL);
int nLen = GetMenuStringLen(nIDItem, nFlags);
if(nLen == 0)
{
bstrText = ::SysAllocString(OLESTR(""));
return (bstrText != NULL) ? TRUE : FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -