📄 graphics.cpp
字号:
#include "StdAfx.h"
#include "graphics.h"
CGraphics::CGraphics(HWND hWnd)
{
m_hWnd = hWnd;
m_hDc = NULL;
SetOrigin(0, 0);
m_nColor = RGB(0, 0, 0);
m_nBgColor = RGB(255, 255, 255);
m_hPen = CreatePen(PS_SOLID, 1, m_nColor);
m_hBrush = CreateSolidBrush(m_nBgColor);
// SetBkMode(m_hDc, TRANSPARENT);
}
CGraphics::CGraphics(HDC hParentDc, int w, int h)
{
m_hWnd = NULL;
m_hDc = ::CreateCompatibleDC(hParentDc);
m_hBgBm = ::CreateCompatibleBitmap(hParentDc, w, h);
SelectObject(m_hDc, m_hBgBm);
SetOrigin(0, 0);
m_nColor = RGB(0, 0, 0);
m_nBgColor = RGB(255, 255, 255);
m_hPen = ::CreatePen(PS_SOLID, 1, m_nColor);
m_hBrush = ::CreateSolidBrush(m_nBgColor);
// SetBkMode(m_hDc, TRANSPARENT);
}
CGraphics::~CGraphics(void)
{
::DeleteObject(m_hBrush);
::DeleteObject(m_hPen);
if (m_hDc && !m_hWnd)
::DeleteDC(m_hDc);
}
void CGraphics::BeginPaint()
{
m_hDc = ::BeginPaint(m_hWnd, &m_ps);
::SetBkMode(m_hDc, TRANSPARENT);
}
void CGraphics::EndPaint()
{
::EndPaint(m_hWnd, &m_ps);
m_hDc = NULL;
}
const RECT* CGraphics::GetPaintRect()
{
if (m_hDc)
return &m_ps.rcPaint;
return NULL;
}
void CGraphics::SetOrigin(int x, int y)
{
m_nOriginX = x;
m_nOriginY = y;
}
void CGraphics::SetColor(int rgb)
{
m_nColor = rgb;
::DeleteObject(m_hPen);
m_hPen = ::CreatePen(PS_SOLID, 1, m_nColor);
}
void CGraphics::SetBgColor(int rgb)
{
m_nBgColor = rgb;
DeleteObject(m_hBrush);
m_hBrush = CreateSolidBrush(m_nBgColor);
}
void CGraphics::DrawRoundRect(const RECT* pRect, int nEllipseWidth, int nEllipseHeight)
{
::SelectObject(m_hDc, m_hPen);
::SelectObject(m_hDc, m_hBrush);
::RoundRect(m_hDc, pRect->left + m_nOriginX, pRect->top + m_nOriginY, pRect->right + m_nOriginX, pRect->bottom + m_nOriginY, nEllipseWidth, nEllipseHeight);
}
void CGraphics::DrawText(const RECT* pRect, char* pText, int nFormat)
{
::SetTextColor(m_hDc, m_nColor);
RECT r = *pRect;
::OffsetRect(&r, m_nOriginX, m_nOriginY);
::DrawText(m_hDc, pText, (int)strlen(pText), &r, nFormat);
}
void CGraphics::SetFont(CFont* pFont)
{
if (pFont)
::SelectObject(m_hDc, pFont->GetFont());
}
void CGraphics::Copy(const RECT* pDestRect, CGraphics* pSrc, int x, int y)
{
::BitBlt( m_hDc, pDestRect->left + m_nOriginX, pDestRect->top + m_nOriginY
, pDestRect->right - pDestRect->left + m_nOriginX, pDestRect->bottom - pDestRect->top + m_nOriginY
, pSrc->m_hDc, x, y, SRCCOPY);
}
void CGraphics::DrawRect(const RECT* pRect)
{
RECT r = *pRect;
::OffsetRect(&r, m_nOriginX, m_nOriginY);
::FillRect(m_hDc, &r, m_hBrush);
}
void CGraphics::DrawLine(int x1, int y1, int x2, int y2)
{
BOOL r;
HGDIOBJ old = ::SelectObject(m_hDc, m_hPen);
r = ::MoveToEx(m_hDc, x1 + m_nOriginX, y1 + m_nOriginY, NULL);
r = ::LineTo(m_hDc, x2 + m_nOriginX, y2 + m_nOriginY);
}
CFont::CFont(const char* pFaceName, int nHeight, FontWeight tWeight)
{
memset(&m_oLogFont, 0, sizeof(m_oLogFont));
strcpy(m_oLogFont.lfFaceName, pFaceName);
m_oLogFont.lfHeight = nHeight;
SetWeight(tWeight);
m_oLogFont.lfQuality = CLEARTYPE_QUALITY;
}
void CFont::SetFace(const char* pFaceName)
{
ReleaseHandle();
strcpy(m_oLogFont.lfFaceName, pFaceName);
}
void CFont::SetHeight(int h)
{
ReleaseHandle();
m_oLogFont.lfHeight = h;
}
void CFont::SetWeight(FontWeight tWeight)
{
ReleaseHandle();
switch(tWeight)
{
case THIN:
m_oLogFont.lfWeight = 100; break;
case NORMAL:
m_oLogFont.lfWeight = 400; break;
case BOLD:
m_oLogFont.lfWeight = 700; break;
}
}
void CFont::SetItalic(BOOL bFlag)
{
ReleaseHandle();
m_oLogFont.lfItalic = bFlag;
}
void CFont::SetUnderline(BOOL bFlag)
{
ReleaseHandle();
m_oLogFont.lfUnderline = bFlag;
}
void CFont::SetStrikeOut(BOOL bFlag)
{
ReleaseHandle();
m_oLogFont.lfStrikeOut = bFlag;
}
HFONT CFont::GetFont()
{
if (NULL == m_hFont)
m_hFont = CreateFontIndirect(&m_oLogFont);
return m_hFont;
}
void CFont::ReleaseHandle()
{
if (NULL != m_hFont)
{
DeleteObject(m_hFont);
m_hFont = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -