📄 statichandset.cpp
字号:
// StaticHandset.cpp : implementation file
//
#include "stdafx.h"
#include "StaticHandset.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStaticHandset
/////////////////////////////////////////////////////////////////////////////
// CStaticHandset message handlers
#define TOOLTIP_ID 1
/////////////////////////////////////////////////////////////////////////////
//CStaticHandset
CStaticHandset::CStaticHandset()
{
m_hLinkCursor=NULL;//Nocursorasyet
m_crLinkColour=RGB(128,128,128);//Blue
m_crVisitedColour=RGB(128,128,128);//Purple
m_crHoverColour=RGB(128,128,128);
m_bOverControl=FALSE;//Cursornotyetovercontrol
m_bVisited=FALSE;//Hasn'tbeenvisitedyet.
m_bUnderline=TRUE;//Underlinethelink?
m_bAdjustToFit=TRUE;//Resizethewindowtofitthetext?
m_strURL.Empty();
}
CStaticHandset::~CStaticHandset()
{
m_Font.DeleteObject();
}
BEGIN_MESSAGE_MAP(CStaticHandset,CStatic)
//{{AFX_MSG_MAP(CStaticHandset)
ON_CONTROL_REFLECT(STN_CLICKED,OnClicked)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CHyperLinkmessagehandlers
void CStaticHandset::PreSubclassWindow()
{
//TODO:Addyourspecializedcodehereand/orcallthebaseclass
//WewanttogetmouseclicksviaSTN_CLICKED
DWORD dwStyle=GetStyle();
::SetWindowLong(GetSafeHwnd(),GWL_STYLE,dwStyle|SS_NOTIFY);
//SettheURLasthewindowtext
if(m_strURL.IsEmpty())
GetWindowText(m_strURL);
//Checkthatthewindowtextisn'tempty.Ifitis,setitastheURL.
CString strWndText;
GetWindowText(strWndText);
if(strWndText.IsEmpty())
{
ASSERT(!m_strURL.IsEmpty());//Windowand URL both NULL.DUH!
SetWindowText(m_strURL);
}
//Createthefont
LOGFONT lf;
GetFont()->GetLogFont(&lf);
lf.lfUnderline=m_bUnderline;
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
PositionWindow();//AdjustsizeofwindowtofitURLifnecessary
SetDefaultCursor();//Tryandloadupa"hand"cursor
//Createthetooltip
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this,m_strURL,rect,TOOLTIP_ID);
CStatic::PreSubclassWindow();
}
void CStaticHandset::OnClicked()
{
int result=(int)GotoURL(m_strURL,SW_SHOW);
m_bVisited=(result>HINSTANCE_ERROR);
if(!m_bVisited)
{
MessageBeep(MB_ICONEXCLAMATION);//Unabletofollowlink
ReportError(result);
}
else
SetVisited();//Repainttoshowvisitedcolour
}
BOOL CStaticHandset::PreTranslateMessage(MSG*pMsg)
{
//TODO:Addyourspecializedcodehereand/orcallthebaseclass
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
HBRUSH CStaticHandset::CtlColor(CDC*pDC,UINT nCtlColor)
{
//TODO:ChangeanyattributesoftheDChere
ASSERT(nCtlColor==CTLCOLOR_STATIC);
if(m_bOverControl)
pDC->SetTextColor(m_crHoverColour);
else if(m_bVisited)
pDC->SetTextColor(m_crVisitedColour);
else
pDC->SetTextColor(m_crLinkColour);
//transparenttext.
pDC->SetBkMode(TRANSPARENT);
return(HBRUSH)GetStockObject(NULL_BRUSH);
//TODO:Returnanon-NULLbrushiftheparent'shandlershouldnotbecalled
//returnNULL;
}
BOOL CStaticHandset::OnSetCursor(CWnd*pWnd,UINT nHitTest,UINT message)
{
//TODO:Addyourmessagehandlercodehereand/orcalldefault
if(m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
//returnCStatic::OnSetCursor(pWnd,nHitTest,message);
}
void CStaticHandset::OnMouseMove(UINT nFlags,CPoint point)
{
//TODO:Addyourmessagehandlercodehereand/orcalldefault
if(m_bOverControl)//Cursoriscurrentlyovercontrol
{
CRect rect;
GetClientRect(rect);
if(!rect.PtInRect(point))
{
m_bOverControl=FALSE;
ReleaseCapture();
RedrawWindow();
return;
}
}
else//Cursorhasjustmovedovercontrol
{
m_bOverControl=TRUE;
RedrawWindow();
SetCapture();
}
CStatic::OnMouseMove(nFlags,point);
}
/////////////////////////////////////////////////////////////////////////////
//CHyperLinkoperations
void CStaticHandset::SetURL(CString strURL)
{
m_strURL=strURL;
if(::IsWindow(GetSafeHwnd()))
{
PositionWindow();
m_ToolTip.UpdateTipText(strURL,this,TOOLTIP_ID);
}
}
CString CStaticHandset::GetURL()const
{
return m_strURL;
}
void CStaticHandset::SetColours(COLORREF crLinkColour,COLORREF crVisitedColour,
COLORREF crHoverColour/*=-1*/)
{
m_crLinkColour=crLinkColour;
m_crVisitedColour=crVisitedColour;
if(crHoverColour==-1)
m_crHoverColour=::GetSysColor(COLOR_HIGHLIGHT);
else
m_crHoverColour=crHoverColour;
if(::IsWindow(m_hWnd))
Invalidate();
}
COLORREF CStaticHandset::GetLinkColour()const
{
return m_crLinkColour;
}
COLORREF CStaticHandset::GetVisitedColour()const
{
return m_crVisitedColour;
}
COLORREF CStaticHandset::GetHoverColour()const
{
return m_crHoverColour;
}
void CStaticHandset::SetVisited(BOOL bVisited/*=TRUE*/)
{
m_bVisited=bVisited;
if(::IsWindow(GetSafeHwnd()))
Invalidate();
}
BOOL CStaticHandset::GetVisited()const
{
return m_bVisited;
}
void CStaticHandset::SetLinkCursor(HCURSOR hCursor)
{
m_hLinkCursor=hCursor;//
if(m_hLinkCursor==NULL)
SetDefaultCursor();
}
HCURSOR CStaticHandset::GetLinkCursor()const
{
return m_hLinkCursor;
}
void CStaticHandset::SetUnderline(BOOL bUnderline/*=TRUE*/)
{
m_bUnderline=bUnderline;
if(::IsWindow(GetSafeHwnd()))
{
LOGFONT lf;
GetFont()->GetLogFont(&lf);
lf.lfUnderline=m_bUnderline;
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
Invalidate();
}
}
BOOL CStaticHandset::GetUnderline()const
{
return m_bUnderline;
}
void CStaticHandset::SetAutoSize(BOOL bAutoSize/*=TRUE*/)
{
m_bAdjustToFit=bAutoSize;
if(::IsWindow(GetSafeHwnd()))
PositionWindow();
}
BOOL CStaticHandset::GetAutoSize()const
{
return m_bAdjustToFit;
}
//Moveandresizethewindowsothatthewindowisthesamesize
//asthehyperlinktext.Thisstopsthehyperlinkcursorbeingactive
//whenitisnotdirectlyoverthetext.Ifthetextisleftjustified
//thenthewindowismerelyshrunk,butifitiscentredorright
//justifiedthenthewindowwillhavetobemovedaswell.
void CStaticHandset::PositionWindow()
{
if(!::IsWindow(GetSafeHwnd())||!m_bAdjustToFit)
return;
//Getthecurrentwindowposition
CRect rect;
GetWindowRect(rect);
CWnd*pParent=GetParent();
if(pParent)
pParent->ScreenToClient(rect);
//Getthesizeofthewindowtext
CString strWndText;
GetWindowText(strWndText);
CDC*pDC=GetDC();
CFont*pOldFont=pDC->SelectObject(&m_Font);
CSize Extent=pDC->GetTextExtent(strWndText);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
//Getthetextjustificationviathewindowstyle
DWORD dwStyle=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);
else if(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
void CStaticHandset::SetDefaultCursor()
{
if(m_hLinkCursor==NULL)//Nocursorhandle-loadourown
{
//Getthewindowsdirectory
CString strWndDir;
GetWindowsDirectory(strWndDir.GetBuffer(MAX_PATH),MAX_PATH);
strWndDir.ReleaseBuffer();
strWndDir+=_T("\\winhlp32.exe");
//Thisretrievescursor#106fromwinhlp32.exe,whichisahandpointer
HMODULE hModule=LoadLibrary(strWndDir);
if(hModule)
{
HCURSOR hHandCursor=::LoadCursor(hModule,MAKEINTRESOURCE(106));
if(hHandCursor)
m_hLinkCursor=CopyCursor(hHandCursor);
}
FreeLibrary(hModule);
}
}
LONG CStaticHandset::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 CStaticHandset::ReportError(int nError)
{
CString str;
switch(nError)
{
case 0:
str="Theoperatingsystemisout\nofmemoryorresources.";
break;
case SE_ERR_PNF:
str="Thespecifiedpathwasnotfound.";
break;
case SE_ERR_FNF:
str="Thespecifiedfilewasnotfound.";
break;
case ERROR_BAD_FORMAT:
str="The.EXEfileisinvalid\n(non-Win32.EXEorerrorin.EXEimage).";
break;
case SE_ERR_ACCESSDENIED:
str="Theoperatingsystemdenied\naccesstothespecifiedfile.";
break;
case SE_ERR_ASSOCINCOMPLETE:
str="Thefilenameassociationis\nincompleteorinvalid.";
break;
case SE_ERR_DDEBUSY:
str="TheDDEtransactioncouldnot\nbecompletedbecauseotherDDEtransactions\nwerebeingprocessed.";
break;
case SE_ERR_DDEFAIL:
str="TheDDEtransactionfailed.";
break;
case SE_ERR_DDETIMEOUT:
str="TheDDEtransactioncouldnot\nbecompletedbecausetherequesttimedout.";
break;
case SE_ERR_DLLNOTFOUND:
str="Thespecifieddynamic-linklibrarywasnotfound.";
break;
case SE_ERR_NOASSOC:
str="Thereisnoapplicationassociated\nwiththegivenfilenameextension.";
break;
case SE_ERR_OOM:
str="Therewasnotenoughmemorytocompletetheoperation.";
break;
case SE_ERR_SHARE:
str="Asharingviolationoccurred.";
default:
str.Format("UnknownError(%d)occurred.",nError);
break;
}
str="Unabletoopenhyperlink:\n\n"+str;
AfxMessageBox(str,MB_ICONEXCLAMATION|MB_OK);
}
HINSTANCE CStaticHandset::GotoURL(LPCTSTR url,int showcmd)
{
TCHAR key[MAX_PATH+MAX_PATH];
//FirsttryShellExecute()
HINSTANCE result=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("\\shell\\open\\command"));
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);
}
}
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -