You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

296 lines
6.8 KiB
C++

// LabelEx.cpp : implementation file
//
#include "stdafx.h"
#include "LabelEx.h"
#include "GlobalDefine.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLabelEx
CLabelEx::CLabelEx()
{
m_bClicked=FALSE;
m_bOver=FALSE;
m_bUnderLine=FALSE;
m_bAutoUnderLine=FALSE;
m_bBorder=FALSE;
m_bHighLight=TRUE;
m_bBackUnderLine=FALSE;
m_crText=RGB(0,0,0);
m_crBorder=RGB(0,0,0);
m_crBG=RGB(255,255,255);
m_crUnderLine=RGB(0,0,255);
m_hHandCur=AfxGetApp()->LoadCursor(IDC_HAND);
SetTextColor(RGB_RED);
SetBkColor(RGB_GRAY);
m_CurBmpID = 0;
}
CLabelEx::~CLabelEx()
{
if(m_BGBm.GetSafeHandle()!=NULL)
m_BGBm.DeleteObject();
if(m_MouseOverBGBm.GetSafeHandle()!=NULL)
m_MouseOverBGBm.DeleteObject();
if(m_ClickedBGBm.GetSafeHandle()!=NULL)
m_ClickedBGBm.DeleteObject();
if(m_LabelBm.GetSafeHandle()!=NULL)
m_LabelBm.DeleteObject();
if(m_MouseOverLabelBm.GetSafeHandle()!=NULL)
m_MouseOverLabelBm.DeleteObject();
if(m_ClickedLabelBm.GetSafeHandle()!=NULL)
m_ClickedLabelBm.DeleteObject();
}
BEGIN_MESSAGE_MAP(CLabelEx, CStatic)
//{{AFX_MSG_MAP(CLabelEx)
ON_WM_PAINT()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLabelEx message handlers
void CLabelEx::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.SetTextColor(m_crText);
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(this->GetFont());
///×¼±¸¹¤×÷
CRect rect;
CDC MemDC;
CPen BorderPen,*pOldPen,UnderLinePen;
CBrush BGBrush,*pOldBrush;
BITMAP bm;
this->GetClientRect(&rect);
MemDC.CreateCompatibleDC(&dc);
MemDC.SetMapMode(dc.GetMapMode());
///»­±ß¿ò
if(m_bBorder)
{
BorderPen.CreatePen(PS_SOLID,1,m_crBorder);
BGBrush.CreateSolidBrush(m_crBG);
pOldPen=dc.SelectObject(&BorderPen);
pOldBrush=dc.SelectObject(&BGBrush);
dc.Rectangle(&rect);
dc.SelectObject(pOldPen);
dc.SelectObject(pOldBrush);
rect.DeflateRect(1,1);
}
///Ìù±³¾°Í¼
if(m_bClicked && m_ClickedBGBm.GetSafeHandle()!=NULL)
{
MemDC.SelectObject(m_ClickedBGBm);
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&MemDC,0,0,SRCCOPY);
}
else if(m_bOver && m_MouseOverBGBm.GetSafeHandle()!=NULL)//Êó±ê¾­¹ýµÄʱºò
{
MemDC.SelectObject(m_MouseOverBGBm);
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&MemDC,0,0,SRCCOPY);
}
else if(m_BGBm.GetSafeHandle()!=NULL)
{
MemDC.SelectObject(m_BGBm);
dc.BitBlt(rect.left,rect.top,rect.Width(),rect.Height(),
&MemDC,0,0,SRCCOPY);
}
///Ìù±êǩͼƬ
if(m_bClicked && m_ClickedLabelBm.GetSafeHandle()!=NULL)
{
m_ClickedLabelBm.GetBitmap(&bm);
double fScal=bm.bmWidth*1.0/bm.bmHeight;
MemDC.SelectObject(m_ClickedLabelBm);
dc.StretchBlt(rect.left,rect.top,int(rect.Height()*fScal),rect.Height(),
&MemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
else if(m_bOver && m_MouseOverLabelBm.GetSafeHandle()!=NULL)
{
m_MouseOverLabelBm.GetBitmap(&bm);
double fScal=bm.bmWidth*1.0/bm.bmHeight;
MemDC.SelectObject(m_MouseOverLabelBm);
dc.StretchBlt(rect.left,rect.top,int(rect.Height()*fScal),rect.Height(),
&MemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
else if(m_LabelBm.GetSafeHandle()!=NULL)
{
m_LabelBm.GetBitmap(&bm);
double fScal=bm.bmWidth*1.0/bm.bmHeight;
MemDC.SelectObject(m_LabelBm);
dc.StretchBlt(rect.left,rect.top,int(rect.Height()*fScal),rect.Height(),
&MemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY);
}
///Êä³öÎÄ×Ö
TEXTMETRIC tm;
dc.GetTextMetrics(&tm);
CString strText1 = m_CurText1;
CString strText2 = m_CurText2;
if(strText1.GetLength()>0)
{
CRect TextRect = rect;
int AdjustPos = -8;
TextRect.bottom += AdjustPos;
TextRect.top += AdjustPos;
dc.DrawText(strText1, -1, &TextRect, DT_SINGLELINE| DT_VCENTER | DT_CENTER);
}
if(strText2.GetLength()>0)
{
CRect TextRect = rect;
int AdjustPos = 12;
TextRect.bottom += AdjustPos;
TextRect.top += AdjustPos;
dc.DrawText(strText2, -1, &TextRect, DT_SINGLELINE| DT_VCENTER | DT_CENTER);
}
}
void CLabelEx::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
SetFont(GetParent()->GetFont());
CStatic::PreSubclassWindow();
}
void CLabelEx::SetTextColor(COLORREF crText)
{
m_crText=crText;
}
void CLabelEx::SetUnderLine(BOOL bUnderLine, COLORREF crUnderLine)
{
m_bUnderLine=bUnderLine;
m_crUnderLine=crUnderLine;
}
void CLabelEx::SetBorder(BOOL bBorder, COLORREF crBorder)
{
m_bBorder=bBorder;
m_crBorder=crBorder;
}
void CLabelEx::SetBkColor(COLORREF crBkColor)
{
m_crBG=crBkColor;
}
void CLabelEx::EnableAutoUnderLine(BOOL bAutoUnderLine)
{
m_bAutoUnderLine=bAutoUnderLine;
if(m_bAutoUnderLine) ///ÉèÖÃÁË×Ô¶¯Ï»®Ïß
{
m_bBackUnderLine=m_bUnderLine;//±£´æÔ­À´Ï»®ÏßµÄ״̬
m_bUnderLine=FALSE;
}
else ///È¡ÏûÁË×Ô¶¯Ï»®Ïß
{
m_bUnderLine=m_bBackUnderLine;//»Ø¸´Ô­À´Ï»®ÏßµÄ״̬
}
}
void CLabelEx::RedrawWindow()
{
CStatic::RedrawWindow();
//RECT rect;
//this->GetWindowRect(&rect);
//GetParent()->ScreenToClient(&rect);
//GetParent()->InvalidateRect(&rect);
}
void CLabelEx::SetBGBitmap(UINT ID)
{
if(m_BGBm.GetSafeHandle()!=NULL)
m_BGBm.DeleteObject();
m_BGBm.LoadBitmap(ID);
}
void CLabelEx::SetMouseOverBGBitmap(UINT ID)
{
if(m_MouseOverBGBm.GetSafeHandle()!=NULL)
m_MouseOverBGBm.DeleteObject();
m_MouseOverBGBm.LoadBitmap(ID);
}
void CLabelEx::SetClickedBGBitmap(UINT ID)
{
if(m_ClickedBGBm.GetSafeHandle()!=NULL)
m_ClickedBGBm.DeleteObject();
m_ClickedBGBm.LoadBitmap(ID);
}
void CLabelEx::SetLabelBitmap(UINT ID)
{
if(m_LabelBm.GetSafeHandle()!=NULL)
m_LabelBm.DeleteObject();
m_LabelBm.LoadBitmap(ID);
}
void CLabelEx::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bClicked=!m_bClicked;
RedrawWindow();
CStatic::OnLButtonDown(nFlags, point);
}
void CLabelEx::SetMouseOverLabelBitmap(UINT ID)
{
if(m_MouseOverLabelBm.GetSafeHandle()!=NULL)
m_MouseOverLabelBm.DeleteObject();
m_MouseOverLabelBm.LoadBitmap(ID);
}
void CLabelEx::SetClickedLabelBitmap(UINT ID)
{
if(m_ClickedLabelBm.GetSafeHandle()!=NULL)
m_ClickedLabelBm.DeleteObject();
m_ClickedLabelBm.LoadBitmap(ID);
}
///×Ô¶¯¸ÐÓ¦×ÖÌåÑÕÉ«
void CLabelEx::EnableHighLight(BOOL bHighLight)
{
m_bHighLight=bHighLight;
}
void CLabelEx::SetLabelInfo(UINT ID,CString Text1,CString Text2)
{
if(m_CurBmpID!=ID || Text1 != m_CurText1|| Text2 != m_CurText2)
{
m_CurText1 = Text1;
m_CurText2 = Text2;
m_CurBmpID = ID;
SetLabelBitmap(ID);
RedrawWindow();//ÖØ»æ
}
}