参照网络写了个超链接类
1如果响应下划线会闪烁,现在我把这个移动到超链接上出现下划线去掉,
2还是有移动到超链接上,窗口背景会变白,被其他窗口挡住后背景才恢复窗口原来的背景头文件HyperLinkStatic.h #if !defined(AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_)
#define AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// HyperLinkStatic.h : header file///////////////////////////////////////////////////////////////////////////////// CHyperLinkStatic windowclass CHyperLinkStatic : public CStatic{// Constructionpublic:void SetUnderline(BOOL Underline);
//设置默认值参数
void SetAttrbute(CString url= _T("www.baidu.com"),CString StaticText=_T("www.baidu.com"), CString strTipInfo=_T(""),COLORREF InitColor = RGB(0,0,255),COLORREF VisitedColor = RGB(255,0,0),COLORREF CoverColor = RGB(0,125,155));CHyperLinkStatic();private:CString m_strTipInfo; //Tip提示CString m_strURL; //网址COLORREF m_cPointingColor; //COLORREF m_cVisitedColor; //COLORREF m_cNormalColor; //BOOL m_bVisited;BOOL m_bPointing;BOOL m_bNormal;BOOL m_bUnderLine;CFont m_Font; //文字字体CToolTipCtrl m_ToolTip; //设置Tip提示HCURSOR m_hCursorLink;
// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHyperLinkStatic)public:virtual BOOL PreTranslateMessage(MSG* pMsg);protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementationpublic:virtual ~CHyperLinkStatic();// Generated message map functionsprotected://{{AFX_MSG(CHyperLinkStatic)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSGDECLARE_MESSAGE_MAP()};/////////////////////////////////////////////////////////////////////////////
#endif
// HyperLinkStatic.cpp : implementation file//#include "stdafx.h"#include "HyperLinkStatic.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CHyperLinkStaticCHyperLinkStatic::CHyperLinkStatic(){m_cVisitedColor=RGB(85,26,139);m_bNormal=TRUE;m_bVisited=FALSE;m_bPointing=FALSE;
}CHyperLinkStatic::~CHyperLinkStatic(){m_Font.DeleteObject();}BEGIN_MESSAGE_MAP(CHyperLinkStatic, CStatic)//{{AFX_MSG_MAP(CHyperLinkStatic)ON_WM_MOUSEMOVE()ON_WM_CTLCOLOR_REFLECT()ON_WM_LBUTTONDOWN()ON_WM_SETCURSOR()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CHyperLinkStatic message handlersvoid CHyperLinkStatic::SetAttrbute(CString url,CString StaticText, CString strTipInfo,COLORREF InitColor, COLORREF VisitedColor,COLORREF CoverColor){
m_strURL = url;
//设置字体
m_Font.CreateFont(15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
10, // nWeight
FALSE, // bItalic 斜体
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T( "Arial ")); // lpszFac
SetFont(&m_Font);
SetWindowText(StaticText);
// 根据字体的长度和宽度设置静态框的长度和宽度
CDC *pDC = GetDC();
CString str;
GetWindowText(str);
CSize size = pDC->GetTextExtent(str, str.GetLength());
SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
ReleaseDC(pDC);
m_strTipInfo=strTipInfo;
m_cNormalColor = InitColor;
m_cVisitedColor = VisitedColor;
m_cPointingColor = CoverColor;
}
void CHyperLinkStatic::PreSubclassWindow(){
// TODO: Add your specialized code here and/or call the base class
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.Activate(TRUE);
m_ToolTip.AddTool(this, m_strTipInfo, rect, 1);
CStatic::PreSubclassWindow();
}HBRUSH CHyperLinkStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
ASSERT(nCtlColor == CTLCOLOR_STATIC); // TODO: Change any attributes of the DC here
if(m_bVisited)
pDC->SetTextColor(m_cVisitedColor);
else
{
if(m_bPointing)
{
pDC->SetTextColor(m_cPointingColor);
SetUnderline(TRUE);
}
else
{
pDC->SetTextColor(m_cNormalColor);
//SetUnderline(FALSE);
}
}
// TODO: Return a different brush if the default is not desired
pDC->SetBkMode(TRANSPARENT);//设置背景透明
return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
}
void CHyperLinkStatic::OnMouseMove(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect(rect); // 得到当前文本框的矩形区域
static BOOL bIsIn = FALSE; // 判断前一次鼠标是否已经在文本框区域
if (rect.PtInRect(point)) // 在区域内
{
m_bPointing = TRUE; // 记录鼠标已经停留在超级链接文本框的上方
if (!bIsIn) // 如果是第一次停留,则用其他颜色重绘文本
{
SetCapture(); // 设置鼠标捕获
bIsIn = TRUE;
Invalidate(); // 调用CtrlColor重绘文本
}
}
else
{
m_bPointing = FALSE; // 记录鼠标不在超级链接文本框的上方
if (bIsIn) // 如果是第一次离开区域,则用其他颜色重绘文本
{
ReleaseCapture(); // 释放鼠标捕获
bIsIn = FALSE;
Invalidate(); // 调用CtrlColor重绘文本
}
}
m_ToolTip.UpdateTipText(m_strTipInfo, this, 1); CStatic::OnMouseMove(nFlags, point);
}
void CHyperLinkStatic::OnLButtonDown(UINT nFlags, CPoint point){ // TODO: Add your message handler code here and/or call default
m_bVisited = TRUE;
ShellExecute(NULL, _T("open"), m_strURL, NULL,NULL,SW_SHOW);
CStatic::OnLButtonDown(nFlags, point);}BOOL CHyperLinkStatic::PreTranslateMessage(MSG* pMsg){// TODO: Add your specialized code here and/or call the base classm_ToolTip.RelayEvent(pMsg);return CStatic::PreTranslateMessage(pMsg);}
BOOL CHyperLinkStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){
// TODO: Add your message handler code here and/or call default
// if (m_hCursorLink == NULL)
// {
static bTriedOnce = FALSE;
if (!bTriedOnce)
{
CString windir;
GetWindowsDirectory(windir.GetBuffer(MAX_PATH), MAX_PATH);
windir.ReleaseBuffer();
windir += _T("\\winhlp32.exe");
HMODULE hModule = LoadLibrary(windir);
if (hModule)
{
m_hCursorLink =CopyCursor(::LoadCursor(hModule, MAKEINTRESOURCE(106)));
}
FreeLibrary(hModule);
bTriedOnce = TRUE;
}
// }
if (m_hCursorLink)
{
::SetCursor(m_hCursorLink);
return TRUE;
}
return FALSE;
//下面//去不去无所谓的,试过了
//return CStatic::OnSetCursor(pWnd, nHitTest, message);
}void CHyperLinkStatic::SetUnderline(BOOL Underline)//调用此函数设置下划线{
m_bUnderLine = Underline;
CFont *pFont = GetFont();//此处是我添加的
ASSERT(pFont);
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfUnderline = m_bUnderLine;
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);//我添加结束
}
1如果响应下划线会闪烁,现在我把这个移动到超链接上出现下划线去掉,
2还是有移动到超链接上,窗口背景会变白,被其他窗口挡住后背景才恢复窗口原来的背景头文件HyperLinkStatic.h #if !defined(AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_)
#define AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// HyperLinkStatic.h : header file///////////////////////////////////////////////////////////////////////////////// CHyperLinkStatic windowclass CHyperLinkStatic : public CStatic{// Constructionpublic:void SetUnderline(BOOL Underline);
//设置默认值参数
void SetAttrbute(CString url= _T("www.baidu.com"),CString StaticText=_T("www.baidu.com"), CString strTipInfo=_T(""),COLORREF InitColor = RGB(0,0,255),COLORREF VisitedColor = RGB(255,0,0),COLORREF CoverColor = RGB(0,125,155));CHyperLinkStatic();private:CString m_strTipInfo; //Tip提示CString m_strURL; //网址COLORREF m_cPointingColor; //COLORREF m_cVisitedColor; //COLORREF m_cNormalColor; //BOOL m_bVisited;BOOL m_bPointing;BOOL m_bNormal;BOOL m_bUnderLine;CFont m_Font; //文字字体CToolTipCtrl m_ToolTip; //设置Tip提示HCURSOR m_hCursorLink;
// Attributespublic:// Operationspublic:// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CHyperLinkStatic)public:virtual BOOL PreTranslateMessage(MSG* pMsg);protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL// Implementationpublic:virtual ~CHyperLinkStatic();// Generated message map functionsprotected://{{AFX_MSG(CHyperLinkStatic)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSGDECLARE_MESSAGE_MAP()};/////////////////////////////////////////////////////////////////////////////
#endif
// HyperLinkStatic.cpp : implementation file//#include "stdafx.h"#include "HyperLinkStatic.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CHyperLinkStaticCHyperLinkStatic::CHyperLinkStatic(){m_cVisitedColor=RGB(85,26,139);m_bNormal=TRUE;m_bVisited=FALSE;m_bPointing=FALSE;
}CHyperLinkStatic::~CHyperLinkStatic(){m_Font.DeleteObject();}BEGIN_MESSAGE_MAP(CHyperLinkStatic, CStatic)//{{AFX_MSG_MAP(CHyperLinkStatic)ON_WM_MOUSEMOVE()ON_WM_CTLCOLOR_REFLECT()ON_WM_LBUTTONDOWN()ON_WM_SETCURSOR()//}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CHyperLinkStatic message handlersvoid CHyperLinkStatic::SetAttrbute(CString url,CString StaticText, CString strTipInfo,COLORREF InitColor, COLORREF VisitedColor,COLORREF CoverColor){
m_strURL = url;
//设置字体
m_Font.CreateFont(15, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
10, // nWeight
FALSE, // bItalic 斜体
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
DEFAULT_QUALITY, // nQuality
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
_T( "Arial ")); // lpszFac
SetFont(&m_Font);
SetWindowText(StaticText);
// 根据字体的长度和宽度设置静态框的长度和宽度
CDC *pDC = GetDC();
CString str;
GetWindowText(str);
CSize size = pDC->GetTextExtent(str, str.GetLength());
SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
ReleaseDC(pDC);
m_strTipInfo=strTipInfo;
m_cNormalColor = InitColor;
m_cVisitedColor = VisitedColor;
m_cPointingColor = CoverColor;
}
void CHyperLinkStatic::PreSubclassWindow(){
// TODO: Add your specialized code here and/or call the base class
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.Activate(TRUE);
m_ToolTip.AddTool(this, m_strTipInfo, rect, 1);
CStatic::PreSubclassWindow();
}HBRUSH CHyperLinkStatic::CtlColor(CDC* pDC, UINT nCtlColor)
{
ASSERT(nCtlColor == CTLCOLOR_STATIC); // TODO: Change any attributes of the DC here
if(m_bVisited)
pDC->SetTextColor(m_cVisitedColor);
else
{
if(m_bPointing)
{
pDC->SetTextColor(m_cPointingColor);
SetUnderline(TRUE);
}
else
{
pDC->SetTextColor(m_cNormalColor);
//SetUnderline(FALSE);
}
}
// TODO: Return a different brush if the default is not desired
pDC->SetBkMode(TRANSPARENT);//设置背景透明
return (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
}
void CHyperLinkStatic::OnMouseMove(UINT nFlags, CPoint point){// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect(rect); // 得到当前文本框的矩形区域
static BOOL bIsIn = FALSE; // 判断前一次鼠标是否已经在文本框区域
if (rect.PtInRect(point)) // 在区域内
{
m_bPointing = TRUE; // 记录鼠标已经停留在超级链接文本框的上方
if (!bIsIn) // 如果是第一次停留,则用其他颜色重绘文本
{
SetCapture(); // 设置鼠标捕获
bIsIn = TRUE;
Invalidate(); // 调用CtrlColor重绘文本
}
}
else
{
m_bPointing = FALSE; // 记录鼠标不在超级链接文本框的上方
if (bIsIn) // 如果是第一次离开区域,则用其他颜色重绘文本
{
ReleaseCapture(); // 释放鼠标捕获
bIsIn = FALSE;
Invalidate(); // 调用CtrlColor重绘文本
}
}
m_ToolTip.UpdateTipText(m_strTipInfo, this, 1); CStatic::OnMouseMove(nFlags, point);
}
void CHyperLinkStatic::OnLButtonDown(UINT nFlags, CPoint point){ // TODO: Add your message handler code here and/or call default
m_bVisited = TRUE;
ShellExecute(NULL, _T("open"), m_strURL, NULL,NULL,SW_SHOW);
CStatic::OnLButtonDown(nFlags, point);}BOOL CHyperLinkStatic::PreTranslateMessage(MSG* pMsg){// TODO: Add your specialized code here and/or call the base classm_ToolTip.RelayEvent(pMsg);return CStatic::PreTranslateMessage(pMsg);}
BOOL CHyperLinkStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message){
// TODO: Add your message handler code here and/or call default
// if (m_hCursorLink == NULL)
// {
static bTriedOnce = FALSE;
if (!bTriedOnce)
{
CString windir;
GetWindowsDirectory(windir.GetBuffer(MAX_PATH), MAX_PATH);
windir.ReleaseBuffer();
windir += _T("\\winhlp32.exe");
HMODULE hModule = LoadLibrary(windir);
if (hModule)
{
m_hCursorLink =CopyCursor(::LoadCursor(hModule, MAKEINTRESOURCE(106)));
}
FreeLibrary(hModule);
bTriedOnce = TRUE;
}
// }
if (m_hCursorLink)
{
::SetCursor(m_hCursorLink);
return TRUE;
}
return FALSE;
//下面//去不去无所谓的,试过了
//return CStatic::OnSetCursor(pWnd, nHitTest, message);
}void CHyperLinkStatic::SetUnderline(BOOL Underline)//调用此函数设置下划线{
m_bUnderLine = Underline;
CFont *pFont = GetFont();//此处是我添加的
ASSERT(pFont);
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfUnderline = m_bUnderLine;
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);//我添加结束
}
解决方案 »
- 请问一个问题,VS2010 里生成文件时,输出窗提醒太多了。
- MFC下做过视频音频通讯开发的进来谈谈看法~
- Sendmessage--内存释放
- 我有一组数:1、2、3、4、1、2、5、1、1、3 要在combo下拉控件中显示,但是不能有重复的出现?
- VC 如何实时得到 Webbrowser的进度?
- 鼠标移动到一条曲线上的一个特定点A时,变化鼠标cursor,但是新的cursor老是跑到离特定点A三四个像素外的位置,怎么能让它变化后仍停留在A
- 位图显示的问题
- 调用ColeDateTime 和ColeDateTimeSpan 要用哪些头文件
- 很基础的问题 谢谢!!
- 为什么我编译后的执行文件不执行?
- 两个线程对同一张位图进行读写。
- hello
// LinkWnd.cpp : 实现文件
//#include "stdafx.h"
#include "LinkWnd.h"
// CLinkWndIMPLEMENT_DYNAMIC(CLinkWnd, CStatic)CLinkWnd::CLinkWnd()
:m_hHand(NULL),m_hNormal(NULL),m_bMouseHover(FALSE),m_bAlwaysUnderLine(FALSE),m_bUnderLine(FALSE)
{}CLinkWnd::~CLinkWnd()
{}
BEGIN_MESSAGE_MAP(CLinkWnd, CStatic)
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_MOUSELEAVE()
ON_WM_SETCURSOR()
END_MESSAGE_MAP()// CLinkWnd 消息处理程序
void CLinkWnd::SetCaption(CString strCaption)
{
m_strCaption = strCaption;
}void CLinkWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (!m_bMouseHover)
{
TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT), TME_LEAVE, m_hWnd, 0};
m_bMouseHover = TrackMouseEvent(&tme);
::SetCursor(m_hHand);
Invalidate();
}
CStatic::OnMouseMove(nFlags, point);
}void CLinkWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CStatic::OnPaint()
CDC *pParentDC = GetParent()->GetDC();
if (pParentDC != NULL)
{
dc.SetBkColor(pParentDC->GetBkColor());
} CRect rc;
GetClientRect(&rc);
COLORREF textColor;
CSize sz = dc.GetTextExtent(m_strCaption,(int)_tcslen(m_strCaption));
CPoint Start((rc.Width()-sz.cx)/2, (rc.Height()-sz.cy)/2+sz.cy);
CPoint end(rc.Width()/2+sz.cx/2, (rc.Height()-sz.cy)/2+sz.cy); dc.SetBkMode(TRANSPARENT);
if (m_bMouseHover == TRUE)
{
textColor = RGB(100,100,155);
if (m_bAlwaysUnderLine == FALSE)
{
DrawLine(&dc, Start, end);
m_bUnderLine = TRUE;
}
}
else
{
textColor = RGB(200,200,255);
if ((m_bAlwaysUnderLine == TRUE && m_bUnderLine == FALSE) || (m_bAlwaysUnderLine == FALSE && m_bUnderLine == TRUE))
{
DrawLine(&dc, Start, end);
m_bUnderLine = TRUE;
}
} dc.SetTextColor(textColor);
dc.DrawText(m_strCaption, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}int CLinkWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
m_hNormal = AfxGetApp()->LoadStandardCursor(IDC_IBEAM);
m_hHand = AfxGetApp()->LoadStandardCursor(IDC_HAND); return 0;
}void CLinkWnd::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_bMouseHover = FALSE;
::SetCursor(m_hNormal); Invalidate(); CStatic::OnMouseLeave();
}BOOL CLinkWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
return TRUE;
return CStatic::OnSetCursor(pWnd, nHitTest, message);
}void CLinkWnd::DrawLine(CDC* pDC, CPoint& pStart, CPoint& pEnd)
{
CPen newPen(PS_SOLID, 1, RGB(0,0,0));
CPen* pOldPen = pDC->SelectObject(&newPen);
long lOldMode = pDC->SetROP2(R2_NOTXORPEN);
pDC->MoveTo(pStart);
pDC->LineTo(pEnd);
pDC->SetROP2(lOldMode);
pDC->SelectObject(pOldPen);
}void CLinkWnd::SetAlwaysUnderline(BOOL bUnderline)
{
m_bAlwaysUnderLine = bUnderline;
}