📄 chyperlink.h
字号:
#ifndef __CHYPERLINK_INCLUDED__
#define __CHYPERLINK_INCLUDED__
//
// 僗僞僥傿僢僋僐儞僩儘乕儖傪Subclass壔偟偰僴僀僷乕儕儞僋傪僒億乕僩
//
// Usage: CHyperLink hyperlink;
// hyperlink.Attach( GetDlgItem( hWnd, IDC_URL ) );
//
class CHyperLink
{
public:
CHyperLink() : m_hWnd(NULL), m_lpLabel(NULL),
m_hCursor(NULL), m_hFont(NULL),
m_lpHyperLink(NULL), m_lpfnOldWndProc(NULL)
{
}
~CHyperLink()
{
Detach();
}
// 僐儞僩儘乕儖偵傾僞僢僠偡傞
BOOL Attach( HWND hWnd, LPCSTR lpLabel = NULL, LPCSTR lpHyperLink = NULL )
{
if( m_hWnd )
return FALSE; // 婛偵Attach嵪傒
m_hWnd = hWnd;
// 暥帤楍傪僐僺乕
if( lpLabel ) {
m_lpLabel = new CHAR[::lstrlen(lpLabel)+1];
::lstrcpy( m_lpLabel, lpLabel );
::SendMessage( m_hWnd, WM_SETTEXT, 0, (LPARAM)m_lpLabel );
}
// 昞帵偲僴僀僷乕儕儞僋偑摨偠応崌偼僴僀僷乕儕儞僋偼儔儀儖偲摨偠偵
if( lpLabel && !lpHyperLink ) {
m_lpHyperLink = new CHAR[::lstrlen(lpLabel)+1];
::lstrcpy( m_lpHyperLink, lpLabel );
}
// 僴僀僷乕儕儞僋偑暿偺応崌
if( lpHyperLink ) {
m_lpHyperLink = new CHAR[::lstrlen(lpHyperLink)+1];
::lstrcpy( m_lpHyperLink, lpHyperLink );
}
// 億僀儞僩僇乕僜儖(柍偗傟偽僨僼僅儖僩)
m_hCursor = ::LoadCursor( NULL, IDC_HAND );
// 傾儞僟乕儔僀儞僼僅儞僩偺嶌惉
HFONT m_hOldFont = (HFONT)::SendMessage( hWnd, WM_GETFONT, 0, 0 );
LOGFONT lFont;
::GetObject( m_hOldFont, sizeof(LOGFONT), &lFont );
lFont.lfUnderline = TRUE;
m_hFont = ::CreateFontIndirect( &lFont );
// 儔儀儖偺昞帵椞堟傪寁嶼
CalcLabelRectangle();
// 僗僞僀儖偺曄峏(捠抦傪僀僱乕僽儖偵偡傞)
DWORD dwStyle = ::GetWindowLong( hWnd, GWL_STYLE );
dwStyle |= SS_NOTIFY;
::SetWindowLong( hWnd, GWL_STYLE, (LONG)dwStyle );
// 僐儞僩儘乕儖偺僒僽僋儔僗壔
m_lpfnOldWndProc = (WNDPROC)::SetWindowLong( hWnd, GWL_WNDPROC, (LONG)HyperLinkProc );
// This傪杽傔崬傓
::SetWindowLong( hWnd, GWL_USERDATA, (LONG)this );
return TRUE;
}
BOOL Detach()
{
if( m_hWnd ) {
// 僒僽僋儔僗壔傪夝彍
if( m_lpfnOldWndProc ) {
::SetWindowLong( m_hWnd, GWL_WNDPROC, (LONG)m_lpfnOldWndProc );
m_lpfnOldWndProc = NULL;
}
// 僼僅儞僩傪嶍彍
if( m_hFont ) {
::DeleteObject( (HGDIOBJ)m_hFont );
m_hFont = NULL;
}
// 暥帤楍傪嶍彍
if( m_lpLabel ) {
delete[] m_lpLabel;
m_lpLabel = NULL;
}
if( m_lpHyperLink ) {
delete[] m_lpHyperLink;
m_lpHyperLink = NULL;
}
}
m_hWnd = NULL;
return TRUE;
}
BOOL SetLabel( LPCSTR lpLabel )
{
if( m_lpLabel ) {
delete[] m_lpLabel;
}
m_lpLabel = new CHAR[::lstrlen(lpLabel)+1];
::lstrcpy( m_lpLabel, lpLabel );
// 僐儞僩儘乕儖偵暥帤楍傪愝掕
::SendMessage( m_hWnd, WM_SETTEXT, 0, (LPARAM)m_lpLabel );
// 儔儀儖偺昞帵椞堟傪寁嶼
CalcLabelRectangle();
}
BOOL SetHyperLink( LPCSTR lpHyperLink )
{
if( m_lpHyperLink ) {
delete[] m_lpHyperLink;
}
m_lpHyperLink = new CHAR[::lstrlen(lpHyperLink)+1];
::lstrcpy( m_lpHyperLink, lpHyperLink );
}
protected:
BOOL CalcLabelRectangle()
{
if( !::IsWindow(m_hWnd) )
return FALSE;
if( !m_lpLabel )
return FALSE;
RECT rcClient;
::GetClientRect( m_hWnd, &rcClient );
m_rcLabel = rcClient;
HDC hDC = ::GetDC( m_hWnd );
HFONT hOldFont = (HFONT)::SelectObject( hDC, m_hFont );
// 僗僞僀儖
DWORD dwStyle = ::GetWindowLong( m_hWnd, GWL_STYLE );
INT nDrawStyle = DT_LEFT;
if( dwStyle && SS_CENTER ) {
nDrawStyle = DT_CENTER;
} else if( dwStyle && SS_RIGHT ) {
nDrawStyle = DT_RIGHT;
}
// 暥帤楍昞帵偲昤夋椞堟偺寁嶼
::DrawText( hDC, m_lpLabel, -1, &m_rcLabel, nDrawStyle | DT_WORDBREAK | DT_CALCRECT );
::SelectObject( hDC, hOldFont );
// 僗僞僀儖偵傛偭偰僆僼僙僢僩傪寁嶼
if( dwStyle & SS_CENTER ) {
::OffsetRect( &m_rcLabel, (rcClient.right - m_rcLabel.right) / 2, 0 );
} else if (dwStyle & SS_RIGHT) {
::OffsetRect( &m_rcLabel, rcClient.right - m_rcLabel.right, 0 );
}
return true;
}
static LRESULT CALLBACK HyperLinkProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CHyperLink* pHyperLink = reinterpret_cast<CHyperLink*>(::GetWindowLong( hWnd, GWL_USERDATA));
switch( msg ) {
case WM_DESTROY:
{
// 2003/10/11 儕儕乕僗屻偵fix...
WNDPROC pWndProcOld = pHyperLink->m_lpfnOldWndProc;
pHyperLink->Detach();
return CallWindowProc( pWndProcOld, hWnd, msg, wParam, lParam );
}
break;
case WM_LBUTTONDOWN: {
POINT pt = { LOWORD(lParam), HIWORD(lParam) };
if( pHyperLink->m_lpLabel && pHyperLink->m_lpHyperLink ) {
if( ::PtInRect( &pHyperLink->m_rcLabel, pt ) ) {
::ShellExecute( hWnd, NULL, pHyperLink->m_lpHyperLink, NULL, NULL, SW_SHOWNORMAL );
return TRUE;
}
}
}
break;
case WM_MOUSEMOVE: {
POINT pt = { LOWORD(lParam), HIWORD(lParam) };
if( pHyperLink->m_lpLabel && pHyperLink->m_lpHyperLink ) {
if( ::PtInRect( &pHyperLink->m_rcLabel, pt ) ) {
::SetCursor( pHyperLink->m_hCursor );
return TRUE;
}
}
}
break;
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hDC = ::BeginPaint( hWnd, &ps );
DWORD dwStyle = ::GetWindowLong( hWnd, GWL_STYLE );
INT nDrawStyle = DT_LEFT;
if( dwStyle && SS_CENTER ) {
nDrawStyle = DT_CENTER;
} else if( dwStyle && SS_RIGHT ) {
nDrawStyle = DT_RIGHT;
}
// 傾僩儕價儏乕僩
::SetBkMode( hDC, TRANSPARENT );
::SetTextColor( hDC, 0x00FF0000 );
// 暥帤楍昞帵
if( pHyperLink->m_lpLabel ) {
HFONT hOldFont = (HFONT)::SelectObject( hDC, pHyperLink->m_hFont );
::DrawText( hDC, pHyperLink->m_lpLabel, -1, &pHyperLink->m_rcLabel, nDrawStyle | DT_WORDBREAK );
::SelectObject( hDC, hOldFont );
}
::EndPaint( hWnd, &ps );
}
return TRUE;
default:
break;
}
return CallWindowProc( pHyperLink->m_lpfnOldWndProc, hWnd, msg, wParam, lParam );
}
HWND m_hWnd;
HFONT m_hFont;
HFONT m_hOldFont;
HCURSOR m_hCursor;
WNDPROC m_lpfnOldWndProc;
RECT m_rcLabel;
LPSTR m_lpLabel;
LPSTR m_lpHyperLink;
private:
};
#endif // !__CHYPERLINK_INCLUDED__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -