![]() 圖一、實現圖形超鏈接功能的程序界面圖 |
一、 實現方法
著名的CHyperLink類只能提供文字鏈接,不能用于圖形控件的超鏈接,于是本實例在其基礎上修改了一下,定義了一個CMapHyperLink類,該類現在只對圖形控件(picture control)生效。用戶可使用成員函數void SetURL(CString strURL)設置要訪問的互聯網地址,如SetURL("http://www.google.com");使用成員函數void SetTipText(CString strURL)設置超鏈接提示條(CToolTipCtrl)的文字內容,如果不在此設置,那就默認是您設置的URL地址,如SetTipText("歡迎訪問強大的google搜索");使用成員函數void SetLinkCursor(HCURSOR hCursor)設置鼠標在超鏈接狀態的圖標,默認是手型圖標。該類還提供了HCURSOR GetLinkCursor() const、void SetAutoSize(BOOL bAutoSize = TRUE)等成員函數,提供了一些相應的輔助功能,讀者朋友可以參考代碼部分。這里主要講述三個主要的問題:一是如何實現提示功能;二是在控件上如何改變鼠標的形狀,給用戶提供另外一種暗示-當前區域提供超鏈接功能;三是如何根據網頁或信箱地址啟動超鏈接功能。
Visual C++提供了CCtoolTipCtrl類用來實現提示功能。工具提示控制是一個小窗口,在其中顯示單行文字用以描述應用程序中的工具的用途。這里的工具所指的既可以是窗口(如工具欄上的按鈕),也可以是一個固定的區域。大家都知道利用APPWIZARD生成 的應用程序中工具欄帶有提示,當你將鼠標放在工具欄某一個按鈕上時,將顯示一個小提示框告訴你按鈕的功能,這種功能方便了軟件的使用者。但是在超鏈接區如何實現提示呢?首先聲明一個CtoolTipCtrl類的變量,調用Create()成員函數創建通用工具提示,并將它附在CtoolTipCtrl對象上,然后調用CtoolTipCtrl類的AddToo()成員函數注冊工具提示控制,從而為光標放在規定的窗口或區域內時顯示工具提示做準備。該函數的原形為:
BOOL AddTool( CWnd* pWnd,LPCTSTR lpszText, LPCRECT lpRectTool,UINT nIDTOOL ); |
其中參數pWnd為指向包含工具提示控制的窗口指針,參數lpszText為所要在工具提示中顯示的文字,參數lpRectTool為工具書提示所對應的窗口或規定區域,參數nIDTOOL為工具提示的標志號。
在調用CtoolTipCtrl類的Active()函數激活提示后,最后要作的工作是調用CtoolTipCtrl類的RelayEvent()函數將鼠標的WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_LBUTTONUP等消息傳遞給工具提示控制對象,以便控件進行提示處理。
下面的代碼實現了在應用程序的視圖區顯示對應點的坐標為例:
//在文件頭定義的全局變量 CToolTipCtrl m_ToolTip;//工具提示對象 char string[50];//用來存放提示文字 CRect rect;//用來存放工具提示所對應的窗口的尺寸 #define IDC_CONST 12345//定義的工具提示標志號,注意不要和系統沖突 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); } |
如果用戶想動態的改變提示字符串,可以調用CtoolTipCtrl類的成員函數UpdateTipText()來實現,該函數的原型為:
void UpdateTipText( LPCTSTR lpszText, CWnd* pWnd, UINT nIDTool = 0 ); |
該函數的參數的含義與成員函數AddTool()的參數的含義大同小異,這里不再贅述。
對于超鏈接來說,一般會在超鏈接區域改變鼠標的形狀,顯示手狀的鼠標,提示這是一個超鏈接區域。當然可以在程序中添加一個手狀的光標資源,然后使用LoadCursor()函數等加載,這種方法對廣大讀者朋友一定是耳熟能詳了,所以為了擴大讀者朋友的編程思路,這里介紹一種從Windows的winhlp32.exe文件中加載光標資源,代碼如下:
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); } } |
為了根據網頁或信箱地址實現超鏈接功能,需要用到一個WINDOWS API函數ShellExecute(),其原型為:
HINSTANCE ShellExecute( HWND hwnd, //窗口句柄 LPCTSTR lpOperation, //操作類型 LPCTSTR lpFile, //文件指針 LPCTSTR lpParameters, //文件可帶的參數 LPCTSTR lpDirectory, //缺省目錄 INT nShowCmd //顯示方式 ); |
ShellExecute()函數用于打開或執行一個文件,在調用此函數時只須指定要打開或執行的文件名,而不必管用什么程序去打開或執行文件,WINDOWS會自動根據要打開或執行的文件去判斷該如何執行文件或用什么程序去打開文件。函數中的參數lpOperation說明所要執行的操作,該值可以設置為"Open"、"Print"、"Explore",分別用來進行"打開"、"打印"、"瀏覽"操作。下面給出了ShellExecute()函數的一些使用方法:
(1)打開一個應用程序:
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW ); 或 ShellExecute(this->m_hWnd,"open","notepad.exe", "c:\MyLog.log","",SW_SHOW ); |
(2)打開一個同系統程序相關連的文檔
ShellExecute(this->m_hWnd,"open", "c:\abc.txt","","",SW_SHOW ); |
(3)打開一個網頁
ShellExecute(this->m_hWnd,"open", " http://www.google.com","","",/ SW_SHOW ); |
(4)激活相關程序,發送EMAIL
ShellExecute(this->m_hWnd,"open","mailto:nishinapp@yahoo.com","","", W_SHOW ); |
(5)用系統打印機打印文檔
ShellExecute(this->m_hWnd,"print", "c:\abc.txt","","", SW_HIDE); |
(6)用系統查找功能來查找指定文件
ShellExecute(m_hWnd,"find","d:\nish", NULL,NULL,SW_SHOW); |
(7)啟動一個程序,直到它運行結束
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還提供了一個與ShellExecuteEx()函數相類似的函數WinExec(),它相對于ShellExecuteEx()來說更簡單易用,只是功能沒有它強大而已,具體使用方法讀者朋友自行參閱MSDN。
二、編程步驟
l、啟動Visual C++6.0,生成一個基于對話框的應用程序,將該程序命名為"Test";
2、在對話框上放置一個靜態控件,并顯示一幅圖象;
3、使用Class Wizard為應用程序添加一個CMapHyperLink類,其基類為CStatic;
4、在對話框中添加一個CmapHyperLink類對象m_MapHyperLink1;
5、添加代碼,編譯運行程序。
三、程序代碼
///////////////////////////////////////////////////////////////////////// //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 //設置圖形的超鏈接 m_MapHyperLink1.SetURL("www.yesky.com"); m_MapHyperLink1.SetTipText("歡迎訪問天極網"); // TODO: Add extra initialization here return TRUE; // return TRUE unless you set the focus to a control } |
四、小結
本實例通過介紹如何實現超鏈接功能,介紹了工具提示、動態地從可執行文件中加載圖標、使用外殼函數ShellExecute()等知識,甚至還包括注冊表的操作等內容,應該說雖然程序比較簡單,但包含的內容還是比較豐富的。最后,運行此程序,將在對話框上顯示"天極網"的首頁鏈接,在圖像上點鼠標左鍵后將自動進入天極網首頁,效果很理想。