如何在对话框中加入超链接
2012-10-19 09:45
399 查看
CHyperLink继承CStatic类
classCHyperLink:publicCStatic
{
DECLARE_DYNAMIC(CHyperLink)
public:
CHyperLink();
virtual~CHyperLink();
//protected:
// DECLARE_MESSAGE_MAP()
public:
voidSetURL(CStringstrURL);//=========================================
CStringGetURL()const;
voidSetColours(COLORREFcrLinkColour,COLORREFcrVisitedColour,
COLORREFcrHoverColour=-1);
COLORREFGetLinkColour()const;
COLORREFGetVisitedColour()const;
COLORREFGetHoverColour()const;
voidSetVisited(BOOLbVisited=TRUE);
BOOLGetVisited()const;
voidSetLinkCursor(HCURSORhCursor);
HCURSORGetLinkCursor()const;
voidSetUnderline(BOOLbUnderline=TRUE);//***********************************
BOOLGetUnderline()const;
voidSetAutoSize(BOOLbAutoSize=TRUE);
BOOLGetAutoSize()const;
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CHyperLink)
public:
virtualBOOLPreTranslateMessage(MSG*pMsg);
protected:
virtualvoidPreSubclassWindow();
//}}AFX_VIRTUAL
//Implementation
protected:
HINSTANCEGotoURL(LPCTSTRurl,intshowcmd);
voidReportError(intnError);
LONGGetRegKey(HKEYkey,LPCTSTRsubkey,LPTSTRretdata);
voidPositionWindow();
voidSetDefaultCursor();
//Protectedattributes
protected:
COLORREFm_crLinkColour,m_crVisitedColour;//Hyperlinkcolours
COLORREFm_crHoverColour;//Hovercolour
BOOLm_bOverControl;//cursorovercontrol?
BOOLm_bVisited;//Hasitbeenvisited?
BOOLm_bUnderline;//underlinehyperlink?
BOOLm_bAdjustToFit;//Adjustwindowsizetofittext?
CStringm_strURL;//hyperlinkURL
CFontm_Font;//Underlinefontifnecessary
HCURSORm_hLinkCursor;//Cursorforhyperlink
CToolTipCtrlm_ToolTip;//Thetooltip
//Generatedmessagemapfunctions
protected:
//{{AFX_MSG(CHyperLink)
afx_msgHBRUSHCtlColor(CDC*pDC,UINTnCtlColor);
afx_msgBOOLOnSetCursor(CWnd*pWnd,UINTnHitTest,UINTmessage);
afx_msgvoidOnMouseMove(UINTnFlags,CPointpoint);
//}}AFX_MSG
afx_msgvoidOnClicked();
DECLARE_MESSAGE_MAP()
};
HyperLink.cpp文件:
#include"stdafx.h"
#include"HyperLink.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
#defineTOOLTIP_ID1
//CHyperLink
IMPLEMENT_DYNAMIC(CHyperLink,CStatic)
CHyperLink::CHyperLink()
{
m_hLinkCursor=NULL;//Nocursorasyet
m_crLinkColour=RGB(0,0,238);//Blue
m_crVisitedColour=RGB(85,26,139);//Purple
m_crHoverColour=::GetSysColor(COLOR_HIGHLIGHT);
m_bOverControl=FALSE;//Cursornotyetovercontrol
m_bVisited=FALSE;//Hasn'tbeenvisitedyet.
m_bUnderline=TRUE;//Underlinethelink?
m_bAdjustToFit=TRUE;//Resizethewindowtofitthetext?
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()
/////////////////////////////////////////////////////////////////////////////
//CHyperLinkmessagehandlers
BOOLCHyperLink::PreTranslateMessage(MSG*pMsg)
{
m_ToolTip.RelayEvent(pMsg);
returnCStatic::PreTranslateMessage(pMsg);
}
voidCHyperLink::OnClicked()
{
intresult=(int)GotoURL(m_strURL,SW_SHOW);
m_bVisited=(result>HINSTANCE_ERROR);
if(!m_bVisited){
MessageBeep(MB_ICONEXCLAMATION);//Unabletofollowlink
ReportError(result);
}else
SetVisited();//Repainttoshowvisitedcolour
}
HBRUSHCHyperLink::CtlColor(CDC*pDC,UINTnCtlColor)
{
ASSERT(nCtlColor==CTLCOLOR_STATIC);
if(m_bOverControl)
pDC->SetTextColor(m_crHoverColour);
elseif(m_bVisited)
pDC->SetTextColor(m_crVisitedColour);
else
pDC->SetTextColor(m_crLinkColour);
//transparenttext.
pDC->SetBkMode(TRANSPARENT);
return(HBRUSH)GetStockObject(NULL_BRUSH);
}
voidCHyperLink::OnMouseMove(UINTnFlags,CPointpoint)
{
CStatic::OnMouseMove(nFlags,point);
if(m_bOverControl)//Cursoriscurrentlyovercontrol
{
CRectrect;
GetClientRect(rect);
if(!rect.PtInRect(point))
{
m_bOverControl=FALSE;
ReleaseCapture();
RedrawWindow();
return;
}
}
else//Cursorhasjustmovedovercontrol
{
m_bOverControl=TRUE;
RedrawWindow();
SetCapture();
}
}
BOOLCHyperLink::OnSetCursor(CWnd*/*pWnd*/,UINT/*nHitTest*/,UINT/*message*/)
{
if(m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
returnTRUE;
}
returnFALSE;
}
voidCHyperLink::PreSubclassWindow()
{
//WewanttogetmouseclicksviaSTN_CLICKED
DWORDdwStyle=GetStyle();
::SetWindowLong(GetSafeHwnd(),GWL_STYLE,dwStyle|SS_NOTIFY);
//SettheURLasthewindowtext
if(m_strURL.IsEmpty())
GetWindowText(m_strURL);
//Checkthatthewindowtextisn'tempty.Ifitis,setitastheURL.
CStringstrWndText;
GetWindowText(strWndText);
if(strWndText.IsEmpty()){
ASSERT(!m_strURL.IsEmpty());//WindowandURLbothNULL.DUH!
SetWindowText(m_strURL);
}
//Createthefont
LOGFONTlf;
GetFont()->GetLogFont(&lf);
lf.lfUnderline=m_bUnderline;
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
PositionWindow();//AdjustsizeofwindowtofitURLifnecessary
SetDefaultCursor();//Tryandloadupa"hand"cursor
//Createthetooltip
CRectrect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this,m_strURL,rect,TOOLTIP_ID);
CStatic::PreSubclassWindow();
}
/////////////////////////////////////////////////////////////////////////////
//CHyperLinkoperations
voidCHyperLink::SetURL(CStringstrURL)
{
m_strURL=strURL;
if(::IsWindow(GetSafeHwnd())){
PositionWindow();
m_ToolTip.UpdateTipText(strURL,this,TOOLTIP_ID);
}
}
CStringCHyperLink::GetURL()const
{
returnm_strURL;
}
voidCHyperLink::SetColours(COLORREFcrLinkColour,COLORREFcrVisitedColour,
COLORREFcrHoverColour/*=-1*/)
{
m_crLinkColour=crLinkColour;
m_crVisitedColour=crVisitedColour;
if(crHoverColour==-1)
m_crHoverColour=::GetSysColor(COLOR_HIGHLIGHT);
else
m_crHoverColour=crHoverColour;
if(::IsWindow(m_hWnd))
Invalidate();
}
COLORREFCHyperLink::GetLinkColour()const
{
returnm_crLinkColour;
}
COLORREFCHyperLink::GetVisitedColour()const
{
returnm_crVisitedColour;
}
COLORREFCHyperLink::GetHoverColour()const
{
returnm_crHoverColour;
}
voidCHyperLink::SetVisited(BOOLbVisited/*=TRUE*/)
{
m_bVisited=bVisited;
if(::IsWindow(GetSafeHwnd()))
Invalidate();
}
BOOLCHyperLink::GetVisited()const
{
returnm_bVisited;
}
voidCHyperLink::SetLinkCursor(HCURSORhCursor)
{
m_hLinkCursor=hCursor;
if(m_hLinkCursor==NULL)
SetDefaultCursor();
}
HCURSORCHyperLink::GetLinkCursor()const
{
returnm_hLinkCursor;
}
voidCHyperLink::SetUnderline(BOOLbUnderline/*=TRUE*/)
{
m_bUnderline=bUnderline;
if(::IsWindow(GetSafeHwnd()))
{
LOGFONTlf;
GetFont()->GetLogFont(&lf);
lf.lfUnderline=m_bUnderline;
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
Invalidate();
}
}
BOOLCHyperLink::GetUnderline()const
{
returnm_bUnderline;
}
voidCHyperLink::SetAutoSize(BOOLbAutoSize/*=TRUE*/)
{
m_bAdjustToFit=bAutoSize;
if(::IsWindow(GetSafeHwnd()))
PositionWindow();
}
BOOLCHyperLink::GetAutoSize()const
{
returnm_bAdjustToFit;
}
//Moveandresizethewindowsothatthewindowisthesamesize
//asthehyperlinktext.Thisstopsthehyperlinkcursorbeingactive
//whenitisnotdirectlyoverthetext.Ifthetextisleftjustified
//thenthewindowismerelyshrunk,butifitiscentredorright
//justifiedthenthewindowwillhavetobemovedaswell.
//
//SuggestedbyP錶K.T鴑der
voidCHyperLink::PositionWindow()
{
if(!::IsWindow(GetSafeHwnd())||!m_bAdjustToFit)
return;
//Getthecurrentwindowposition
CRectrect;
GetWindowRect(rect);
CWnd*pParent=GetParent();
if(pParent)
pParent->ScreenToClient(rect);
//Getthesizeofthewindowtext
CStringstrWndText;
GetWindowText(strWndText);
CDC*pDC=GetDC();
CFont*pOldFont=pDC->SelectObject(&m_Font);
CSizeExtent=pDC->GetTextExtent(strWndText);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
//Getthetextjustificationviathewindowstyle
DWORDdwStyle=GetStyle();
//Recalcthewindowsizeandpositionbasedonthetextjustification
if(dwStyle&SS_CENTERIMAGE)
rect.DeflateRect(0,(rect.Height()-Extent.cy)/2);
else
rect.bottom=rect.top+Extent.cy;
if(dwStyle&SS_CENTER)
rect.DeflateRect((rect.Width()-Extent.cx)/2,0);
elseif(dwStyle&SS_RIGHT)
rect.left=rect.right-Extent.cx;
else//SS_LEFT=0,sowecan'ttestforitexplicitly
rect.right=rect.left+Extent.cx;
//Movethewindow
SetWindowPos(NULL,rect.left,rect.top,rect.Width(),rect.Height(),SWP_NOZORDER);
}
/////////////////////////////////////////////////////////////////////////////
//CHyperLinkimplementation
//ThefollowingappearedinPaulDiLascia'sJan1998MSJarticles.
//Itloadsa"hand"cursorfromthewinhlp32.exemodule
voidCHyperLink::SetDefaultCursor()
{
if(m_hLinkCursor==NULL)//Nocursorhandle-loadourown
{
//Getthewindowsdirectory
CStringstrWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH),MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir+=_T("");
//Thisretrievescursor#106fromwinhlp32.exe,whichisahandpointer
HMODULEhModule=LoadLibrary(strWndDir);
if(hModule){
HCURSORhHandCursor=::LoadCursor(hModule,MAKEINTRESOURCE(106));
if(hHandCursor)
m_hLinkCursor=CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
LONGCHyperLink::GetRegKey(HKEYkey,LPCTSTRsubkey,LPTSTRretdata)
{
HKEYhkey;
LONGretval=RegOpenKeyEx(key,subkey,0,KEY_QUERY_VALUE,&hkey);
if(retval==ERROR_SUCCESS){
longdatasize=MAX_PATH;
TCHARdata[MAX_PATH];
RegQueryValue(hkey,NULL,data,&datasize);
lstrcpy(retdata,data);
RegCloseKey(hkey);
}
returnretval;
}
voidCHyperLink::ReportError(intnError)
{
CStringstr;
switch(nError){
case0:str="Theoperatingsystemisoutofmemoryorresources.";break;
caseSE_ERR_PNF:str="Thespecifiedpathwasnotfound.";break;
caseSE_ERR_FNF:str="Thespecifiedfilewasnotfound.";break;
caseERROR_BAD_FORMAT:str="The.EXEfileisinvalid(non-Win32.EXEorerrorin.EXEimage).";break;
caseSE_ERR_ACCESSDENIED:str="Theoperatingsystemdeniedaccesstothespecifiedfile.";break;
caseSE_ERR_ASSOCINCOMPLETE:str="Thefilenameassociationisincompleteorinvalid.";break;
caseSE_ERR_DDEBUSY:str="TheDDEtransactioncouldnotbecompletedbecauseotherDDEtransactions\nwerebeingprocessed.";break;
caseSE_ERR_DDEFAIL:str="TheDDEtransactionfailed.";break;
caseSE_ERR_DDETIMEOUT:str="TheDDEtransactioncouldnotbecompletedbecausetherequesttimedout.";break;
caseSE_ERR_DLLNOTFOUND:str="Thespecifieddynamic-linklibrarywasnotfound.";break;
caseSE_ERR_NOASSOC:str="Thereisnoapplicationassociatedwiththegivenfilenameextension.";break;
caseSE_ERR_OOM:str="Therewasnotenoughmemorytocompletetheoperation.";break;
caseSE_ERR_SHARE:str="Asharingviolationoccurred.";
default:str.Format("UnknownError(%d)occurred.",nError);
break;
}
str="Unabletoopenhyperlink:\n\n"+str;
AfxMessageBox(str,MB_ICONEXCLAMATION|MB_OK);
}
HINSTANCECHyperLink::GotoURL(LPCTSTRurl,intshowcmd)
{
TCHARkey[MAX_PATH+MAX_PATH];
//FirsttryShellExecute()
HINSTANCEresult=ShellExecute(NULL,_T("open"),url,NULL,NULL,showcmd);
//Ifitfailed,getthe.htmregkeyandlookuptheprogram
if((UINT)result<=HINSTANCE_ERROR){
if(GetRegKey(HKEY_CLASSES_ROOT,_T(".htm"),key)==ERROR_SUCCESS){
lstrcat(key,_T(""));
if(GetRegKey(HKEY_CLASSES_ROOT,key,key)==ERROR_SUCCESS){
TCHAR*pos;
pos=_tcsstr(key,_T("\"%1\""));
if(pos==NULL){//Noquotesfound
pos=strstr(key,_T("%1"));//Checkfor%1,withoutquotes
if(pos==NULL)//Noparameteratall...
pos=key+lstrlen(key)-1;
else
*pos='\0';//Removetheparameter
}
else
*pos='\0';//Removetheparameter
lstrcat(pos,_T(""));
lstrcat(pos,url);
result=(HINSTANCE)WinExec(key,showcmd);
}
}
}
returnresult;
}
在OnInitDialog()中调用:
CStringsUrl="www.baidu.com";
m_HyperLink.SetURL(sUrl);
m_HyperLink.SetUnderline(FALSE);//去掉下划线
相关文章推荐
- 如何在对话框中加入状态条?
- 如何在对话框中加入工具条
- 如何在对话框中加入工具条/状态条?
- 如何将自己的LOGO或者图片加入到已有的对话框界面上转载海洋女神博客--为了记住
- 在Windows Mobile和Wince(Windows Embedded CE)下进行WTL开发,如何加入超链接(HyperLink)
- 如何在对话框中加入菜单栏?
- 如何在博客内加入超链接
- 如何在win32控制台加入MFC对话框(Adding Windows To Your Console Application)
- 如何给基于对话框的应用程序加入一个工具栏?
- 如何在对话框中加入TOOLBAR?
- Flex,如何在TextArea中加入超链接
- Windows Mobile下使用Native C++(WTL, MFC, Win32)开发,如何为对话框加入菜单
- 如何在程序中加入Growl通知
- 如何在程序中加入Growl通知
- 如何在 Zen Cart 加入最新版本 FCKEditor
- 如何让listView加入的HeaderView不可点击
- 如何在程序中加入Growl通知
- VC6.0 对话框 的控件工具箱 如何找到
- ASP.Net 如何加入一个确认窗口
- 如何在程序中加入Growl通知