#include "stdafx.h"
|
#include "HmLabel.h"
|
|
|
CHmLabel::CHmLabel()
|
{
|
m_crFrame = RGB(128, 128, 128);
|
m_crBackground = RGB(255, 0, 0);
|
m_crForeground = RGB(255, 255, 255);
|
m_hFont = NULL;
|
m_hFontNote = NULL;
|
}
|
|
|
CHmLabel::~CHmLabel()
|
{
|
if (m_hFont != NULL) {
|
::DeleteObject(m_hFont);
|
m_hFont = NULL;
|
}
|
|
if (m_hFontNote != NULL) {
|
::DeleteObject(m_hFontNote);
|
m_hFontNote = NULL;
|
}
|
}
|
|
BEGIN_MESSAGE_MAP(CHmLabel, CStatic)
|
ON_WM_PAINT()
|
ON_WM_NCPAINT()
|
END_MESSAGE_MAP()
|
|
void CHmLabel::setText(CString strText)
|
{
|
SetWindowText(strText);
|
Invalidate();
|
}
|
|
void CHmLabel::setNote1(CString strNote1)
|
{
|
m_strNote1 = strNote1;
|
}
|
|
void CHmLabel::setFontSize(int size)
|
{
|
if (m_hFont != NULL) {
|
::DeleteObject(m_hFont);
|
m_hFont = NULL;
|
}
|
|
m_hFont = CreateFont(size, 0, 0, 0, FW_MEDIUM,
|
FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
|
CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, _T("ËÎÌå"));
|
}
|
|
void CHmLabel::setNoteFontSize(int size)
|
{
|
if (m_hFontNote != NULL) {
|
::DeleteObject(m_hFontNote);
|
m_hFontNote = NULL;
|
}
|
|
m_hFontNote = CreateFont(size, 0, 0, 0, FW_MEDIUM,
|
FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
|
CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, _T("ËÎÌå"));
|
}
|
|
void CHmLabel::setNoteTextColor(COLORREF color, BOOL bInvalidate/* = FALSE*/)
|
{
|
m_crNote = color;
|
if (bInvalidate) Invalidate(TRUE);
|
}
|
|
void CHmLabel::setBackground(COLORREF color, BOOL bInvalidate/* = FALSE*/)
|
{
|
m_crBackground = color;
|
if (bInvalidate) Invalidate(TRUE);
|
}
|
|
void CHmLabel::setForeground(COLORREF color, BOOL bInvalidate/* = FALSE*/)
|
{
|
m_crForeground = color;
|
if (bInvalidate) Invalidate(TRUE);
|
}
|
|
void CHmLabel::OnPaint()
|
{
|
CPaintDC dc(this); // device context for painting
|
// TODO: ÔÚ´Ë´¦Ìí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂë
|
// ²»Îª»æÍ¼ÏûÏ¢µ÷Óà CStatic::OnPaint()
|
|
|
HDC hMemDC;
|
HBITMAP hBitmap;
|
RECT rcClient;
|
CString strText;
|
HBRUSH hBrushBK;
|
|
|
GetClientRect(&rcClient);
|
hMemDC = ::CreateCompatibleDC(dc.m_hDC);
|
hBitmap = ::CreateCompatibleBitmap(dc.m_hDC, rcClient.right - rcClient.left,
|
rcClient.bottom - rcClient.top);
|
::SelectObject(hMemDC, hBitmap);
|
|
|
// ±³¾°ÑÕÉ«
|
hBrushBK = CreateSolidBrush(m_crBackground);
|
::FillRect(hMemDC, &rcClient, hBrushBK);
|
DeleteObject(hBrushBK);
|
|
|
// ÎÄ×Ö
|
char szText[256];
|
GetWindowText(szText, 256);
|
RECT rcText = rcClient;
|
SetBkMode(hMemDC, TRANSPARENT);
|
SetTextColor(hMemDC, m_crForeground);
|
::SelectObject(hMemDC, m_hFont == NULL ? (HFONT)GetStockObject(DEFAULT_GUI_FONT) : m_hFont);
|
::DrawTextA(hMemDC, szText, (int)strlen(szText), &rcText, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS);
|
|
|
// Note1
|
SetTextColor(hMemDC, m_crNote);
|
::SelectObject(hMemDC, m_hFontNote == NULL ? (HFONT)GetStockObject(DEFAULT_GUI_FONT) : m_hFontNote);
|
::DrawText(hMemDC, m_strNote1, m_strNote1.GetLength(), &rcClient, DT_CENTER | DT_BOTTOM | DT_SINGLELINE | DT_END_ELLIPSIS);
|
|
|
// EndPaint
|
::BitBlt(dc.m_hDC, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
|
hMemDC, 0, 0, SRCCOPY);
|
::DeleteObject(hBitmap);
|
::DeleteDC(hMemDC);
|
}
|
|
|
void CHmLabel::OnNcPaint()
|
{
|
// TODO: ÔÚ´Ë´¦Ìí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂë
|
// ²»Îª»æÍ¼ÏûÏ¢µ÷Óà CStatic::OnNcPaint()
|
long styleEx = GetWindowLong(m_hWnd, GWL_EXSTYLE);
|
if ((styleEx & WS_EX_CLIENTEDGE) == WS_EX_CLIENTEDGE) {
|
|
RECT rect, rcClient;
|
GetClientRect(&rcClient);
|
::ClientToScreen(m_hWnd, (LPPOINT)&rcClient.left);
|
::ClientToScreen(m_hWnd, (LPPOINT)&rcClient.right);
|
GetWindowRect(&rect);
|
::OffsetRect(&rcClient, -rect.left, -rect.top);
|
|
rect.right -= rect.left;
|
rect.bottom -= rect.top;
|
rect.left = 0;
|
rect.top = 0;
|
|
HRGN hRgnWnd = CreateRectRgnIndirect(&rect);
|
HRGN hRgnClient = CreateRectRgnIndirect(&rcClient);
|
|
HBRUSH hBrushBK, hBrushFrame;
|
HDC hDC = ::GetWindowDC(m_hWnd);
|
::SelectClipRgn(hDC, hRgnWnd);
|
::ExtSelectClipRgn(hDC, hRgnClient, RGN_DIFF);
|
|
hBrushBK = CreateSolidBrush(m_crBackground);
|
::FillRect(hDC, &rect, hBrushBK);
|
DeleteObject(hBrushBK);
|
|
hBrushFrame = CreateSolidBrush(m_crFrame);
|
::FrameRect(hDC, &rect, hBrushFrame);
|
|
::DeleteObject(hRgnWnd);
|
::DeleteObject(hRgnClient);
|
DeleteObject(hBrushFrame);
|
::ReleaseDC(m_hWnd, hDC);
|
}
|
}
|