#include "stdafx.h" #include "GroupLabel.h" CGroupLabel::CGroupLabel() { m_crBkgnd = RGB(255, 255, 255); m_crLine = RGB(188, 188, 188); m_crText = RGB(88, 88, 88); m_nPadding[PADDING_LEFT] = 5; m_nPadding[PADDING_TOP] = 5; m_nPadding[PADDING_RIGHT] = 5; m_nPadding[PADDING_BOTTOM] = 5; m_nTextAlign = TEXT_ALIGN_CENTER; m_bEnableResize = FALSE; } CGroupLabel::~CGroupLabel() { } BEGIN_MESSAGE_MAP(CGroupLabel, CStatic) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_SETCURSOR() END_MESSAGE_MAP() void CGroupLabel::EnableResize() { m_bEnableResize = TRUE; DWORD dwStyle = GetStyle(); dwStyle |= SS_NOTIFY; SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle); } void CGroupLabel::DisableResize() { m_bEnableResize = FALSE; } void CGroupLabel::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: ÔÚ´Ë´¦Ìí¼ÓÏûÏ¢´¦Àí³ÌÐò´úÂë // ²»Îª»æÍ¼ÏûÏ¢µ÷Óà CStatic::OnPaint() HDC hMemDC; HBITMAP hBitmap; RECT rcClient; CString strText; HFONT hFont; HBRUSH hBrushBK; ::GetClientRect(m_hWnd, &rcClient); hMemDC = ::CreateCompatibleDC(dc.m_hDC); hBitmap = ::CreateCompatibleBitmap(dc.m_hDC, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top); ::SelectObject(hMemDC, hBitmap); ::SetBkMode(hMemDC, TRANSPARENT); // ±³¾°ÑÕÉ« hBrushBK = CreateSolidBrush(m_crBkgnd); ::FillRect(hMemDC, &rcClient, hBrushBK); DeleteObject(hBrushBK); // ´°¿Ú±êÌâ int x1, x2, x3, x4; x1 = rcClient.left + m_nPadding[PADDING_LEFT]; x4 = rcClient.right - m_nPadding[PADDING_RIGHT]; hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT); ::SelectObject(hMemDC, hFont); ::SetTextColor(hMemDC, m_crText); char szTitle[256]; int nTextLen = ::GetWindowText(m_hWnd, szTitle, 256); if (nTextLen > 0) { RECT rcTitle; SIZE sizeText; rcTitle.left = x1; rcTitle.top = rcClient.top + 2; rcTitle.bottom = rcClient.bottom - 2; rcTitle.right = x4; #define TEXT_SPAGE 12 GetTextExtentPoint32(hMemDC, szTitle, nTextLen, &sizeText); if (TEXT_ALIGN_LEFT == m_nTextAlign) { ::DrawText(hMemDC, szTitle, nTextLen, &rcTitle, DT_LEFT | DT_VCENTER | DT_SINGLELINE); x1 += sizeText.cx + TEXT_SPAGE; } else if (TEXT_ALIGN_CENTER == m_nTextAlign) { ::DrawText(hMemDC, szTitle, nTextLen, &rcTitle, DT_CENTER | DT_VCENTER | DT_SINGLELINE); x2 = x1 + (x4 - x1 - sizeText.cx - TEXT_SPAGE * 2) / 2; x3 = x2 + sizeText.cx + TEXT_SPAGE * 2; } else { ::DrawText(hMemDC, szTitle, nTextLen, &rcTitle, DT_RIGHT | DT_VCENTER | DT_SINGLELINE); x4 -= (sizeText.cx + TEXT_SPAGE); } } // ·Ö¸ôÏß HPEN hPen = CreatePen(PS_SOLID, 1, m_crLine); HPEN hOldPen = (HPEN)SelectObject(hMemDC, hPen); int y = (rcClient.bottom - rcClient.top) / 2; if (TEXT_ALIGN_LEFT == m_nTextAlign) { ::MoveToEx(hMemDC, x1, y, NULL); ::LineTo(hMemDC, x4, y); } else if (TEXT_ALIGN_CENTER == m_nTextAlign) { ::MoveToEx(hMemDC, x1, y, NULL); ::LineTo(hMemDC, x2, y); ::MoveToEx(hMemDC, x3, y, NULL); ::LineTo(hMemDC, x4, y); } else { ::MoveToEx(hMemDC, x1, y, NULL); ::LineTo(hMemDC, x4, y); } SelectObject(hMemDC, hOldPen); ::DeleteObject(hPen); // EndPaint ::BitBlt(dc.m_hDC, 0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top, hMemDC, 0, 0, SRCCOPY); ::DeleteObject(hBitmap); ::DeleteDC(hMemDC); } void CGroupLabel::SetTextAlignMode(int nAlign) { if (nAlign == TEXT_ALIGN_LEFT || nAlign == TEXT_ALIGN_CENTER || nAlign == TEXT_ALIGN_RIGHT) { m_nTextAlign = nAlign; RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); ::InvalidateRect(m_hWnd, &rcClient, TRUE); } } void CGroupLabel::Setpadding(int type, unsigned int nPadding) { if (type >= PADDING_LEFT && PADDING_LEFT <= PADDING_BOTTOM) { m_nPadding[type] = nPadding; } } /* * ÉèÖñ³¾°É« * color -- ±³¾°É« */ void CGroupLabel::SetBkgndColor(COLORREF color) { m_crBkgnd = color; } /* * ÉèÖÃÎı¾É« * color -- ±³¾°É« */ void CGroupLabel::SetTextColor(COLORREF color) { m_crText = color; RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); ::InvalidateRect(m_hWnd, &rcClient, TRUE); } /* * ÉèÖÃÏßÉ« * color -- ±³¾°É« */ void CGroupLabel::SetLineColor(COLORREF color) { m_crLine = color; RECT rcClient; ::GetClientRect(m_hWnd, &rcClient); ::InvalidateRect(m_hWnd, &rcClient, TRUE); } void CGroupLabel::OnLButtonDown(UINT nFlags, CPoint point) { if (!m_bEnableResize) { CStatic::OnLButtonDown(nFlags, point); return; } CPoint pt, ptNew; pt = point; int nMoveY = 0; // ²¶×½Êó±êÏûÏ¢£¬¼ì²âÊÇ·ñÍ϶¯ CRect rcParent, rcWindows; GetClientRect(&rcWindows); ::ClientToScreen(m_hWnd, (LPPOINT)&rcWindows); ::ClientToScreen(m_hWnd, (LPPOINT)&rcWindows.right); GetParent()->GetClientRect(&rcParent); ::ClientToScreen(GetParent()->m_hWnd, (LPPOINT)&rcParent); HDC hDC = ::GetDC(::GetDesktopWindow()); ::DrawFocusRect(hDC, &rcWindows); if (::GetCapture() == NULL) { SetCapture(); ASSERT(this == GetCapture()); AfxLockTempMaps(); for (;;) { MSG msg; VERIFY(::GetMessage(&msg, NULL, 0, 0)); if (GetCapture() != this) break; switch (msg.message) { case WM_MOUSEMOVE: ptNew = msg.pt; if (ptNew.y < rcParent.top) ptNew.y = rcParent.top; ::DrawFocusRect(hDC, &rcWindows); rcWindows.top = ptNew.y - 3; rcWindows.bottom = ptNew.y + 3; ::DrawFocusRect(hDC, &rcWindows); ::ScreenToClient(m_hWnd, &ptNew); break; case WM_LBUTTONUP: ptNew = msg.pt; ::ScreenToClient(m_hWnd, &ptNew); nMoveY = ptNew.y - pt.y; goto ExitLoop; case WM_KEYDOWN: if (msg.wParam == VK_ESCAPE) { goto ExitLoop; } break; default: DispatchMessage(&msg); break; } } ExitLoop: ::DrawFocusRect(hDC, &rcWindows); ::ReleaseDC(::GetDesktopWindow(), hDC); ReleaseCapture(); ::InvalidateRect(m_hWnd, NULL, TRUE); GetParent()->SendMessage(ID_MSG_RESIZEY, nMoveY, 0); AfxUnlockTempMaps(FALSE); } CStatic::OnLButtonDown(nFlags, point); } BOOL CGroupLabel::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { if (m_bEnableResize) { ::SetCursor(::LoadCursor(NULL, IDC_SIZENS)); return TRUE; } return CStatic::OnSetCursor(pWnd, nHitTest, message); }