//HyperLink.h #include "stdafx.h" #include "HyperLink.h" #include <afxconv.h> // For LPTSTR -> LPSTR macros #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif#define TOOLTIP_ID 1///////////////////////////////////////////////////////////////////////////// // CHyperLinkCHyperLink::CHyperLink() { m_hLinkCursor = NULL; // No cursor as yet m_bOverControl = FALSE; // Cursor not yet over control m_strURL.Empty(); }CHyperLink::~CHyperLink() { m_Font.DeleteObject(); }BEGIN_MESSAGE_MAP(CHyperLink, CStatic) //{{AFX_MSG_MAP(CHyperLink) ON_CONTROL_REFLECT(STN_CLICKED, OnClicked) ON_WM_CTLCOLOR_REFLECT() ON_WM_SETCURSOR() ON_WM_MOUSEMOVE() //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CHyperLink message handlersvoid CHyperLink::OnClicked() { m_bOverControl = FALSE; GotoURL(m_strURL, SW_SHOW); }HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor) { ASSERT(nCtlColor == CTLCOLOR_STATIC); UNUSED(nCtlColor); if (m_bOverControl) pDC->SetTextColor(0xFF0080); else pDC->SetTextColor(0xFF0000); // transparent text. pDC->SetBkMode(TRANSPARENT); return (HBRUSH)GetStockObject(NULL_BRUSH); } BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/) { if (m_hLinkCursor) { ::SetCursor(m_hLinkCursor); return TRUE; } return FALSE; }void CHyperLink::OnMouseMove(UINT nFlags, CPoint point) { CStatic::OnMouseMove(nFlags, point); if (m_bOverControl) // Cursor is currently over control { CRect rect; GetClientRect(rect); if (!rect.PtInRect(point)) { m_bOverControl = FALSE; ReleaseCapture(); RedrawWindow(); return; } } else // Cursor has just moved over control { m_bOverControl = TRUE; RedrawWindow(); SetCapture(); } }void CHyperLink::PreSubclassWindow() { // We want to get mouse clicks via STN_CLICKED 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); } // Create the font LOGFONT lf; GetFont()->GetLogFont(&lf); lf.lfUnderline = (BYTE) TRUE; m_Font.CreateFontIndirect(&lf); SetFont(&m_Font); // Create the tooltip CRect rect; GetClientRect(rect); CStatic::PreSubclassWindow(); }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]; RegQueryValue(hkey, NULL, data, &datasize); lstrcpy(retdata,data); RegCloseKey(hkey); } return retval; }HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd) { TCHAR key[MAX_PATH + MAX_PATH]; // First try ShellExecute() HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd); // If it failed, get the .htm regkey and lookup the program 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);#ifdef _UNICODE USES_CONVERSION; result = (HINSTANCE) WinExec(T2A(key),showcmd); #else result = (HINSTANCE) WinExec(key,showcmd); #endif } } } return result; }void CHyperLink::InitHyperLink(CString strLink, HCURSOR hCursor) { m_strURL = strLink; m_hLinkCursor = hCursor; }
考,上边的是CPP, 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 class CHyperLink : public CStatic { // Construction/destruction public: CHyperLink(); virtual ~CHyperLink();// Attributes public:// Operations public: void InitHyperLink(CString strLink,HCURSOR hCursor);// Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CHyperLink) public: protected: virtual void PreSubclassWindow(); //}}AFX_VIRTUAL// Implementation protected: HINSTANCE GotoURL(LPCTSTR url, int showcmd); LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata);// Protected attributes protected: HCURSOR m_hLinkCursor; BOOL m_bOverControl; // cursor over control? CString m_strURL; // hyperlink URL CFont m_Font; // Underline font if necessary // Generated message map functions protected: //{{AFX_MSG(CHyperLink) afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor); afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message); afx_msg void OnMouseMove(UINT nFlags, CPoint point); //}}AFX_MSG afx_msg void OnClicked(); DECLARE_MESSAGE_MAP() };///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_)
#include "stdafx.h"
#include "HyperLink.h"
#include <afxconv.h> // For LPTSTR -> LPSTR macros
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#define TOOLTIP_ID 1/////////////////////////////////////////////////////////////////////////////
// CHyperLinkCHyperLink::CHyperLink()
{
m_hLinkCursor = NULL; // No cursor as yet
m_bOverControl = FALSE; // Cursor not yet over control
m_strURL.Empty();
}CHyperLink::~CHyperLink()
{
m_Font.DeleteObject();
}BEGIN_MESSAGE_MAP(CHyperLink, CStatic)
//{{AFX_MSG_MAP(CHyperLink)
ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETCURSOR() ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CHyperLink message handlersvoid CHyperLink::OnClicked()
{
m_bOverControl = FALSE;
GotoURL(m_strURL, SW_SHOW);
}HBRUSH CHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
ASSERT(nCtlColor == CTLCOLOR_STATIC);
UNUSED(nCtlColor); if (m_bOverControl)
pDC->SetTextColor(0xFF0080);
else
pDC->SetTextColor(0xFF0000); // transparent text.
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
BOOL CHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
{
if (m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
}void CHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
CStatic::OnMouseMove(nFlags, point); if (m_bOverControl) // Cursor is currently over control
{
CRect rect;
GetClientRect(rect); if (!rect.PtInRect(point))
{
m_bOverControl = FALSE;
ReleaseCapture();
RedrawWindow();
return;
}
}
else // Cursor has just moved over control
{
m_bOverControl = TRUE;
RedrawWindow();
SetCapture();
}
}void CHyperLink::PreSubclassWindow()
{
// We want to get mouse clicks via STN_CLICKED
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);
} // Create the font
LOGFONT lf;
GetFont()->GetLogFont(&lf);
lf.lfUnderline = (BYTE) TRUE;
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
// Create the tooltip
CRect rect;
GetClientRect(rect); CStatic::PreSubclassWindow();
}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];
RegQueryValue(hkey, NULL, data, &datasize);
lstrcpy(retdata,data);
RegCloseKey(hkey);
} return retval;
}HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
{
TCHAR key[MAX_PATH + MAX_PATH]; // First try ShellExecute()
HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd); // If it failed, get the .htm regkey and lookup the program
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);#ifdef _UNICODE
USES_CONVERSION;
result = (HINSTANCE) WinExec(T2A(key),showcmd);
#else
result = (HINSTANCE) WinExec(key,showcmd);
#endif
}
}
} return result;
}void CHyperLink::InitHyperLink(CString strLink, HCURSOR hCursor)
{
m_strURL = strLink;
m_hLinkCursor = hCursor;
}
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
class CHyperLink : public CStatic
{
// Construction/destruction
public:
CHyperLink();
virtual ~CHyperLink();// Attributes
public:// Operations
public:
void InitHyperLink(CString strLink,HCURSOR hCursor);// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHyperLink)
public:
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL// Implementation
protected:
HINSTANCE GotoURL(LPCTSTR url, int showcmd);
LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata);// Protected attributes
protected:
HCURSOR m_hLinkCursor;
BOOL m_bOverControl; // cursor over control?
CString m_strURL; // hyperlink URL
CFont m_Font; // Underline font if necessary // Generated message map functions
protected:
//{{AFX_MSG(CHyperLink)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message); afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG
afx_msg void OnClicked();
DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HYPERLINK_H__D1625061_574B_11D1_ABBA_00A0243D1382__INCLUDED_)
我这里还有一个问题?请大哥一并给解答了吧!好不好?
http://www.csdn.net/expert/topic/136/136562.shtm
那一个分更多!
m_url.InitHyperLink(_T("http://www.csdn.net"),AfxGetApp()->LoadCursor(IDC_CURSOR_HAND));
m_url是对话框上CStatic控件,用ClassWizard做成CHyperLink m_url
你的名字好酷