AxibugEmuOnline/References/virtuanessrc097-master/CHyperLink.h

254 lines
12 KiB
C
Raw Permalink Normal View History

<EFBFBD><EFBFBD>#ifndef __CHYPERLINK_INCLUDED__
#define __CHYPERLINK_INCLUDED__
//
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0SubclassSW0f0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0
//
// 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();
}
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0k0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0Y0<EFBFBD>0
BOOL Attach( HWND hWnd, LPCSTR lpLabel = NULL, LPCSTR lpHyperLink = NULL )
{
if( m_hWnd )
return FALSE; // <EFBFBD>ek0Attachn0
m_hWnd = hWnd;
// <EFBFBD>eW[R<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0
if( lpLabel ) {
m_lpLabel = new CHAR[::lstrlen(lpLabel)+1];
::lstrcpy( m_lpLabel, lpLabel );
::SendMessage( m_hWnd, WM_SETTEXT, 0, (LPARAM)m_lpLabel );
}
// h<EFBFBD>:yh0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0L0 TX04XTo0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0o0<EFBFBD>0<EFBFBD>0<EFBFBD>0h0 TX0k0
if( lpLabel && !lpHyperLink ) {
m_lpHyperLink = new CHAR[::lstrlen(lpLabel)+1];
::lstrcpy( m_lpHyperLink, lpLabel );
}
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0L0%Rn04XT
if( lpHyperLink ) {
m_lpHyperLink = new CHAR[::lstrlen(lpHyperLink)+1];
::lstrcpy( m_lpHyperLink, lpHyperLink );
}
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0(!qQ0<EFBFBD>0p0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0)
#ifndef IDC_HAND
#define IDC_HAND MAKEINTRESOURCE(32649)
#endif
m_hCursor = ::LoadCursor( NULL, IDC_HAND );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0n0\Ob
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 );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0n0h<EFBFBD>:y<EFBFBD><EFBFBD>W<EFBFBD>0<EFBFBD><EFBFBD>{
CalcLabelRectangle();
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0n0 Y<EFBFBD>f(<EFBFBD><EFBFBD>w<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0k0Y0<EFBFBD>0)
DWORD dwStyle = ::GetWindowLong( hWnd, GWL_STYLE );
dwStyle |= SS_NOTIFY;
::SetWindowLong( hWnd, GWL_STYLE, (LONG)dwStyle );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0n0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0S
m_lpfnOldWndProc = (WNDPROC)::SetWindowLong( hWnd, GWL_WNDPROC, (LONG)HyperLinkProc );
// This<EFBFBD>0<EFBFBD>W<EFBFBD>0<EFBFBD><EFBFBD><EFBFBD>0
::SetWindowLong( hWnd, GWL_USERDATA, (LONG)this );
return TRUE;
}
BOOL Detach()
{
if( m_hWnd ) {
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0S<EFBFBD>0<EFBFBD><EFBFBD>d<EFBFBD>
if( m_lpfnOldWndProc ) {
::SetWindowLong( m_hWnd, GWL_WNDPROC, (LONG)m_lpfnOldWndProc );
m_lpfnOldWndProc = NULL;
}
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0JRd<EFBFBD>
if( m_hFont ) {
::DeleteObject( (HGDIOBJ)m_hFont );
m_hFont = NULL;
}
// <EFBFBD>eW[R<EFBFBD>0JRd<EFBFBD>
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 );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0k0<EFBFBD>eW[R<EFBFBD>0-<EFBFBD><EFBFBD>[
::SendMessage( m_hWnd, WM_SETTEXT, 0, (LPARAM)m_lpLabel );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0n0h<EFBFBD>:y<EFBFBD><EFBFBD>W<EFBFBD>0<EFBFBD><EFBFBD>{
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 );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0
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;
}
// <EFBFBD>eW[Rh<EFBFBD>:yh0<EFBFBD>c;u<EFBFBD><EFBFBD>Wn0<EFBFBD><EFBFBD>{
::DrawText( hDC, m_lpLabel, -1, &m_rcLabel, nDrawStyle | DT_WORDBREAK | DT_CALCRECT );
::SelectObject( hDC, hOldFont );
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0k0<EFBFBD>0c0f0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD><EFBFBD>{
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 <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>_k0fix...
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( pHyperLink->m_hCursor && ::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;
}
// <EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0<EFBFBD>0
::SetBkMode( hDC, TRANSPARENT );
::SetTextColor( hDC, 0x00FF0000 );
// <EFBFBD>eW[Rh<EFBFBD>:y
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__