图一、实现图形超链接功能的程序界面图 |
一、 实现方法
著名的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()等知识,甚至还包括注册表的操作等内容,应该说虽然程序比较简单,但包含的内容还是比较丰富的。最后,运行此程序,将在对话框上显示"天极网"的首页链接,在图像上点鼠标左键后将自动进入天极网首页,效果很理想。