源代码及可执行文件下载地址: http://files.cnblogs.com/rainboy2010/HyperLink.zip

在Web页面上,我们经常看到超链接,那么在VC中如何实现超链接呢?我们可以通过对Static控件进行自绘来实现超链接的效果和功能。
主要代码如下:
#include "stdafx.h"
#include "HyperLink.h"
#include "atlconv.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TOOLTIP_ID 1
CHyperLink::CHyperLink()
{
m_hLinkCursor = NULL; // No cursor as yet
m_crLinkColour = RGB( 0, 0, 255); // Blue
m_crVisitedColour = RGB( 85, 26, 139); // Purple
m_crHoverColour = RGB(255, 0, 0); // Red
m_bOverControl = FALSE; // Cursor not yet over control
m_bVisited = FALSE; // Hasn‘t been visited yet.
m_nUnderline = ulHover; // Underline the link?
m_bAdjustToFit = TRUE; // Resize the window to fit the text?
m_strURL.Empty();
m_nTimerID = 100;
}
CHyperLink::~CHyperLink()
{
m_UnderlineFont.DeleteObject();
}
BOOL CHyperLink::DestroyWindow()
{
KillTimer(m_nTimerID);
return CStatic::DestroyWindow();
}
BOOL CHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
void CHyperLink::PreSubclassWindow()
{
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
// Set the URL as the window text
if (m_strURL.IsEmpty())
GetWindowText(m_strURL);
// Check that the window text isn‘t empty. If it is, set it as the URL.
CString strWndText;
GetWindowText(strWndText);
if (strWndText.IsEmpty())
{
ASSERT(!m_strURL.IsEmpty()); // Window and URL both NULL. DUH!
SetWindowText(m_strURL);
}
CFont* pFont = GetFont();
if (!pFont)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
if (hFont)
pFont = CFont::FromHandle(hFont);
}
ASSERT(pFont->GetSafeHandle());
// Create the underline font
LOGFONT lf;
pFont->GetLogFont(&lf);
m_StdFont.CreateFontIndirect(&lf);
lf.lfUnderline = (BYTE) TRUE;
m_UnderlineFont.CreateFontIndirect(&lf);
PositionWindow(); // Adjust size of window to fit URL if necessary
SetDefaultCursor(); // Try and load up a "hand" cursor
SetUnderline();
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
CStatic::PreSubclassWindow();
}
BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
void CHyperLink::OnClicked()
{
m_bOverControl = FALSE;
int result = (int)GotoURL(m_strURL, SW_SHOW);
m_bVisited = (result > HINSTANCE_ERROR);
if (!m_bVisited)
{
MessageBeep(MB_ICONEXCLAMATION); // Unable to follow link
ReportError(result);
}
else
{
SetVisited();
}
}
HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
ASSERT(nCtlColor == CTLCOLOR_STATIC);
if (m_bOverControl)
pDC->SetTextColor(m_crHoverColour);
else if (m_bVisited)
pDC->SetTextColor(m_crVisitedColour);
else
pDC->SetTextColor(m_crLinkColour);
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
void CHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bOverControl) // Cursor has just moved over control
{
m_bOverControl = TRUE;
if (m_nUnderline == ulHover)
{
SetFont(&m_UnderlineFont);
}
Invalidate();
SetTimer(m_nTimerID, 100, NULL);
}
CStatic::OnMouseMove(nFlags, point);
}
void CHyperLink::OnTimer(UINT nIDEvent)
{
CPoint p(GetMessagePos());
ScreenToClient(&p);
CRect rect;
GetClientRect(rect);
if (!rect.PtInRect(p))
{
m_bOverControl = FALSE;
KillTimer(m_nTimerID);
if (m_nUnderline != ulAlways)
{
SetFont(&m_StdFont);
}
rect.bottom+=10;
InvalidateRect(rect);
}
CStatic::OnTimer(nIDEvent);
}
BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
{
if (m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
}
BOOL CHyperLink::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
pDC->FillSolidRect(rect, ::GetSysColor(COLOR_3DFACE));
return TRUE;
}
void CHyperLink::SetURL(CString strURL)
{
m_strURL = strURL;
if (::IsWindow(GetSafeHwnd()))
{
PositionWindow();
m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
}
}
CString CHyperLink::GetURL() const
{
return m_strURL;
}
void CHyperLink::SetColours(COLORREF crLinkColour, COLORREF crVisitedColour, COLORREF crHoverColour /* = -1 */)
{
m_crLinkColour = crLinkColour;
m_crVisitedColour = crVisitedColour;
if (crHoverColour == -1)
{
m_crHoverColour = ::GetSysColor(COLOR_HIGHLIGHT);
}
else
{
m_crHoverColour = crHoverColour;
}
if (::IsWindow(m_hWnd))
{
Invalidate();
}
}
COLORREF CHyperLink::GetLinkColour() const
{
return m_crLinkColour;
}
COLORREF CHyperLink::GetVisitedColour() const
{
return m_crVisitedColour;
}
COLORREF CHyperLink::GetHoverColour() const
{
return m_crHoverColour;
}
void CHyperLink::SetVisited(BOOL bVisited)
{
m_bVisited = bVisited;
if (::IsWindow(GetSafeHwnd()))
{
Invalidate();
}
}
BOOL CHyperLink::GetVisited() const
{
return m_bVisited;
}
void CHyperLink::SetLinkCursor(HCURSOR hCursor)
{
m_hLinkCursor = hCursor;
if (m_hLinkCursor == NULL)
{
SetDefaultCursor();
}
}
HCURSOR CHyperLink::GetLinkCursor() const
{
return m_hLinkCursor;
}
void CHyperLink::SetUnderline(int nUnderline /*=ulHover*/)
{
if (m_nUnderline == nUnderline)
return;
if (::IsWindow(GetSafeHwnd()))
{
if (nUnderline == ulAlways)
SetFont(&m_UnderlineFont);
else
SetFont(&m_StdFont);
Invalidate();
}
m_nUnderline = nUnderline;
}
int CHyperLink::GetUnderline() const
{
return m_nUnderline;
}
void CHyperLink::SetAutoSize(BOOL bAutoSize)
{
m_bAdjustToFit = bAutoSize;
if (::IsWindow(GetSafeHwnd()))
{
PositionWindow();
}
}
BOOL CHyperLink::GetAutoSize() const
{
return m_bAdjustToFit;
}
void CHyperLink::SetFontStyle(LONG lfHeight,LONG lfWeight,LPCTSTR lfFaceName)
{
LOGFONT lf={0};
lf.lfHeight=lfHeight;
lf.lfWeight=lfWeight;
lstrcpy(lf.lfFaceName,lfFaceName);
if(m_StdFont.GetSafeHandle())
{
m_StdFont.DeleteObject();
}
if(m_UnderlineFont.GetSafeHandle())
{
m_UnderlineFont.DeleteObject();
}
m_StdFont.CreateFontIndirect(&lf);
lf.lfUnderline = (BYTE)TRUE;
m_UnderlineFont.CreateFontIndirect(&lf);
if (::IsWindow(GetSafeHwnd()))
{
PositionWindow();
if (m_nUnderline == ulAlways)
{
SetFont(&m_UnderlineFont);
}
else
{
SetFont(&m_StdFont);
}
Invalidate();
}
}
void CHyperLink::PositionWindow()
{
if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit) return;
CRect WndRect, ClientRect;
GetWindowRect(WndRect);
GetClientRect(ClientRect);
ClientToScreen(ClientRect);
CWnd* pParent = GetParent();
if (pParent)
{
pParent->ScreenToClient(WndRect);
pParent->ScreenToClient(ClientRect);
}
// Get the size of the window text
CString strWndText;
GetWindowText(strWndText);
CDC* pDC = GetDC();
CFont* pOldFont = pDC->SelectObject(&m_UnderlineFont);
CSize Extent = pDC->GetTextExtent(strWndText);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
// Adjust for window borders
Extent.cx += WndRect.Width() - ClientRect.Width();
Extent.cy += WndRect.Height() - ClientRect.Height();
// Get the text justification via the window style
DWORD dwStyle = GetStyle();
// Recalc the window size and position based on the text justification
if (dwStyle & SS_CENTERIMAGE)
WndRect.DeflateRect(0, (WndRect.Height() - Extent.cy)/2);
else
WndRect.bottom = WndRect.top + Extent.cy;
if (dwStyle & SS_CENTER)
WndRect.DeflateRect((WndRect.Width() - Extent.cx)/2, 0);
else if (dwStyle & SS_RIGHT)
WndRect.left = WndRect.right - Extent.cx;
else // SS_LEFT = 0, so we can‘t test for it explicitly
WndRect.right = WndRect.left + Extent.cx;
// Move the window
SetWindowPos(NULL, WndRect.left, WndRect.top, WndRect.Width(), WndRect.Height(), SWP_NOZORDER);
}
void CHyperLink::SetDefaultCursor()
{
if (m_hLinkCursor == NULL) // No cursor handle - load our own
{
m_hLinkCursor=::LoadCursor(NULL,IDC_HAND);
if (m_hLinkCursor == NULL)
{
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\\winhlp32.exe");
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule)
{
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
}
LONG CHyperLink::GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata)
{
HKEY hkey;
LONG retval = RegOpenKeyEx(key, subkey, 0, KEY_QUERY_VALUE, &hkey);
if (retval == ERROR_SUCCESS)
{
long datasize = MAX_PATH;
TCHAR data[MAX_PATH]={0};
RegQueryValue(hkey, NULL, data, &datasize);
lstrcpy(retdata,data);
RegCloseKey(hkey);
}
return retval;
}
void CHyperLink::ReportError(int nError)
{
CString str;
switch (nError) {
case 0: str = _T("The operating system is out\nof memory or resources."); break;
case SE_ERR_PNF: str = _T("The specified path was not found."); break;
case SE_ERR_FNF: str = _T("The specified file was not found."); break;
case ERROR_BAD_FORMAT: str = _T("The .EXE file is invalid\n(non-Win32.EXE or error in .EXE image)."); break;
case SE_ERR_ACCESSDENIED: str = _T("The operating system denied\naccess to the specified file."); break;
case SE_ERR_ASSOCINCOMPLETE: str = _T("The filename association is\nincomplete or invalid."); break;
case SE_ERR_DDEBUSY: str = _T("The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."); break;
case SE_ERR_DDEFAIL: str = _T("The DDE transaction failed."); break;
case SE_ERR_DDETIMEOUT: str = _T("The DDE transaction could not\nbe completed because the request timed out."); break;
case SE_ERR_DLLNOTFOUND: str = _T("The specified dynamic-link library was not found."); break;
case SE_ERR_NOASSOC: str = _T("There is no application associated\nwith the given filename extension."); break;
case SE_ERR_OOM: str =_T( "There was not enough memory to complete the operation."); break;
case SE_ERR_SHARE: str = _T("A sharing violation occurred. ");
default: str.Format(_T("Unknown Error (%d) occurred."), nError); break;
}
str = _T("Unable to open hyperlink:\n\n") + str;
AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
}
HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
{
TCHAR key[MAX_PATH + MAX_PATH];
HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
if ((UINT)result <= HINSTANCE_ERROR)
{
if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) == ERROR_SUCCESS)
{
lstrcat(key, _T("\\shell\\open\\command"));
if (GetRegKey(HKEY_CLASSES_ROOT,key,key) == ERROR_SUCCESS)
{
TCHAR *pos;
pos = _tcsstr(key, _T("\"%1\""));
if (pos == NULL) { // No quotes found
pos = _tcsstr(key, _T("%1")); // Check for %1, without quotes
if (pos == NULL) // No parameter at all...
pos = key+lstrlen(key)-1;
else
*pos = ‘\0‘; // Remove the parameter
}
else
*pos = ‘\0‘; // Remove the parameter
lstrcat(pos, _T(" "));
lstrcat(pos, url);
USES_CONVERSION;
result = (HINSTANCE) WinExec(T2A(key),showcmd);
}
}
}
return result;
}
原文:http://www.cnblogs.com/rainboy2010/p/4237639.html