📄 atlgdi.h
字号:
// Windows Template Library - WTL version 7.1
// Copyright (C) 1997-2003 Microsoft Corporation
// All rights reserved.
//
// This file is a part of the Windows Template Library.
// The code and information is provided "as-is" without
// warranty of any kind, either expressed or implied.
#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
#if defined(_WIN32_WCE) && defined(DrawIcon)
#undef DrawIcon
#endif //defined(_WIN32_WCE) && defined(DrawIcon)
// 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>
// CFontT<t_bManaged>
// CBitmapT<t_bManaged>
// CPaletteT<t_bManaged>
// CRgnT<t_bManaged>
// CDCT<t_bManaged>
// CPaintDC
// CClientDC
// CWindowDC
// CEnhMetaFileInfo
// CEnhMetaFileT<t_bManaged>
// CEnhMetaFileDC
//
// Global functions:
// AtlIsAlphaBitmapResource()
namespace WTL
{
///////////////////////////////////////////////////////////////////////////////
// 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)
{
#if (_ATL_VER >= 0x0700)
HRSRC hResource = ::FindResource(ATL::_AtlBaseModule.GetResourceInstance(), image.m_lpstr, RT_BITMAP);
ATLASSERT(hResource != NULL);
HGLOBAL hGlobal = ::LoadResource(ATL::_AtlBaseModule.GetResourceInstance(), hResource);
ATLASSERT(hGlobal != NULL);
#else //!(_ATL_VER >= 0x0700)
HRSRC hResource = ::FindResource(ATL::_pModule->GetResourceInstance(), image.m_lpstr, RT_BITMAP);
ATLASSERT(hResource != NULL);
HGLOBAL hGlobal = ::LoadResource(ATL::_pModule->GetResourceInstance(), hResource);
ATLASSERT(hGlobal != NULL);
#endif //!(_ATL_VER >= 0x0700)
LPBITMAPINFOHEADER pBitmapInfoHeader = (LPBITMAPINFOHEADER)::LockResource(hGlobal);
ATLASSERT(pBitmapInfoHeader != NULL);
return (pBitmapInfoHeader->biBitCount == 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;
}
HBRUSH CreateBrushIndirect(const LOGBRUSH* lpLogBrush)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreateBrushIndirect(lpLogBrush);
return m_hBrush;
}
#endif //!_WIN32_WCE
HBRUSH CreatePatternBrush(HBITMAP hBitmap)
{
ATLASSERT(m_hBrush == NULL);
m_hBrush = ::CreatePatternBrush(hBitmap);
return m_hBrush;
}
HBRUSH CreateDIBPatternBrush(HGLOBAL hPackedDIB, UINT nUsage)
{
ATLASSERT(hPackedDIB != NULL);
#ifndef _WIN32_WCE
const void* lpPackedDIB = ::GlobalLock(hPackedDIB);
#else // CE specific
const void* lpPackedDIB = GlobalLock(hPackedDIB);
#endif //_WIN32_WCE
ATLASSERT(lpPackedDIB != NULL);
m_hBrush = ::CreateDIBPatternBrushPt(lpPackedDIB, nUsage);
#ifndef _WIN32_WCE
::GlobalUnlock(hPackedDIB);
#else // CE specific
GlobalUnlock(hPackedDIB);
#endif //_WIN32_WCE
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
template <bool t_bManaged>
class CFontT
{
public:
// Data members
HFONT m_hFont;
// Constructor/destructor/operators
CFontT(HFONT hFont = NULL) : m_hFont(hFont)
{ }
~CFontT()
{
if(t_bManaged && m_hFont != NULL)
DeleteObject();
}
CFontT<t_bManaged>& operator =(HFONT hFont)
{
Attach(hFont);
return *this;
}
void Attach(HFONT hFont)
{
if(t_bManaged && m_hFont != NULL && m_hFont != hFont)
::DeleteObject(m_hFont);
m_hFont = hFont;
}
HFONT Detach()
{
HFONT hFont = m_hFont;
m_hFont = NULL;
return hFont;
}
operator HFONT() const { return m_hFont; }
bool IsNull() const { return (m_hFont == NULL); }
// Create methods
HFONT CreateFontIndirect(const LOGFONT* lpLogFont)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFontIndirect(lpLogFont);
return m_hFont;
}
#ifndef _WIN32_WCE
#if (_WIN32_WINNT >= 0x0500)
HFONT CreateFontIndirectEx(CONST ENUMLOGFONTEXDV* penumlfex)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFontIndirectEx(penumlfex);
return m_hFont;
}
#endif //(_WIN32_WINNT >= 0x0500)
HFONT CreateFont(int nHeight, int nWidth, int nEscapement,
int nOrientation, int nWeight, BYTE bItalic, BYTE bUnderline,
BYTE cStrikeOut, BYTE nCharSet, BYTE nOutPrecision,
BYTE nClipPrecision, BYTE nQuality, BYTE nPitchAndFamily,
LPCTSTR lpszFacename)
{
ATLASSERT(m_hFont == NULL);
m_hFont = ::CreateFont(nHeight, nWidth, nEscapement,
nOrientation, nWeight, bItalic, bUnderline, cStrikeOut,
nCharSet, nOutPrecision, nClipPrecision, nQuality,
nPitchAndFamily, lpszFacename);
return m_hFont;
}
#endif //!_WIN32_WCE
HFONT CreatePointFont(int nPointSize, LPCTSTR lpszFaceName, HDC hDC = NULL)
{
LOGFONT logFont = { 0 };
logFont.lfCharSet = DEFAULT_CHARSET;
logFont.lfHeight = nPointSize;
lstrcpyn(logFont.lfFaceName, lpszFaceName, sizeof(logFont.lfFaceName) / sizeof(TCHAR));
return CreatePointFontIndirect(&logFont, hDC);
}
HFONT CreatePointFontIndirect(const LOGFONT* lpLogFont, HDC hDC = NULL)
{
HDC hDC1 = (hDC != NULL) ? hDC : ::GetDC(NULL);
// convert nPointSize to logical units based on hDC
LOGFONT logFont = *lpLogFont;
#ifndef _WIN32_WCE
POINT pt = { 0, 0 };
pt.y = ::GetDeviceCaps(hDC1, LOGPIXELSY) * logFont.lfHeight;
pt.y /= 720; // 72 points/inch, 10 decipoints/point
::DPtoLP(hDC1, &pt, 1);
POINT ptOrg = { 0, 0 };
::DPtoLP(hDC1, &ptOrg, 1);
logFont.lfHeight = -abs(pt.y - ptOrg.y);
#else // CE specific
// DP and LP are always the same on CE
logFont.lfHeight = ::GetDeviceCaps(hDC1, LOGPIXELSY) * logFont.lfHeight;
logFont.lfHeight /= 720; // 72 points/inch, 10 decipoints/point
if(logFont.lfHeight > 0)
{
logFont.lfHeight *= -1;
}
#endif //!_WIN32_WCE
if(hDC == NULL)
::ReleaseDC(NULL, hDC1);
return CreateFontIndirect(&logFont);
}
BOOL DeleteObject()
{
ATLASSERT(m_hFont != NULL);
BOOL bRet = ::DeleteObject(m_hFont);
if(bRet)
m_hFont = NULL;
return bRet;
}
// Attributes
int GetLogFont(LOGFONT* pLogFont) const
{
ATLASSERT(m_hFont != NULL);
return ::GetObject(m_hFont, sizeof(LOGFONT), pLogFont);
}
bool GetLogFont(LOGFONT& LogFont) const
{
ATLASSERT(m_hFont != NULL);
return (::GetObject(m_hFont, sizeof(LOGFONT), &LogFont) == sizeof(LOGFONT));
}
};
typedef CFontT<false> CFontHandle;
typedef CFontT<true> CFont;
///////////////////////////////////////////////////////////////////////////////
// CBitmap
template <bool t_bManaged>
class CBitmapT
{
public:
// Data members
HBITMAP m_hBitmap;
// Constructor/destructor/operators
CBitmapT(HBITMAP hBitmap = NULL) : m_hBitmap(hBitmap)
{ }
~CBitmapT()
{
if(t_bManaged && m_hBitmap != NULL)
DeleteObject();
}
CBitmapT<t_bManaged>& operator =(HBITMAP hBitmap)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -