📄 atlgdi.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 __ATLGDI_H__
#define __ATLGDI_H__
#pragma once
#ifndef __cplusplus
#error ATL requires C++ compilation (use a .cpp suffix)
#endif
#ifndef __ATLAPP_H__
#error atlgdi.h requires atlapp.h to be included first
#endif
// protect template members from windowsx.h macros
#ifdef _INC_WINDOWSX
#undef CopyRgn
#undef CreateBrush
#undef CreatePen
#undef SelectBrush
#undef SelectPen
#undef SelectFont
#undef SelectBitmap
#endif // _INC_WINDOWSX
// required libraries
#if !defined(_ATL_NO_MSIMG) && !defined(_WIN32_WCE)
#pragma comment(lib, "msimg32.lib")
#endif // !defined(_ATL_NO_MSIMG) && !defined(_WIN32_WCE)
#if !defined(_ATL_NO_OPENGL) && !defined(_WIN32_WCE)
#pragma comment(lib, "opengl32.lib")
#endif // !defined(_ATL_NO_OPENGL) && !defined(_WIN32_WCE)
///////////////////////////////////////////////////////////////////////////////
// Classes in this file:
//
// CPenT<t_bManaged>
// CBrushT<t_bManaged>
// CLogFont
// CFontT<t_bManaged>
// CBitmapT<t_bManaged>
// CPaletteT<t_bManaged>
// CRgnT<t_bManaged>
// CDCT<t_bManaged>
// CPaintDC
// CClientDC
// CWindowDC
// CMemoryDC
// CEnhMetaFileInfo
// CEnhMetaFileT<t_bManaged>
// CEnhMetaFileDC
//
// Global functions:
// AtlGetBitmapResourceInfo()
// AtlGetBitmapResourceBitsPerPixel()
// AtlIsAlphaBitmapResource()
// AtlIsDib16()
// AtlGetDibColorTableSize()
// AtlGetDibNumColors(),
// AtlGetDibBitmap()
// AtlCopyBitmap()
// AtlCreatePackedDib16()
// AtlSetClipboardDib16()
// AtlGetClipboardDib()
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// Bitmap resource helpers to extract bitmap information for a bitmap resource
inline LPBITMAPINFOHEADER AtlGetBitmapResourceInfo(HMODULE hModule, ATL::_U_STRINGorID image)
{
HRSRC hResource = ::FindResource(hModule, image.m_lpstr, RT_BITMAP);
ATLASSERT(hResource != NULL);
HGLOBAL hGlobal = ::LoadResource(hModule, hResource);
ATLASSERT(hGlobal != NULL);
LPBITMAPINFOHEADER pBitmapInfoHeader = (LPBITMAPINFOHEADER)::LockResource(hGlobal);
ATLASSERT(pBitmapInfoHeader != NULL);
return pBitmapInfoHeader;
}
inline WORD AtlGetBitmapResourceBitsPerPixel(HMODULE hModule, ATL::_U_STRINGorID image)
{
LPBITMAPINFOHEADER pBitmapInfoHeader = AtlGetBitmapResourceInfo(hModule, image);
ATLASSERT(pBitmapInfoHeader != NULL);
return pBitmapInfoHeader->biBitCount;
}
inline WORD AtlGetBitmapResourceBitsPerPixel(ATL::_U_STRINGorID image)
{
return AtlGetBitmapResourceBitsPerPixel(ModuleHelper::GetResourceInstance(), image);
}
///////////////////////////////////////////////////////////////////////////////
// 32-bit (alpha channel) bitmap resource helper
// Note: 32-bit (alpha channel) images work only on Windows XP with Common Controls version 6.
// If you want your app to work on older version of Windows, load non-alpha images if Common
// Controls version is less than 6.
inline bool AtlIsAlphaBitmapResource(ATL::_U_STRINGorID image)
{
return (AtlGetBitmapResourceBitsPerPixel(image) == 32);
}
///////////////////////////////////////////////////////////////////////////////
// CPen
template <bool t_bManaged>
class CPenT
{
public:
// Data members
HPEN m_hPen;
// Constructor/destructor/operators
CPenT(HPEN hPen = NULL) : m_hPen(hPen)
{ }
~CPenT()
{
if(t_bManaged && m_hPen != NULL)
DeleteObject();
}
CPenT<t_bManaged>& operator =(HPEN hPen)
{
Attach(hPen);
return *this;
}
void Attach(HPEN hPen)
{
if(t_bManaged && m_hPen != NULL && m_hPen != hPen)
::DeleteObject(m_hPen);
m_hPen = hPen;
}
HPEN Detach()
{
HPEN hPen = m_hPen;
m_hPen = NULL;
return hPen;
}
operator HPEN() const { return m_hPen; }
bool IsNull() const { return (m_hPen == NULL); }
// Create methods
HPEN CreatePen(int nPenStyle, int nWidth, COLORREF crColor)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::CreatePen(nPenStyle, nWidth, crColor);
return m_hPen;
}
#ifndef _WIN32_WCE
HPEN CreatePen(int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::ExtCreatePen(nPenStyle, nWidth, pLogBrush, nStyleCount, lpStyle);
return m_hPen;
}
#endif // !_WIN32_WCE
HPEN CreatePenIndirect(LPLOGPEN lpLogPen)
{
ATLASSERT(m_hPen == NULL);
m_hPen = ::CreatePenIndirect(lpLogPen);
return m_hPen;
}
BOOL DeleteObject()
{
ATLASSERT(m_hPen != NULL);
BOOL bRet = ::DeleteObject(m_hPen);
if(bRet)
m_hPen = NULL;
return bRet;
}
// Attributes
int GetLogPen(LOGPEN* pLogPen) const
{
ATLASSERT(m_hPen != NULL);
return ::GetObject(m_hPen, sizeof(LOGPEN), pLogPen);
}
bool GetLogPen(LOGPEN& LogPen) const
{
ATLASSERT(m_hPen != NULL);
return (::GetObject(m_hPen, sizeof(LOGPEN), &LogPen) == sizeof(LOGPEN));
}
#ifndef _WIN32_WCE
int GetExtLogPen(EXTLOGPEN* pLogPen) const
{
ATLASSERT(m_hPen != NULL);
return ::GetObject(m_hPen, sizeof(EXTLOGPEN), pLogPen);
}
bool GetExtLogPen(EXTLOGPEN& ExtLogPen) const
{
ATLASSERT(m_hPen != NULL);
return (::GetObject(m_hPen, sizeof(EXTLOGPEN), &ExtLogPen) == sizeof(EXTLOGPEN));
}
#endif // !_WIN32_WCE
};
typedef CPenT<false> CPenHandle;
typedef CPenT<true> CPen;
///////////////////////////////////////////////////////////////////////////////
// CBrush
template <bool t_bManaged>
class CBrushT
{
public:
// Data members
HBRUSH m_hBrush;
// Constructor/destructor/operators
CBrushT(HBRUSH hBrush = NULL) : m_hBrush(hBrush)
{ }
~CBrushT()
{
if(t_bManaged && m_hBrush != NULL)
DeleteObject();
}
CBrushT<t_bManaged>& operator =(HBRUSH hBrush)
{
Attach(hBrush);
return *this;
}
void Attach(HBRUSH hBrush)
{
if(t_bManaged && m_hBrush != NULL && m_hBrush != hBrush)
::DeleteObject(m_hBrush);
m_hBrush = hBrush;
}
HBRUSH Detach()
{
HBRUSH hBrush = m_hBrush;
m_hBrush = NULL;
return hBrush;
}
operator HBRUSH() const { return m_hBrush; }
bool IsNull() const { return (m_hBrush == NULL); }
// Create methods
HBRUSH CreateSolidBrush(COLORREF crColor)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateSolidBrush(crColor);
return m_hBrush;
}
#ifndef _WIN32_WCE
HBRUSH CreateHatchBrush(int nIndex, COLORREF crColor)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateHatchBrush(nIndex, crColor);
return m_hBrush;
}
#endif // !_WIN32_WCE
#if !defined(_WIN32_WCE) || (_ATL_VER >= 0x0800)
HBRUSH CreateBrushIndirect(const LOGBRUSH* lpLogBrush)
{
ATLASSERT(m_hBrush == NULL);
#ifndef _WIN32_WCE
m_hBrush = ::CreateBrushIndirect(lpLogBrush);
#else // CE specific
m_hBrush = ATL::CreateBrushIndirect(lpLogBrush);
#endif // _WIN32_WCE
return m_hBrush;
}
#endif // !defined(_WIN32_WCE) || (_ATL_VER >= 0x0800)
HBRUSH CreatePatternBrush(HBITMAP hBitmap)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreatePatternBrush(hBitmap);
return m_hBrush;
}
HBRUSH CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage)
{
ATLASSERT(hPackedDIB != NULL);
const void* lpPackedDIB = GlobalLock(hPackedDIB);
ATLASSERT(lpPackedDIB != NULL);
m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
GlobalUnlock(hPackedDIB);
return m_hBrush;
}
HBRUSH CreateDIBPatternBrush(const void* lpPackedDIB, UINT nUsage)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
return m_hBrush;
}
HBRUSH CreateSysColorBrush(int nIndex)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::GetSysColorBrush(nIndex);
return m_hBrush;
}
BOOL DeleteObject()
{
ATLASSERT(m_hBrush != NULL);
BOOL bRet = ::DeleteObject(m_hBrush);
if(bRet)
m_hBrush = NULL;
return bRet;
}
// Attributes
int GetLogBrush(LOGBRUSH* pLogBrush) const
{
ATLASSERT(m_hBrush != NULL);
return ::GetObject(m_hBrush, sizeof(LOGBRUSH), pLogBrush);
}
bool GetLogBrush(LOGBRUSH& LogBrush) const
{
ATLASSERT(m_hBrush != NULL);
return (::GetObject(m_hBrush, sizeof(LOGBRUSH), &LogBrush) == sizeof(LOGBRUSH));
}
};
typedef CBrushT<false> CBrushHandle;
typedef CBrushT<true> CBrush;
///////////////////////////////////////////////////////////////////////////////
// CFont
class CLogFont : public LOGFONT
{
public:
CLogFont()
{
memset(this, 0, sizeof(LOGFONT));
}
CLogFont(const LOGFONT& lf)
{
Copy(&lf);
}
CLogFont(HFONT hFont)
{
ATLASSERT(::GetObjectType(hFont) == OBJ_FONT);
::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
}
HFONT CreateFontIndirect()
{
return ::CreateFontIndirect(this);
}
void SetBold()
{
lfWeight = FW_BOLD;
}
bool IsBold() const
{
return (lfWeight >= FW_BOLD);
}
void MakeBolder(int iScale = 1)
{
lfWeight += FW_BOLD * iScale;
}
void MakeLarger(int iScale)
{
if(lfHeight > 0)
lfHeight += iScale;
else
lfHeight -= iScale;
}
void SetHeight(LONG nPointSize, HDC hDC = NULL)
{
// For MM_TEXT mapping mode
lfHeight = -::MulDiv(nPointSize, ::GetDeviceCaps(hDC, LOGPIXELSY), 72);
}
LONG GetHeight(HDC hDC = NULL) const
{
// For MM_TEXT mapping mode
return ::MulDiv(-lfHeight, 72, ::GetDeviceCaps(hDC, LOGPIXELSY));
}
LONG GetDeciPointHeight(HDC hDC = NULL) const
{
#ifndef _WIN32_WCE
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC, &ptOrg, 1);
POINT pt = { 0, 0 };
pt.y = abs(lfHeight) + ptOrg.y;
::LPtoDP(hDC,&pt,1);
return ::MulDiv(pt.y, 720, ::GetDeviceCaps(hDC, LOGPIXELSY)); // 72 points/inch, 10 decipoints/point
#else // CE specific
// DP and LP are always the same on CE
return ::MulDiv(abs(lfHeight), 720, ::GetDeviceCaps(hDC, LOGPIXELSY)); // 72 points/inch, 10 decipoints/point
#endif // _WIN32_WCE
}
void SetHeightFromDeciPoint(LONG nDeciPtHeight, HDC hDC = NULL)
{
#ifndef _WIN32_WCE
POINT pt = { 0, 0 };
pt.y = ::MulDiv(::GetDeviceCaps(hDC, LOGPIXELSY), nDeciPtHeight, 720); // 72 points/inch, 10 decipoints/point
::DPtoLP(hDC, &pt, 1);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC, &ptOrg, 1);
lfHeight = -abs(pt.y - ptOrg.y);
#else // CE specific
// DP and LP are always the same on CE
lfHeight = -abs(::MulDiv(::GetDeviceCaps(hDC, LOGPIXELSY), nDeciPtHeight, 720)); // 72 points/inch, 10 decipoints/point
#endif // _WIN32_WCE
}
#ifndef _WIN32_WCE
void SetCaptionFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfCaptionFont);
}
void SetMenuFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfMenuFont);
}
void SetStatusFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfStatusFont);
}
void SetMessageBoxFont()
{
NONCLIENTMETRICS ncm = { RunTimeHelper::SizeOf_NONCLIENTMETRICS() };
ATLVERIFY(::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0));
Copy(&ncm.lfMessageFont);
}
#endif // !_WIN32_WCE
void Copy(const LOGFONT* pLogFont)
{
ATLASSERT(pLogFont != NULL);
*(LOGFONT*)this = *pLogFont;
}
CLogFont& operator =(const CLogFont& src)
{
Copy(&src);
return *this;
}
CLogFont& operator =(const LOGFONT& src)
{
Copy(&src);
return *this;
}
CLogFont& operator =(HFONT hFont)
{
ATLASSERT(::GetObjectType(hFont) == OBJ_FONT);
::GetObject(hFont, sizeof(LOGFONT), (LOGFONT*)this);
return *this;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -