📄 hyperlink.cpp
字号:
g_crHoverColor = ::GetSysColor(COLOR_HIGHLIGHT);
else
g_crHoverColor = crHoverColor;
}
void CHyperLink::SetColors(HYPERLINKCOLORS& linkColors) {
g_crLinkColor = linkColors.crLink;
g_crActiveColor = linkColors.crActive;
g_crVisitedColor = linkColors.crVisited;
g_crHoverColor = linkColors.crHover;
}
void CHyperLink::GetColors(HYPERLINKCOLORS& linkColors) {
linkColors.crLink = g_crLinkColor;
linkColors.crActive = g_crActiveColor;
linkColors.crVisited = g_crVisitedColor;
linkColors.crHover = g_crHoverColor;
}
void CHyperLink::SetLinkCursor(HCURSOR hCursor) {
ASSERT(hCursor != NULL);
g_hLinkCursor = hCursor;
if (g_hLinkCursor == NULL)
SetDefaultCursor();
}
HCURSOR CHyperLink::GetLinkCursor() {
return g_hLinkCursor;
}
BOOL CHyperLink:: 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);
SETBITS(m_dwStyle, dwAdd);
if (bApply && ::IsWindow(GetSafeHwnd())) {
// If possible, APPLY the new styles on the fly
if (BITSET(dwAdd,StyleUnderline) || BITSET(dwRemove,StyleUnderline))
SwitchFont();
if (BITSET(dwAdd,StyleItalic) || BITSET(dwRemove,StyleItalic))
SwitchFont();
if (BITSET(dwAdd,StyleAutoSize))
AdjustWindow();
if (BITSET(dwRemove,StyleUseHover))
Invalidate();
}
return TRUE;
}
DWORD CHyperLink::GetLinkStyle() const {
return m_dwStyle;
}
void CHyperLink::SetURL(CString strURL,CString strTip)
{
m_strURL = strURL;
if (::IsWindow(GetSafeHwnd())) {
ShowWindow(SW_HIDE);
AdjustWindow();
if (strTip=="")
m_ToolTip.UpdateTipText(strURL, this, TOOLTIP_ID);
else
m_ToolTip.UpdateTipText(strTip, this, TOOLTIP_ID);
ShowWindow(SW_SHOW);
}
}
void CHyperLink::SetTip(CString strTip)
{
m_ToolTip.UpdateTipText(strTip, this, TOOLTIP_ID);
}
CString CHyperLink::GetURL() const {
return m_strURL;
}
void CHyperLink::SetWindowText(LPCTSTR lpszText)
{
ASSERT(lpszText != NULL);
if (::IsWindow(GetSafeHwnd())) {
// Set the window text and adjust its size while the window
// is kept hidden in order to allow dynamic modification
ShowWindow(SW_HIDE); // Hide window
// Call the base class SetWindowText()
CStatic::SetWindowText(lpszText);
// Resize the control if necessary
AdjustWindow();
ShowWindow(SW_SHOW); // Show window
}
}
void CHyperLink::SetFont(CFont* pFont)
{
ASSERT(::IsWindow(GetSafeHwnd()));
ASSERT(pFont != NULL);
// Set the window font and adjust its size while the window
// is kept hidden in order to allow dynamic modification
ShowWindow(SW_HIDE); // Hide window
LOGFONT lf;
// Create the new font
pFont->GetLogFont(&lf);
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&lf);
// Call the base class SetFont()
CStatic::SetFont(&m_Font);
// Resize the control if necessary
AdjustWindow();
ShowWindow(SW_SHOW); // Show window
}
// Function to set underline on/off
void CHyperLink::SwitchFont()
{
LOGFONT lf;
CFont* pFont = GetFont();
if (pFont != NULL) {
pFont->GetLogFont(&lf);
lf.lfUnderline = BITSET(m_dwStyle,StyleUnderline);
lf.lfItalic=BITSET(m_dwStyle,StyleItalic);
m_Font.DeleteObject();
m_Font.CreateFontIndirect(&lf);
SetFont(&m_Font);
}
}
// Move and resize the window so that its client area has the same size
// as the hyperlink text. This prevents the hyperlink cursor being active
// when it is not over the text.
void CHyperLink::AdjustWindow()
{
ASSERT(::IsWindow(GetSafeHwnd()));
if (!BITSET(m_dwStyle,StyleAutoSize))
return;
// Get the current window rect
CRect rcWnd;
GetWindowRect(rcWnd);
// For a child CWnd object, window rect is relative to the
// upper-left corner of the parent window抯 client area.
CWnd* pParent = GetParent();
if (pParent)
pParent->ScreenToClient(rcWnd);
// Get the current client rect
CRect rcClient;
GetClientRect(rcClient);
// Calc border size based on window and client rects
int borderWidth = rcWnd.Width() - rcClient.Width();
int borderHeight = rcWnd.Height() - rcClient.Height();
// Get the extent of window text
CString strWndText;
GetWindowText(strWndText);
CDC* pDC = GetDC();
CFont* pOldFont = pDC->SelectObject(&m_Font);
CSize Extent = pDC->GetTextExtent(strWndText);
pDC->SelectObject(pOldFont);
ReleaseDC(pDC);
// Get the text justification style
DWORD dwStyle = GetStyle();
// Recalc window size and position based on text justification
if (BITSET(dwStyle, SS_CENTERIMAGE))
rcWnd.DeflateRect(0, (rcWnd.Height() - Extent.cy) / 2);
else
rcWnd.bottom = rcWnd.top + Extent.cy;
if (BITSET(dwStyle, SS_CENTER))
rcWnd.DeflateRect((rcWnd.Width() - Extent.cx) / 2, 0);
else if (BITSET(dwStyle,SS_RIGHT))
rcWnd.left = rcWnd.right - Extent.cx;
else // SS_LEFT
rcWnd.right = rcWnd.left + Extent.cx;
// Move and resize the window
MoveWindow(rcWnd.left, rcWnd.top, rcWnd.Width() + borderWidth,
rcWnd.Height() + borderHeight);
}
void CHyperLink::SetVisited(BOOL bVisited /* = TRUE */) {
m_bVisited = bVisited;
}
BOOL CHyperLink::IsVisited() const {
return m_bVisited;
}
/////////////////////////////////////////////////////////////////////////////
// CHyperLink implementation
// The following function appeared in Paul DiLascia's Jan 1998
// MSJ articles. It loads a "hand" cursor from "winhlp32.exe"
// resources
void CHyperLink::SetDefaultCursor()
{
if (g_hLinkCursor == NULL) // No cursor handle - load our own
{
g_hLinkCursor=AfxGetApp()->LoadCursor("HYPERLINK");
}
}
LONG CHyperLink::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;
}
// "GotoURL" function by Stuart Patterson
// As seen in the August, 1997 Windows Developer's Journal.
HINSTANCE CHyperLink::GotoURL(LPCTSTR url, int showcmd)
{
TCHAR key[MAX_PATH + MAX_PATH];
// First try ShellExecute()
HINSTANCE result = ShellExecute(NULL, _T("open"), url, NULL,NULL, showcmd);
// If it failed, get the .htm regkey and lookup the program
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) { // No quotes found
pos = strstr(key, _T("%1")); // Check for %1, without quotes
if (pos == NULL) // No parameter at all...
pos = key+lstrlen(key)-1;
else
*pos = '\0'; // Remove the parameter
}
else
*pos = '\0'; // Remove the parameter
lstrcat(pos, _T(" "));
lstrcat(pos, url);
result = (HINSTANCE) WinExec(key,showcmd);
}
}
}
return result;
}
// Activate the link
void CHyperLink::FollowLink()
{
int result = (int) GotoURL(m_strURL, SW_SHOW);
if (result <= HINSTANCE_ERROR)
{
MessageBeep(MB_ICONEXCLAMATION); // 不能跳到链接
CString str = "不能打开链接:\n\n" + ReportError(result);
AfxMessageBox(str, MB_ICONEXCLAMATION | MB_OK);
}
else
{
// 标记链接已访问过,并重画窗口
m_bVisited = TRUE;
Invalidate();
}
}
//激活提示Activate
void CHyperLink::ActivateToolTip(BOOL Activate)
{
m_ToolTip.Activate(Activate);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -