分类: C/C++
2008-08-01 17:01:20
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; int nTextLeft=0,nTextTop=0; //文字输出的位置 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; nTextLeft=int(rect.Height()*fScal) 4; 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; nTextLeft=int(rect.Height()*fScal) 4; 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; nTextLeft=int(rect.Height()*fScal) 4; MemDC.SelectObject(m_LabelBm); dc.StretchBlt(rect.left,rect.top,int(rect.Height()*fScal),rect.Height(), &MemDC,0,0,bm.bmWidth,bm.bmHeight,SRCCOPY); } else { nTextLeft=4; } ///输出文字 TEXTMETRIC tm; dc.GetTextMetrics(&tm); CString strText; this->GetWindowText(strText); nTextTop=rect.top (rect.Height()-tm.tmHeight)/2; if(strText.GetLength()>0) { dc.TextOut(nTextLeft,nTextTop,strText); } ///画下划线 if(m_bUnderLine) { nTextLeft-=2; nTextTop=nTextTop tm.tmHeight 1; UnderLinePen.CreatePen(PS_SOLID,1,m_crUnderLine); pOldPen=dc.SelectObject(&UnderLinePen); dc.MoveTo(nTextLeft,nTextTop); dc.LineTo(nTextLeft tm.tmAveCharWidth*strText.GetLength(),nTextTop); } }注:对字体加下划线我没有使用直接设置字体下划线的方法,因为我觉得那样不好看,呵呵
void CLabelEx::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if (m_bOver) // Cursor is currently over control { CRect rect; GetClientRect(rect); if (!rect.PtInRect(point)) { m_bOver = FALSE; if(m_bAutoUnderLine) ///自动下划线 { this->SetUnderLine(FALSE,RGB(0,0,0)); } if(m_bHighLight) //自动高亮 { ///恢复原来的字体颜色 this->SetTextColor(m_crBackText); } RedrawWindow(); ReleaseCapture(); return; } } else // Cursor has just moved over control { m_bOver = TRUE; if(m_bAutoUnderLine) { this->SetUnderLine(TRUE,RGB(0,0,255)); } if(m_bHighLight) { m_crBackText=m_crText; this->SetTextColor(RGB(0,0,255)); } RedrawWindow(); SetCapture(); ::SetCursor(m_hHandCur); } CStatic::OnMouseMove(nFlags, point); }注:这种方法简单方便,但是有一个问题,看附带的工程,单击Label1弹出一个对话框后Label1无法恢复原状。我一直没解决这个问题.若谁知道请告知我 querw@sina.com 下载本文示例代码