📄 jfhyperlink.cpp
字号:
// HyperLink.cpp : implementation file
//
// HyperLink static control.
//
// Copyright (C) 1997, 1998 Giancarlo Iovino (giancarlo@saria.com)
// All rights reserved. May not be sold for profit.
//
// This code is based on CJfHyperLink by Chris Maunder.
// "GotoURL" function by Stuart Patterson appeared in the Aug, 1997
// Windows Developer's Journal.
// "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
#include "stdafx.h"
#include "JfHyperLink.h"
#ifdef _DEBUG
#define new DEBUm_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define TOOLTIP_ID 1
#define SETBITS(dw, bits) (dw |= bits)
#define CLEARBITS(dw, bits) (dw &= ~(bits))
#define BITSET(dw, bit) (((dw) & (bit)) != 0L)
/////////////////////////////////////////////////////////////////////////////
// CJfHyperLink
const DWORD CJfHyperLink::StyleUnderline = 0x00000001; // Underline bit
const DWORD CJfHyperLink::StyleUseHover = 0x00000002; // Hand over coloring bit
const DWORD CJfHyperLink::StyleAutoSize = 0x00000004; // Auto size bit
const DWORD CJfHyperLink::StyleDownClick = 0x00000008; // Down click mode bit
const DWORD CJfHyperLink::StyleGetFocusOnClick = 0x00000010; // Get focus on click bit
const DWORD CJfHyperLink::StyleNoHandCursor = 0x00000020; // No hand cursor bit
const DWORD CJfHyperLink::StyleNoActiveColor = 0x00000040; // No active color bit
CJfHyperLink::CJfHyperLink()
{
m_pOwner = false;
m_bOverControl = FALSE; // Cursor not yet over control
m_bVisited = FALSE; // Link has not been visited yet
m_bLinkActive = FALSE; // Control doesn't own the focus yet
m_strURL.Empty(); // Set URL to an empty string
// Set default styles
m_dwStyle = StyleUnderline|StyleAutoSize|StyleGetFocusOnClick;
m_crLinkColor = RGB(0, 0, 255); // Blue
m_crActiveColor = RGB(0, 128, 128); // Dark cyan
m_crVisitedColor = RGB(128, 0, 128); // Purple
m_crHoverColor = RGB(255, 0, 0 ); // Red
m_hLinkCursor = NULL;
}
CJfHyperLink::~CJfHyperLink()
{
m_Font.DeleteObject();
}
IMPLEMENT_DYNAMIC(CJfHyperLink, CStatic)
BEGIN_MESSAGE_MAP(CJfHyperLink, CStatic)
//{{AFX_MSm_MAP(CJfHyperLink)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_SETFOCUS()
ON_WM_KILLFOCUS()
ON_WM_KEYDOWN()
ON_WM_NCHITTEST()
ON_WM_LBUTTONDOWN()
//}}AFX_MSm_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CJfHyperLink message handlers
BOOL CJfHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
void CJfHyperLink::PreSubclassWindow()
{
CString strWndText;
// If the URL string is empty try to set it to the window text
if (m_strURL.IsEmpty())
GetWindowText(m_strURL);
// Check that the window text isn't empty.
// If it is, set it as URL string.
GetWindowText(strWndText);
if (strWndText.IsEmpty())
CStatic::SetWindowText(m_strURL);
// Get the current window font
CFont* pFont = GetFont();
if (pFont != NULL)
{
LOGFONT lf;
pFont->GetLogFont(&lf);
lf.lfUnderline = BITSET(m_dwStyle, StyleUnderline);
if (m_Font.CreateFontIndirect(&lf))
CStatic::SetFont(&m_Font);
// Adjust window size to fit URL if necessary
AdjustWindow();
}
else
{
// if GetFont() returns NULL then probably the static
// control is not of a text type: it's better to set
// auto-resizing off
CLEARBITS(m_dwStyle,StyleAutoSize);
}
if (!BITSET(m_dwStyle,StyleNoHandCursor))
SetDefaultCursor(); // Try to load an "hand" cursor
// Create the tooltip
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
CStatic::PreSubclassWindow();
}
// Handler for WM_CTLCOLOR reflected message (see message map)
HBRUSH CJfHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
{
ASSERT(nCtlColor == CTLCOLOR_STATIC);
if (m_bOverControl && BITSET(m_dwStyle,StyleUseHover))
pDC->SetTextColor(m_crHoverColor);
else if (!BITSET(m_dwStyle,StyleNoActiveColor) && m_bLinkActive)
pDC->SetTextColor(m_crActiveColor);
else if (m_bVisited)
pDC->SetTextColor(m_crVisitedColor);
else
pDC->SetTextColor(m_crLinkColor);
// Set transparent drawing mode
pDC->SetBkMode(TRANSPARENT);
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
void CJfHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
if (m_bOverControl) // Cursor currently over control
{
CRect rect;
GetClientRect(rect);
if (!rect.PtInRect(point))
{
m_bOverControl = FALSE;
ReleaseCapture();
Invalidate();
return;
}
}
else // Cursor has left control area
{
m_bOverControl = TRUE;
Invalidate();
SetCapture();
}
}
//////////////////////////////////////////////////////////////////////////
// "Normally, a static control does not get mouse events unless it has
// SS_NOTIFY. This achieves the same effect as SS_NOTIFY, but it's fewer
// lines of code and more reliable than turning on SS_NOTIFY in OnCtlColor
// because Windows doesn't send WM_CTLCOLOR to bitmap static controls."
// (Paul DiLascia)
UINT CJfHyperLink::OnNcHitTest(CPoint /*point*/)
{
return HTCLIENT;
}
void CJfHyperLink::OnLButtonDown(UINT /*nFlags*/, CPoint /*point*/)
{
if (BITSET(m_dwStyle,StyleGetFocusOnClick))
SetFocus(); // Set the focus and make the link active
if (BITSET(m_dwStyle,StyleDownClick))
FollowLink();
m_bLinkActive = TRUE;
}
void CJfHyperLink::OnLButtonUp(UINT /*nFlags*/, CPoint /*point*/)
{
if (m_bLinkActive && !BITSET(m_dwStyle,StyleDownClick))
FollowLink();
}
BOOL CJfHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
{
if (m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
}
void CJfHyperLink::OnSetFocus(CWnd* /*pOldWnd*/)
{
m_bLinkActive = TRUE;
Invalidate(); // Repaint to set the focus
}
void CJfHyperLink::OnKillFocus(CWnd* /*pNewWnd*/)
{
// Assume that control lost focus = mouse out
// this avoid troubles with the Hover color
m_bOverControl = FALSE;
m_bLinkActive = FALSE;
Invalidate(); // Repaint to unset the focus
}
void CJfHyperLink::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_SPACE)
FollowLink();
else
CStatic::OnKeyDown(nChar, nRepCnt, nFlags);
}
/////////////////////////////////////////////////////////////////////////////
// CJfHyperLink operations
void CJfHyperLink::SetColors( COLORREF crLinkColor,
COLORREF crActiveColor,
COLORREF crVisitedColor,
COLORREF crHoverColor /* = -1 */)
{
m_crLinkColor = crLinkColor;
m_crActiveColor = crActiveColor;
m_crVisitedColor = crVisitedColor;
if (crHoverColor == -1)
m_crHoverColor = ::GetSysColor(COLOR_HIGHLIGHT);
else
m_crHoverColor = crHoverColor;
}
void CJfHyperLink::SetLinkColor(COLORREF crLinkColor)
{
m_crLinkColor = crLinkColor;
}
void CJfHyperLink::SetActiveColor(COLORREF crActiveColor)
{
m_crActiveColor = crActiveColor;
}
void CJfHyperLink::SetVisitedColor(COLORREF crVisitedColor)
{
m_crVisitedColor = crVisitedColor;
}
void CJfHyperLink::SetHoverColor(COLORREF crHoverColor)
{
if (crHoverColor == -1)
m_crHoverColor = ::GetSysColor(COLOR_HIGHLIGHT);
else
m_crHoverColor = crHoverColor;
}
void CJfHyperLink::SetLinkCursor(HCURSOR hCursor)
{
ASSERT(hCursor != NULL);
m_hLinkCursor = hCursor;
if (m_hLinkCursor == NULL)
SetDefaultCursor();
}
HCURSOR CJfHyperLink::GetLinkCursor() {
return m_hLinkCursor;
}
BOOL CJfHyperLink:: ModifyLinkStyle(DWORD dwRemove, DWORD dwAdd,
BOOL bApply /* =TRUE */)
{
// Check if we are adding and removing the same style
if ((dwRemove & dwAdd) != 0L)
return FALSE;
// Remove old styles and set the new ones
CLEARBITS(m_dwStyle, dwRemove);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -