目前很多windows軟件的版權(quán)對(duì)話(huà)框中都設(shè)有超級(jí)鏈接,這些鏈接或提供公司網(wǎng)址,或提供電子郵件信箱,使操作者能夠非常方便地與公司和作者聯(lián)系,同時(shí)也為公司作了很好的宣傳。一般情況下,界面上某行文字下面有一行藍(lán)色的橫線(xiàn),標(biāo)志該字符串提供超鏈接功能,當(dāng)用戶(hù)將鼠標(biāo)移動(dòng)到文字上時(shí),鼠標(biāo)變成手狀,如果用戶(hù)此時(shí)單擊鼠標(biāo),程序?qū)?dòng)瀏覽器打開(kāi)某個(gè)網(wǎng)頁(yè)或啟動(dòng)OutLook讓用戶(hù)給指定的郵箱發(fā)送電子郵件。如果能在自己寫(xiě)的軟件中實(shí)現(xiàn)這個(gè)功能,定會(huì)使程序大增光彩。本實(shí)例通過(guò)定義一個(gè)CmapHyperLink類(lèi)實(shí)現(xiàn)了圖像的超鏈接功能
一、 實(shí)現(xiàn)方法
著名的CHyperLink類(lèi)只能提供文字鏈接,不能用于圖形控件的超鏈接,于是本實(shí)例在其基礎(chǔ)上修改了一下,定義了一個(gè)CMapHyperLink類(lèi),該類(lèi)現(xiàn)在只對(duì)圖形控件(picture control)生效。用戶(hù)可使用成員函數(shù)void SetURL(CString strURL)設(shè)置要訪(fǎng)問(wèn)的互聯(lián)網(wǎng)地址,如SetURL("http://www.google.com");使用成員函數(shù)void SetTipText(CString strURL)設(shè)置超鏈接提示條(CToolTipCtrl)的文字內(nèi)容,如果不在此設(shè)置,那就默認(rèn)是您設(shè)置的URL地址,如SetTipText("歡迎訪(fǎng)問(wèn)強(qiáng)大的google搜索");使用成員函數(shù)void SetLinkCursor(HCURSOR hCursor)設(shè)置鼠標(biāo)在超鏈接狀態(tài)的圖標(biāo),默認(rèn)是手型圖標(biāo)。該類(lèi)還提供了HCURSOR GetLinkCursor() const、void SetAutoSize(BOOL bAutoSize = TRUE)等成員函數(shù),提供了一些相應(yīng)的輔助功能,讀者朋友可以參考代碼部分。這里主要講述三個(gè)主要的問(wèn)題:一是如何實(shí)現(xiàn)提示功能;二是在控件上如何改變鼠標(biāo)的形狀,給用戶(hù)提供另外一種暗示-當(dāng)前區(qū)域提供超鏈接功能;三是如何根據(jù)網(wǎng)頁(yè)或信箱地址啟動(dòng)超鏈接功能。
Visual C++提供了CCtoolTipCtrl類(lèi)用來(lái)實(shí)現(xiàn)提示功能。工具提示控制是一個(gè)小窗口,在其中顯示單行文字用以描述應(yīng)用程序中的工具的用途。這里的工具所指的既可以是窗口(如工具欄上的按鈕),也可以是一個(gè)固定的區(qū)域。大家都知道利用APPWIZARD生成 的應(yīng)用程序中工具欄帶有提示,當(dāng)你將鼠標(biāo)放在工具欄某一個(gè)按鈕上時(shí),將顯示一個(gè)小提示框告訴你按鈕的功能,這種功能方便了軟件的使用者。但是在超鏈接區(qū)如何實(shí)現(xiàn)提示呢?首先聲明一個(gè)CtoolTipCtrl類(lèi)的變量,調(diào)用Create()成員函數(shù)創(chuàng)建通用工具提示,并將它附在CtoolTipCtrl對(duì)象上,然后調(diào)用CtoolTipCtrl類(lèi)的AddToo()成員函數(shù)注冊(cè)工具提示控制,從而為光標(biāo)放在規(guī)定的窗口或區(qū)域內(nèi)時(shí)顯示工具提示做準(zhǔn)備。該函數(shù)的原形為:
BOOL AddTool( CWnd* pWnd,LPCTSTR lpszText, LPCRECT lpRectTool,UINT nIDTOOL );
其中參數(shù)pWnd為指向包含工具提示控制的窗口指針,參數(shù)lpszText為所要在工具提示中顯示的文字,參數(shù)lpRectTool為工具書(shū)提示所對(duì)應(yīng)的窗口或規(guī)定區(qū)域,參數(shù)nIDTOOL為工具提示的標(biāo)志號(hào)。
在調(diào)用CtoolTipCtrl類(lèi)的Active()函數(shù)激活提示后,最后要作的工作是調(diào)用CtoolTipCtrl類(lèi)的RelayEvent()函數(shù)將鼠標(biāo)的WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_LBUTTONUP等消息傳遞給工具提示控制對(duì)象,以便控件進(jìn)行提示處理。
下面的代碼實(shí)現(xiàn)了在應(yīng)用程序的視圖區(qū)顯示對(duì)應(yīng)點(diǎn)的坐標(biāo)為例:
//在文件頭定義的全局變量
CToolTipCtrl m_ToolTip;//工具提示對(duì)象
char string[50];//用來(lái)存放提示文字
CRect rect;//用來(lái)存放工具提示所對(duì)應(yīng)的窗口的尺寸
#define IDC_CONST 12345//定義的工具提示標(biāo)志號(hào),注意不要和系統(tǒng)沖突
void CTestView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
wsprintf(string,"%d,%d",point.x,point.y);
GetClientRect(&recty);
m_ToolTip.AddTool(this,string,&rect,IDC_CONST);
m_ToolTip.Activate(TRUE);
CView::OnMouseMove(nFlags, point);
}
LRESULT CTestView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_MOUSEMOVE:
{MSG msg;
msg.hwnd=m_hWnd;
msg.message=message;
msg.wParam=wParam;
msg.lParam=lParam;
m_ToolTip.RelayEvent(&msg);
}
}
return CView::WindowProc(message, wParam, lParam);
}
如果用戶(hù)想動(dòng)態(tài)的改變提示字符串,可以調(diào)用CtoolTipCtrl類(lèi)的成員函數(shù)UpdateTipText()來(lái)實(shí)現(xiàn),該函數(shù)的原型為:
void UpdateTipText( LPCTSTR lpszText, CWnd* pWnd, UINT nIDTool = 0 );
該函數(shù)的參數(shù)的含義與成員函數(shù)AddTool()的參數(shù)的含義大同小異,這里不再贅述。
對(duì)于超鏈接來(lái)說(shuō),一般會(huì)在超鏈接區(qū)域改變鼠標(biāo)的形狀,顯示手狀的鼠標(biāo),提示這是一個(gè)超鏈接區(qū)域。當(dāng)然可以在程序中添加一個(gè)手狀的光標(biāo)資源,然后使用LoadCursor()函數(shù)等加載,這種方法對(duì)廣大讀者朋友一定是耳熟能詳了,所以為了擴(kuò)大讀者朋友的編程思路,這里介紹一種從Windows的winhlp32.exe文件中加載光標(biāo)資源,代碼如下:
void CMapHyperLink::SetDefaultCursor()
{
if (m_hLinkCursor == NULL) // No cursor handle - load our own
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
為了根據(jù)網(wǎng)頁(yè)或信箱地址實(shí)現(xiàn)超鏈接功能,需要用到一個(gè)WINDOWS API函數(shù)ShellExecute(),其原型為:
HINSTANCE ShellExecute(
HWND hwnd, //窗口句柄
LPCTSTR lpOperation, //操作類(lèi)型
LPCTSTR lpFile, //文件指針
LPCTSTR lpParameters, //文件可帶的參數(shù)
LPCTSTR lpDirectory, //缺省目錄
INT nShowCmd //顯示方式
);
ShellExecute()函數(shù)用于打開(kāi)或執(zhí)行一個(gè)文件,在調(diào)用此函數(shù)時(shí)只須指定要打開(kāi)或執(zhí)行的文件名,而不必管用什么程序去打開(kāi)或執(zhí)行文件,WINDOWS會(huì)自動(dòng)根據(jù)要打開(kāi)或執(zhí)行的文件去判斷該如何執(zhí)行文件或用什么程序去打開(kāi)文件。函數(shù)中的參數(shù)lpOperation說(shuō)明所要執(zhí)行的操作,該值可以設(shè)置為"Open"、"Print"、"Explore",分別用來(lái)進(jìn)行"打開(kāi)"、"打印"、"瀏覽"操作。下面給出了ShellExecute()函數(shù)的一些使用方法:
(1)打開(kāi)一個(gè)應(yīng)用程序:
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );
或
ShellExecute(this->m_hWnd,"open","notepad.exe", "c:\MyLog.log","",SW_SHOW );
(2)打開(kāi)一個(gè)同系統(tǒng)程序相關(guān)連的文檔
ShellExecute(this->m_hWnd,"open", "c:\abc.txt","","",SW_SHOW );
(3)打開(kāi)一個(gè)網(wǎng)頁(yè)
ShellExecute(this->m_hWnd,"open", " http://www.google.com","","",/ SW_SHOW );
(4)激活相關(guān)程序,發(fā)送EMAIL
ShellExecute(this->m_hWnd,"open","mailto:nishinapp@yahoo.com","","", W_SHOW );
(5)用系統(tǒng)打印機(jī)打印文檔
ShellExecute(this->m_hWnd,"print", "c:\abc.txt","","", SW_HIDE);
(6)用系統(tǒng)查找功能來(lái)查找指定文件
ShellExecute(m_hWnd,"find","d:\nish", NULL,NULL,SW_SHOW);
(7)啟動(dòng)一個(gè)程序,直到它運(yùn)行結(jié)束
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c:\MyProgram.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
或:
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
if(CreateProcess("c:\winnt\notepad.exe", NULL,
NULL,NULL,FALSE,0,NULL,
NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
else
{
MessageBox("The process could not be started...");
}
(8)顯示文件或文件夾的屬性
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c:"; //can be a file as well
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
Windows還提供了一個(gè)與ShellExecuteEx()函數(shù)相類(lèi)似的函數(shù)WinExec(),它相對(duì)于ShellExecuteEx()來(lái)說(shuō)更簡(jiǎn)單易用,只是功能沒(méi)有它強(qiáng)大而已,具體使用方法讀者朋友自行參閱MSDN。
二、編程步驟
l、啟動(dòng)Visual C++6.0,生成一個(gè)基于對(duì)話(huà)框的應(yīng)用程序,將該程序命名為"Test";
2、在對(duì)話(huà)框上放置一個(gè)靜態(tài)控件,并顯示一幅圖象;
3、使用Class Wizard為應(yīng)用程序添加一個(gè)CMapHyperLink類(lèi),其基類(lèi)為CStatic;
4、在對(duì)話(huà)框中添加一個(gè)CmapHyperLink類(lèi)對(duì)象m_MapHyperLink1;
5、添加代碼,編譯運(yùn)行程序。
三、程序代碼
/////////////////////////////////////////////////////////////////////////
//MapHyperLink.h , MapHyperLink.cpp
#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
// CHyperLink window
class CMapHyperLink : public CStatic
{
// Construction/destruction
public:
CMapHyperLink();
virtual ~CMapHyperLink();
public:
void SetURL(CString strURL);
CString GetURL() const;
void SetTipText(CString strURL);
CString GetTipText() const;
void SetVisited(BOOL bVisited = TRUE);
BOOL GetVisited() const;
void SetLinkCursor(HCURSOR hCursor);
HCURSOR GetLinkCursor() const;
void SetAutoSize(BOOL bAutoSize = TRUE);
BOOL GetAutoSize() const;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHyperLink)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
// Implementation
protected:
HINSTANCE GotoURL(LPCTSTR url, int showcmd);
void ReportError(int nError);
LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata);
void PositionWindow();
void SetDefaultCursor();
// Protected attributes
protected:
BOOL m_bOverControl; // cursor over control?
BOOL m_bVisited; // Has it been visited?
BOOL m_bAdjustToFit; // Adjust window size to fit text?
CString m_strURL; // hyperlink URL
CString m_strTipText; // TipTool control'' text
HCURSOR m_hLinkCursor; // Cursor for hyperlink
CToolTipCtrl m_ToolTip; // The tooltip
protected: // Generated message map functions
//{{AFX_MSG(CHyperLink)
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()
};
#endif
///////////////////////////////////////////////////////////// MapHyperLink.cpp
#include "stdafx.h"
#include "MapHyperLink.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TOOLTIP_ID 1
CMapHyperLink::CMapHyperLink()
{
m_hLinkCursor = NULL; // No cursor as yet
m_bOverControl = FALSE; // Cursor not yet over control
m_bVisited = FALSE; // Hasn''t been visited yet.
m_bAdjustToFit = TRUE; // Resize the window to fit the text?
m_strURL.Empty();
m_strTipText.Empty();
}
CMapHyperLink::~CMapHyperLink()
{}
BEGIN_MESSAGE_MAP(CMapHyperLink, CStatic)
//{{AFX_MSG_MAP(CMapHyperLink)
ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
// CMapHyperLink message handlers
BOOL CMapHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
void CMapHyperLink::OnClicked()
{
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(); // Repaint to show visited colour
}
void CMapHyperLink::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();
}
}
BOOL CMapHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
{
if (m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
}
void CMapHyperLink::PreSubclassWindow()
{
// We want to get mouse clicks via STN_CLICKED
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
SetDefaultCursor(); // Try and load up a "hand" cursor
// Create the tooltip
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
if (m_strTipText.IsEmpty())
{
m_strTipText = m_strURL;
}
m_ToolTip.AddTool(this, m_strTipText, rect, TOOLTIP_ID);
CStatic::PreSubclassWindow();
}
////////////////////////////////////////////// CMapHyperLink operations
void CMapHyperLink::SetURL(CString strURL)
{
m_strURL = strURL;
if (::IsWindow(GetSafeHwnd())) {
PositionWindow();
if (m_strTipText.IsEmpty())
{
m_strTipText = strURL;
}
m_ToolTip.UpdateTipText(m_strTipText, this, TOOLTIP_ID);
}
}
CString CMapHyperLink::GetURL() const
{
return m_strURL;
}
void CMapHyperLink::SetTipText(CString strTipText)
{
m_strTipText = strTipText;
if (::IsWindow(GetSafeHwnd())) {
PositionWindow();
m_ToolTip.UpdateTipText(m_strTipText, this, TOOLTIP_ID);
}
}
CString CMapHyperLink::GetTipText() const
{
return m_strTipText;
}
void CMapHyperLink::SetVisited(BOOL bVisited /* = TRUE */)
{
m_bVisited = bVisited;
if (::IsWindow(GetSafeHwnd()))
Invalidate();
}
BOOL CMapHyperLink::GetVisited() const
{
return m_bVisited;
}
void CMapHyperLink::SetLinkCursor(HCURSOR hCursor)
{
m_hLinkCursor = hCursor;
if (m_hLinkCursor == NULL)
SetDefaultCursor();
}
HCURSOR CMapHyperLink::GetLinkCursor() const
{
return m_hLinkCursor;
}
void CMapHyperLink::SetAutoSize(BOOL bAutoSize /* = TRUE */)
{
m_bAdjustToFit = bAutoSize;
if (::IsWindow(GetSafeHwnd()))
PositionWindow();
}
BOOL CMapHyperLink::GetAutoSize() const
{
return m_bAdjustToFit;
}
// Move and resize the window so that the window is the same size
void CMapHyperLink::PositionWindow()
{
if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit)
return;
// Get the current window position
CRect rect;
GetWindowRect(rect);
CWnd* pParent = GetParent();
if (pParent)
pParent->ScreenToClient(rect);
CRect rectMap;
GetClientRect(rectMap);
// 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)
rect.DeflateRect(0, (rect.Height() - rectMap.Height())/2);
else
rect.bottom = rect.top + rectMap.Height();
if (dwStyle & SS_CENTER)
rect.DeflateRect((rect.Width() - rectMap.Width())/2, 0);
else if (dwStyle & SS_RIGHT)
rect.left = rect.right - rectMap.Width();
else // SS_LEFT = 0, so we can''t test for it explicitly
rect.right = rect.left + rectMap.Width();
// Move the window
SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);
}
/////////////////////////////////////////////////// CMapHyperLink implementation
void CMapHyperLink::SetDefaultCursor()
{
if (m_hLinkCursor == NULL) // No cursor handle - load our own
{
// Get the windows directory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH), MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir += _T("\winhlp32.exe");
// This retrieves cursor #106 from winhlp32.exe, which is a hand pointer
HMODULE hModule = LoadLibrary(strWndDir);
if (hModule) {
HCURSOR hHandCursor = ::LoadCursor(hModule, MAKEINTRESOURCE(106));
if (hHandCursor)
m_hLinkCursor = CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
LONG CMapHyperLink::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;
}
void CMapHyperLink::ReportError(int nError)
{
CString str;
switch (nError) {
case 0:
str = "The operating system is out\nof memory or resources."; break;
case SE_ERR_PNF:
str = "The specified path was not found."; break;
case SE_ERR_FNF:
str = "The specified file was not found."; break;
case ERROR_BAD_FORMAT:
str = "The .EXE file is invalid\n(non-Win32 .EXE or error in .EXE image)."; break;
case SE_ERR_ACCESSDENIED:
str = "The operating system denied\naccess to the specified file."; break;
case SE_ERR_ASSOCINCOMPLETE:
str = "The filename association is\nincomplete or invalid."; break;
case SE_ERR_DDEBUSY:
str = "The DDE transaction could not\nbe completed because other DDE transactions\nwere being processed."; break;
case SE_ERR_DDEFAIL:
str = "The DDE transaction failed."; break;
case SE_ERR_DDETIMEOUT:
str = "The DDE transaction could not\nbe completed because the request timed out."; break;
case SE_ERR_DLLNOTFOUND:
str = "The specified dynamic-link library was not found."; break;
case SE_ERR_NOASSOC:
str = "There is no application associated\nwith the given filename extension."; break;
case SE_ERR_OOM:
str = "There was not enough memory to complete the operation."; break;
case SE_ERR_SHARE:
str = "A sharing violation occurred. ";
default:
str.Format("Unknown Error (%d) occurred.", nError); break;
}
str = "Unable to open hyperlink:\n\n" + str;
AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
}
HINSTANCE CMapHyperLink::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 = strstr(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);
result = (HINSTANCE) WinExec(key,showcmd);
}
}
}
return result;
}
/////////////////////////////////////////////////////////////////////////////////////
BOOL CTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application''s main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//設(shè)置圖形的超鏈接
m_MapHyperLink1.SetURL("www.yesky.com");
m_MapHyperLink1.SetTipText("歡迎訪(fǎng)問(wèn)天極網(wǎng)");
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
四、小結(jié)
本實(shí)例通過(guò)介紹如何實(shí)現(xiàn)超鏈接功能,介紹了工具提示、動(dòng)態(tài)地從可執(zhí)行文件中加載圖標(biāo)、使用外殼函數(shù)ShellExecute()等知識(shí),甚至還包括注冊(cè)表的操作等內(nèi)容,應(yīng)該說(shuō)雖然程序比較簡(jiǎn)單,但包含的內(nèi)容還是比較豐富的。最后,運(yùn)行此程序,將在對(duì)話(huà)框上顯示"天極網(wǎng)"的首頁(yè)鏈接,在圖像上點(diǎn)鼠標(biāo)左鍵后將自動(dòng)進(jìn)入天極網(wǎng)首頁(yè),效果很理想。