扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
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); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者