




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、用VC實(shí)現(xiàn)超鏈接技術(shù) 在網(wǎng)絡(luò)日益普及的今天,我們使用形形色色的軟件過程中,總能發(fā)現(xiàn)在這些軟件中都增加了一種實(shí)用功能超鏈接。使用超鏈接我們可以方便的在應(yīng)用程序中提供網(wǎng)址與電子郵件地址,方便用戶快捷升級,登錄網(wǎng)址瀏覽最新信息,發(fā)送郵件提供合理建議等等。這里所說的超鏈接是指用給定的地址從應(yīng)用程序中啟動瀏覽器或電子郵件軟件的功能,本文將在對話框中插入超鏈接,闡述超鏈接的實(shí)現(xiàn)方法。實(shí)現(xiàn)超鏈接,有些程序員們要說這簡單,只要使用API函數(shù)ShellExecute調(diào)用瀏覽器軟件或相應(yīng)的電子郵件軟件就可以實(shí)現(xiàn)。但這并不是一個好的實(shí)現(xiàn)方法,首先,每一個操作系統(tǒng)的瀏覽器不盡相同,簡單的調(diào)用方法不能很好的解決這一問題
2、;其次,不能實(shí)現(xiàn)良好的界面。一、實(shí)現(xiàn)良好的界面一個界面良好的超鏈接,在對話框中實(shí)現(xiàn)時可以實(shí)現(xiàn)顏色的變換,即正常時為一種顏色,光標(biāo)移到超鏈接控件上時顯示另一種顏色,訪問過的超鏈接又顯示一種顏色。響應(yīng)WM_CTLCOLOR_REFLECT消息,修改響應(yīng)函數(shù)如下可以實(shí)現(xiàn)顏色的變換。HBRUSH CHLink:CtlColor(CDC* pDC, UINT nCtlColor)/ TODO: Change any attributes of the DC hereif(m_bPointing)/m_bPointing為BOOL變量,光標(biāo)指向超鏈接控件為TRUE。pDC->SetTextColor
3、(m_cPointingColor);/m_cPointingColor為COLORREF變量,光標(biāo)指向超鏈接控件時的控件的顏色。elseif(m_bVisited)/m_bVisited為BOOL變量,超鏈接被訪問過為TRUE。pDC->SetTextColor(m_cVisitedColor);/m_cVisitedColor為COLORREF變量,超鏈接被訪問后的控件的顏色。 elseif(m_bNormal)/m_bNormal為BOOL變量,超鏈接正常狀態(tài)為TRUE。pDC->SetTextColor(m_cNormalColor);/m_cNormalColor為COL
4、ORREF變量,超鏈接正常狀態(tài)的顏色。 / TODO: Return a different brush if the default is not desiredpDC->SetBkMode(TRANSPARENT);return (HBRUSH)GetStockObject(NULL_BRUSH);/ HLink.h : header file/ CHLink windowclass CHLink : public CStatic/ Constructionpublic:void SetUnderline(BOOL Underline);void SetTipTitle(CStrin
5、g tipStr);void SetWindowTitle(CString tStr);LONG GetRegKey(HKEY key, LPCTSTR subkey, LPTSTR retdata);HINSTANCE GotoURL(LPCTSTR url, int showcmd);void SetLinkCur();void SetPointingColor(COLORREF PointingColor);void SetVisitedColor(COLORREF VisitedColor);void SetNormalColor(COLORREF NormalColor);void
6、SetURL(CString strURL);CHLink();private:void AutoSizeWindow();CString m_strURL;COLORREF m_cPointingColor;COLORREF m_cVisitedColor;COLORREF m_cNormalColor;BOOL m_bVisited;BOOL m_bPointing;BOOL m_bNormal;BOOL m_bUnderLine;BOOL m_bAdjustFit;BOOL m_Title;BOOL m_tip;CFont m_Font;CToolTipCtrl m_ToolTip;HC
7、URSOR m_cursor;/ Attributespublic:/ Operationspublic:/ Overrides/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CHLink)public:virtual BOOL PreTranslateMessage(MSG* pMsg);protected:virtual void PreSubclassWindow();/AFX_VIRTUAL/ Implementationpublic:virtual CHLink();/ Generated message
8、map functionsprotected:/AFX_MSG(CHLink)afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);/AFX_MSGDECLARE_MESSAGE_MAP();/ HLink.c
9、pp : implementation file/#include "stdafx.h"#include "llll.h"#include "HLink.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CHLinkCHLink:CHLink()m_cNormalColor=RGB(0,0,255);m_cPointingColor=RGB(0,125,155);m_cVisitedColor=RGB(8
10、5,26,139);m_bNormal=TRUE;m_bVisited=FALSE;m_bPointing=FALSE;m_Title=FALSE;m_tip=FALSE;m_bUnderLine=TRUE;m_bAdjustFit=TRUE;CHLink:CHLink()m_Font.DeleteObject();BEGIN_MESSAGE_MAP(CHLink, CStatic)/AFX_MSG_MAP(CHLink)ON_WM_MOUSEMOVE()ON_WM_CTLCOLOR_REFLECT()ON_WM_LBUTTONDOWN()ON_WM_SETCURSOR()/AFX_MSG_M
11、APEND_MESSAGE_MAP()/ CHLink message handlersvoid CHLink:SetURL(CString strURL)m_strURL=strURL;if(!m_Title)SetWindowText(m_strURL);AutoSizeWindow();if(!m_tip)m_ToolTip.UpdateTipText(strURL, this, 1);Invalidate();void CHLink:PreSubclassWindow()/ TODO: Add your specialized code here and/or call the bas
12、e classDWORD dwStyle = GetStyle();:SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);m_cursor=AfxGetApp()->LoadStandardCursor(IDC_ARROW);LOGFONT lf;GetFont()->GetLogFont(&lf);lf.lfUnderline = m_bUnderLine;m_Font.CreateFontIndirect(&lf);SetFont(&m_Font);CRect rect;GetClie
13、ntRect(rect);m_ToolTip.Create(this);m_ToolTip.AddTool(this, m_strURL, rect, 1);SetLinkCur();CStatic:PreSubclassWindow();HBRUSH CHLink:CtlColor(CDC* pDC, UINT nCtlColor)/ TODO: Change any attributes of the DC hereif(m_bPointing)pDC->SetTextColor(m_cPointingColor);elseif(m_bVisited)pDC->SetTextC
14、olor(m_cVisitedColor);elseif(m_bNormal)pDC->SetTextColor(m_cNormalColor);/ TODO: Return a different brush if the default is not desiredpDC->SetBkMode(TRANSPARENT);return (HBRUSH)GetStockObject(NULL_BRUSH);void CHLink:SetPointingColor(COLORREF PointingColor)m_cPointingColor=PointingColor;void C
15、HLink:SetVisitedColor(COLORREF VisitedColor)m_cVisitedColor=VisitedColor;void CHLink:SetNormalColor(COLORREF NormalColor)m_cNormalColor=NormalColor;void CHLink:OnMouseMove(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultCStatic:OnMouseMove(nFlags, point);CRect
16、 rect;GetClientRect(rect);if (!rect.PtInRect(point)m_bPointing = FALSE;ReleaseCapture();else / Cursor has just moved over controlm_bPointing = TRUE;SetLinkCur();SetCapture();Invalidate();void CHLink:AutoSizeWindow()if(m_bAdjustFit)CRect rect;GetWindowRect(rect);CWnd *pParent=GetParent();if(pParent)p
17、Parent->ScreenToClient(rect);CString strText;GetWindowText(strText);CDC* pDC=GetDC();CFont* oldFont=pDC->SelectObject(&m_Font);CSize strExtent=pDC->GetTextExtent(strText);pDC->SelectObject(oldFont);ReleaseDC(pDC);DWORD dwStyle = GetStyle();if (dwStyle & SS_CENTERIMAGE)rect.Deflat
18、eRect(0, (rect.Height() - strExtent.cy)/2);elserect.bottom = rect.top + strExtent.cy;if (dwStyle & SS_CENTER)rect.DeflateRect(rect.Width() - strExtent.cx)/2, 0);elseif (dwStyle & SS_RIGHT)rect.left = rect.right - strExtent.cx;elserect.right = rect.left + strExtent.cx;SetWindowPos(NULL, rect.
19、left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);void CHLink:OnLButtonDown(UINT nFlags, CPoint point)/ TODO: Add your message handler code here and/or call defaultint result = (int)GotoURL(m_strURL, SW_SHOW);m_bVisited = (result > HINSTANCE_ERROR);Invalidate();CStatic:OnLButtonDown(nFla
20、gs, point);BOOL CHLink:PreTranslateMessage(MSG* pMsg)/ TODO: Add your specialized code here and/or call the base classm_ToolTip.RelayEvent(pMsg);return CStatic:PreTranslateMessage(pMsg);void CHLink:SetLinkCur()m_cursor=AfxGetApp()->LoadCursor(IDC_CURSOR);BOOL CHLink:OnSetCursor(CWnd* pWnd, UINT n
21、HitTest, UINT message)/ TODO: Add your message handler code here and/or call default:SetCursor(m_cursor);return TRUE;/return CStatic:OnSetCursor(pWnd, nHitTest, message);HINSTANCE CHLink:GotoURL(LPCTSTR url, int showcmd)TCHAR keyMAX_PATH + MAX_PATH;HINSTANCE result = ShellExecute(NULL, _T("open
22、"), url, NULL,NULL, showcmd);if (UINT)result <= HINSTANCE_ERROR) if (GetRegKey(HKEY_CLASSES_ROOT, _T(".htm"), key) = ERROR_SUCCESS) lstrcat(key, _T("shellopencommand");if (GetRegKey(HKEY_CLASSES_ROOT,key,key) = ERROR_SUCCESS)TCHAR *pos;pos = _tcsstr(key, _T(""%1"");if (pos = NULL)pos = strstr(key, _T("%1");if (pos = NULL)pos = key+lstrlen(key)-1;else*pos = '0'else*pos = '0' / Remove the parameterlstrcat(pos, _T(" ");lstrcat(pos, url);result = (HINSTANCE) WinExec(key,showcmd);return result;
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國全紙多層密實(shí)紙箱行業(yè)投資前景及策略咨詢報告
- 2025年深圳市勞動合同范本參考文獻(xiàn)
- 2025年中國舒安靈己酮可可堿市場調(diào)查研究報告
- 2025企業(yè)簽訂采購合同的注意事項
- 購銷終止合同協(xié)議書范本
- 朝花夕題目及答案
- 常識判斷刑法題目及答案
- 2025拼多多電商平臺店鋪代運(yùn)營服務(wù)合同范本
- 雙方自愿合同協(xié)議書范本
- 巖板知識考試題及答案
- 專題17 語言要簡明+考場滿分作文攻略-【同步作文課】【知識精研】七年級語文下冊單元寫作深度指導(dǎo)(統(tǒng)編版2024)
- 保潔合同協(xié)議書模板下載
- 2025法語DELFA15級閱讀理解試卷及答案
- 2025年全球經(jīng)濟(jì)策略試題及答案
- 山東省濟(jì)南市商河縣2025屆九年級下學(xué)期中考二模語文試卷(含答案)
- 知識產(chǎn)權(quán)國際保護(hù)課件
- 2024年棗莊滕州市中小學(xué)招聘教師筆試真題
- 2025年海南省中考模擬語文試題(含答案)
- 描繪人間溫情-怎樣刻畫人物 課件-2023-2024學(xué)年高中美術(shù)人美版(2019)選擇性必修1 繪畫
- 職業(yè)技術(shù)學(xué)校中醫(yī)康復(fù)技術(shù)專業(yè)人才培養(yǎng)方案
- 遼寧省名校聯(lián)盟2025年高考模擬卷押題卷數(shù)學(xué)(三)
評論
0/150
提交評論