上一篇:类似 MSN 信息发送框的制作(上) >>
多功能标签CLabelEx
|
多功能标签CLabelEx 下载源代码
1、其实就是在OnPaint()里画出各种效果: 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); } } 注:对字体加下划线我没有使用直接设置字体下划线的方法,因为我觉得那样不好看,呵呵 2、感应鼠标用的方法如下所示: 在MouseMove里SetCapture()和ReleaseCapture(); 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 |
下一篇:一步步制作真彩工具条 >>
相关文章:
- · WTL字体类
- · CTreeCtrl类的递归使用
- · 类似EXCEL的报表类库
- · 实现类似VC中可设断点的编辑窗口
- · VC学习笔记之一:怎样实现XP风格按钮
- · 增强GridCtrl
- · 支持数据项查找功能的树控制(CTreeCtrl)类
- · 如何锁定 ListView 的栏目头宽度
- · 带文字的进度条
- · 三态选择树实现终结者
- · 如何定制对话框系统菜单
- · 让CButtonST 类支持鼠标掠过时发声
- · 可以显示多行文字的工具条
- · 利用钩子实现菜单阴影效果
- · 动态真彩工具栏
- · 可以替代系统记事本的程序
- · 如何实现三态选择树
- · 一个好用的DBGRID
- · 告别图标失真的烦恼
- · 自绘按钮补遗
- · 按钮控件的使用
- · 图形超链接类
- · VC中多语言菜单的实现
- · 一种漂亮的自绘菜单
- · CGfxOutBarCtrl之提升篇
- · 颜色选择组合框
- · 如何去掉浮动工具条中的“关闭”按钮
- · 设计XP风格的按钮
- · 实现类似Excel和Visual C++里文件夹式样的标签…
- · 实现类似Excel和Visual C++里文件夹式样的标签…
- · 实现类似Excel和Visual C++里文件夹式样的标签…
- · CGfxOutBarCtrl之BUG篇
- · 如何实现Office式样的扁平组合框
- · CButtonST类公共接口函数
- · CButtonST使用技巧(三)
- · CButtonST使用技巧(二)
- · CButtonST使用技巧(一)
- · CButtonST使用技巧: CButtonST简介
