天刀觉智大师线索:靜态文本 链接控件 背景透明的静态文本
来源:百度文库 编辑:中财网 时间:2024/06/28 10:51:14
VC中显示文本的控件实在用着不爽,于是自己查了一些资料,自己写一个类,与大家分享:)
view plaincopy to clipboardprint?
- 头文件:TFLinkStaticCtrl.h
- #if !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)
- #define AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- // TFLinkStaticCtrl.h : header file
- //
- //////////////////////////////////////////////////////////////////////////
- //*功能: 靜态文本/链接/背景透明
- //*作者: 童方
- //*联系:QQ:58408454 Mail:shfhere@qq.com
- //*时间: 2010年4月整理
- //////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // CTFLinkStaticCtrl window
- class CTFLinkStaticCtrl : public CStatic
- {
- // Construction
- public:
- CTFLinkStaticCtrl();
- // Attributes
- public:
- COLORREF TextColorNormal; //*文本颜色
- COLORREF TextColorMouseHover; //*鼠标移到控件上面时的文本颜色(链接功能时有效)
- BOOL UnderlineNormal; //*是否带下划线
- BOOL UnderlineMouseHover; //*鼠标移到控件上面时是否带下划线(链接功能时有效)
- BOOL Linked; //*是否开启链接功能
- // Operations
- public:
- //*重载处理文本内容函数
- void SetWindowText(LPCTSTR lpString);
- CString GetWindowText();
- int GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const;
- void GetWindowText(CString& rString) const;
- //*设置字体
- void SetFont(LPLOGFONT plf);
- //*打开网址,可以控件的OnClick事件中调用
- void OpenUrl(LPCTSTR lpszUrl);
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CTFLinkStaticCtrl)
- protected:
- virtual void PreSubclassWindow();
- //}}AFX_VIRTUAL
- // Implementation
- public:
- virtual ~CTFLinkStaticCtrl();
- // Generated message map functions
- protected:
- //{{AFX_MSG(CTFStatic)
- afx_msg void OnPaint();
- afx_msg void OnMouseMove(UINT nFlags, CPoint point);
- afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
- afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
- //}}AFX_MSG
- afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
- afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
- DECLARE_MESSAGE_MAP()
- private:
- //*初始化控件
- void InitControl();
- //*控件字体
- CFont* ControlFont;
- //*鼠标是否在控件上面
- BOOL IsMouseHovered;
- //*是否按下鼠标左键
- BOOL IsLButtonDown;
- //*处理MouseMove事件时的标志变量
- BOOL m_bTracking;
- //*开启链接效果时父窗口背景备份
- CBitmap *m_pBackupBackground;
- //*文本占用区域
- CRect RectText;
- //*文本内容
- CString WindowText;
- };
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
- #endif // !defined(AFX_TFLINKSTATICCTRL_H__5B30D3A6_6B9E_4C75_853F_557EF635C796__INCLUDED_)
- 实现文件:TFLinkStaticCtrl.cpp
- // TFLinkStaticCtrl.cpp : implementation file
- //
- #include "stdafx.h"
- #include "TFLinkStaticCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #if(WINVER < 0x500)
- #undef WINVER
- #define WINVER 0x500
- #define IDC_HAND MAKEINTRESOURCE(32649)
- #endif
- //////////////////////////////////////////////////////////////////////////
- //*功能: 靜态文本/链接/背景透明
- //*作者: 童方
- //*联系:QQ:58408454 Mail:shfhere@qq.com
- //*时间: 2010年4月整理
- //////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- // CTFLinkStaticCtrl
- CTFLinkStaticCtrl::CTFLinkStaticCtrl()
- {
- //*设置成员变量初始值
- ControlFont = NULL;
- IsMouseHovered = FALSE;
- IsLButtonDown = FALSE;
- m_bTracking = FALSE;
- TextColorNormal = RGB(0,0,0);
- TextColorMouseHover = RGB(0,0,0);
- UnderlineNormal = FALSE;
- UnderlineMouseHover = FALSE;
- m_pBackupBackground = NULL;
- Linked = FALSE;
- }
- CTFLinkStaticCtrl::~CTFLinkStaticCtrl()
- {
- //*释放字体对象
- if(ControlFont)
- {
- ControlFont->DeleteObject();
- delete ControlFont;
- ControlFont = NULL;
- }
- //*释放背景位图对象
- if(m_pBackupBackground)
- {
- m_pBackupBackground->DeleteObject();
- delete m_pBackupBackground;
- m_pBackupBackground = NULL;
- }
- }
- BEGIN_MESSAGE_MAP(CTFLinkStaticCtrl, CStatic)
- //{{AFX_MSG_MAP(CTFLinkStaticCtrl)
- ON_WM_PAINT()
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
- ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTFLinkStaticCtrl message handlers
- //*初始化控件
- void CTFLinkStaticCtrl::InitControl()
- {
- //*如果已经初始化,则返回
- if(ControlFont)
- return;
- //*修改控件样式,使控件可以收到鼠标消息通知
- ModifyStyle(0, SS_NOTIFY|BS_OWNERDRAW);
- //*得到父窗口
- CWnd* pWnd = GetParent();
- if(!pWnd)
- return;
- //*得到父窗口字体信息
- LOGFONT lf;
- pWnd->GetFont()->GetLogFont(&lf);
- //*将当前控件字体设置为父窗口字体
- ControlFont = new CFont();
- ControlFont->CreateFontIndirect(&lf);
- CStatic::SetFont(ControlFont);
- CStatic::GetWindowText(WindowText);
- }
- //*设置字体
- void CTFLinkStaticCtrl::SetFont(LPLOGFONT plf)
- {
- //*判断传如参数是否为空
- if(plf == NULL)
- return;
- //*去掉下划线属性, 因为在OnPaint中手动绘下划线了
- plf->lfUnderline = FALSE;
- CFont* pFont = new CFont();
- pFont->CreateFontIndirect(plf);
- CStatic::SetFont(pFont);
- delete ControlFont;
- ControlFont = pFont;
- }
- void CTFLinkStaticCtrl::OnPaint()
- {
- //*初始化控件
- // InitControl();
- CPaintDC dc(this);
- //*设置背景模式为透明
- dc.SetBkMode(TRANSPARENT);
- //*载入控件当前字体
- CFont* pOldFont;
- pOldFont = (CFont*)dc.SelectObject(ControlFont);
- //*得到字体信息
- TEXTMETRIC tmText;
- dc.GetTextMetrics(&tmText);
- CString Text;
- //*得到窗口文本
- GetWindowText(Text);
- dc.SetTextColor(TextColorNormal);
- //*如果首次运行, 备份背景内容
- if(m_pBackupBackground == NULL)
- {
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- GetClientRect(&RectText);
- m_pBackupBackground = new CBitmap();
- m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());
- dcMem.SelectObject(m_pBackupBackground);
- dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);
- dcMem.DeleteDC();
- }
- else
- {
- //*非首次运行时, 将背景还原
- CDC dcTmp;
- dcTmp.CreateCompatibleDC(&dc);
- dcTmp.SelectObject(m_pBackupBackground);
- dc.BitBlt(0, 0, RectText.Width(), RectText.Height()+1, &dcTmp, 0, 0, SRCCOPY);
- dcTmp.DeleteDC();
- //*计算当前文本区域
- CRect RectTmp;
- GetClientRect(&RectTmp);
- //*如果与原来文本区域不同, 则重新备份背景区域
- if(RectTmp != RectText)
- {
- RectText = RectTmp;
- m_pBackupBackground->DeleteObject();
- CDC dcMem;
- dcMem.CreateCompatibleDC(&dc);
- m_pBackupBackground->CreateCompatibleBitmap(&dc, RectText.Width(), RectText.Height());
- dcMem.SelectObject(m_pBackupBackground);
- dcMem.BitBlt(0, 0, RectText.Width(), RectText.Height(), &dc, 0, 0, SRCCOPY);
- dcMem.DeleteDC();
- }
- }
- //*如果开启链接效果
- if(Linked)
- {
- //*如果鼠标移动到控件上面
- if(IsMouseHovered)
- {
- //*设置当前文本颜色为TextColorMouseHover
- dc.SetTextColor(TextColorMouseHover);
- //*如果显示下划线, 则绘制下划线
- if(UnderlineMouseHover)
- {
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorMouseHover);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- }
- else
- {
- //*如果鼠标不在控件上面, 则正常绘制
- dc.SetTextColor(TextColorNormal);
- if(UnderlineNormal)
- {
- //*绘制下划线
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorNormal);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- }
- }
- else
- {
- if(UnderlineNormal)
- {
- //*绘制下划线
- CPen Pen, *pOldPen;
- Pen.CreatePen(PS_SOLID, 1, TextColorNormal);
- pOldPen = (CPen*)dc.SelectObject(&Pen);
- dc.MoveTo(0, tmText.tmHeight);
- dc.LineTo(tmText.tmAveCharWidth * Text.GetLength(), tmText.tmHeight);
- dc.SelectObject(pOldPen);
- Pen.DeleteObject();
- }
- dc.SetTextColor(TextColorNormal);
- }
- //*绘制文本
- dc.TextOut(0, 0, Text);
- dc.SelectObject(pOldFont);
- }
- void CTFLinkStaticCtrl::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(!Linked)
- return;
- //*分别处理鼠标移到控件上与移出控件的消息
- if (!m_bTracking)
- {
- TRACKMOUSEEVENT tme;
- tme.cbSize = sizeof(tme);
- tme.hwndTrack = m_hWnd;
- tme.dwFlags = TME_LEAVE | TME_HOVER;
- tme.dwHoverTime = 1;
- m_bTracking = _TrackMouseEvent(&tme);
- }
- CStatic::OnMouseMove(nFlags, point);
- }
- LRESULT CTFLinkStaticCtrl::OnMouseLeave(WPARAM wParam, LPARAM lParam)
- {
- m_bTracking = FALSE;
- IsMouseHovered = FALSE;
- HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_ARROW));
- Invalidate();
- return 0;
- }
- LRESULT CTFLinkStaticCtrl::OnMouseHover(WPARAM wParam, LPARAM lParam)
- {
- m_bTracking = TRUE;
- IsMouseHovered = TRUE;
- HCURSOR hCursor = SetCursor(::LoadCursor(NULL, IDC_HAND));
- Invalidate();
- return 0;
- }
- void CTFLinkStaticCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(!Linked)
- return;
- SetCapture();
- IsLButtonDown = TRUE;
- BringWindowToTop();
- CStatic::OnLButtonDown(nFlags, point);
- }
- void CTFLinkStaticCtrl::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(IsLButtonDown)
- {
- //*向父窗口发送点击消息
- IsLButtonDown = FALSE;
- ReleaseCapture();
- CWnd* pWnd = GetParent();
- if(pWnd)
- {
- pWnd->SendMessage(BN_CLICKED, (WPARAM)GetDlgCtrlID(), (LPARAM)GetSafeHwnd());
- }
- }
- CStatic::OnLButtonUp(nFlags, point);
- }
- void CTFLinkStaticCtrl::PreSubclassWindow()
- {
- // TODO: Add your specialized code here and/or call the base class
- InitControl();
- CStatic::PreSubclassWindow();
- }
- void CTFLinkStaticCtrl::SetWindowText(LPCTSTR lpString)
- {
- WindowText = lpString;
- Invalidate();
- }
- CString CTFLinkStaticCtrl::GetWindowText()
- {
- return WindowText;
- }
- int CTFLinkStaticCtrl::GetWindowText(LPTSTR lpszStringBuf, int nMaxCount) const
- {
- int nLen = WindowText.GetLength();
- if(nLen <= nMaxCount)
- {
- _stprintf(lpszStringBuf, _T("%s"), WindowText);
- return nLen;
- }
- else
- {
- _stprintf(lpszStringBuf, _T("%s"), WindowText.Left(nMaxCount));
- return nMaxCount;
- }
- }
- void CTFLinkStaticCtrl::GetWindowText(CString& rString) const
- {
- rString = WindowText;
- }
- void CTFLinkStaticCtrl::OpenUrl(LPCTSTR lpszUrl)
- {
- ShellExecute(NULL, _T("open"), lpszUrl, NULL, NULL, SW_SHOW);
- }
使用说明:
1、将该类加入到工程
2、在窗口上添加Static控件并设置相应的ID
3、映射为CTFLinkStaticCtrl类的成员变量
4、初始化
如:m_Link, m_Show
链接效果初始化
m_Link.TextColorNormal = RGB(0,0,255);
m_Link.TextColorMouseHover = RGB(0,0,255);
m_Link.UnderlineNormal = FALSE;
m_Link.UnderlineMouseHover = TRUE;
m_Link.Linked = TRUE;
静态文本初始化
m_Show.TextColorNormal = RGB(255,0,0);
m_Show.Linked = FALSE;
5、响应点击(只有Linked属性设置为TRUE才响应此事件)
用向导为相应的Static添加OnClick事件
void YourClass::OnLinkClick()
{
//如果转向网页,则调用
m_Link.OpenURL(www.baidu.com);
//如果响应其他功能,则相当于按钮的点击事件,添加你需要的功能
}
示例代码:http://download.csdn.net/source/2230734
什么是静态文本控件
静态文本控件怎么添加成员变量的
透明的文本编辑器
什么是静态文本,动态文本,输入文本
什么是静态文本
请问怎么控制datagrid控件中链接列的文本字符长度呢?
请问在VC++中,静态文本控件的名称是什么。只告诉读者标识号是IDC_STATIC,那个编书的是畜生!
如何得到目标应用程序LABEL控件的文本值?
网页上的文本控件都无法输入?
如何实现image控件的背景透明(delphi)
在Dreamweaver的文本链接中怎样去掉下划线
在Dreamweaver的文本链接中怎么去掉下划线
怎么改变DreamweaverMX2004表单中的文本字段的背景颜色
怎么改变DreamweaverMX2004表单中的文本字段的背景颜色??
HTML文件中怎么 改变文本的字体大小 以及背景
求一个背景是黑色的文本阅览器
如何把VB文本控件里选中的文本赋值给windows下的光标所在处?
做网页链接时,被链接的文本自动产生了下划线,怎样用HTML去掉下划线?
怎么在richTextBox中加入文本链接????
VC中,如何通过调用字体通用对话框来改变一个控件的文本的颜色
编一个java程序包含文本域和文本区控件的窗口。。
VB中的TextBox控件的文本超出时怎样自动添加滚动条?
VB中用什么控件显示资源管理器中显示文本的图标
Dreamweaver+asp做网页中用什么控件来保存文本的格式