// XHyperLink.cpp Version 1.0
//
// XHyperLink static control. Will open the default browser with the given URL
// when the user clicks on the link.
//
// Copyright (C) 1997 - 1999 Chris Maunder
// All rights reserved. May not be sold for profit.
//
// Thanks to P�l K. T�nder for auto-size and window caption changes.
//
// "GotoURL" function by Stuart Patterson
// As seen in the August, 1997 Windows Developer's Journal.
// Copyright 1997 by Miller Freeman, Inc. All rights reserved.
// Modified by Chris Maunder to use TCHARs instead of chars.
//
// "Default hand cursor" from Paul DiLascia's Jan 1998 MSJ article.
//
// 2/29/00 -- P. Shaffer standard font mod.
//
///////////////////////////////////////////////////////////////////////////////
//
// Modified by: Hans Dietrich
// hdietrich@gmail.com
//
///////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "XHyperLink.h"
#include "atlconv.h" // for Unicode conversion
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma warning(disable : 4996) // This function or variable may be unsafe
#define TOOLTIP_ID 1
// Uncomment following line to enable error message box for URL navigation
//#define XHYPERLINK_REPORT_ERROR
#ifndef IDC_HAND
#define IDC_HAND MAKEINTRESOURCE(32649) // From WINUSER.H
#endif
// sends message to parent when hyperlink is clicked (see SetNotifyParent())
UINT WM_XHYPERLINK_CLICKED = ::RegisterWindowMessage(_T("WM_XHYPERLINK_CLICKED"));
///////////////////////////////////////////////////////////////////////////////
// CXHyperLink
BEGIN_MESSAGE_MAP(CXHyperLink, CStatic)
//{{AFX_MSG_MAP(CXHyperLink)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_SETCURSOR()
ON_WM_MOUSEMOVE()
ON_WM_TIMER()
ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////////
// ctor
CXHyperLink::CXHyperLink()
{
m_hLinkCursor = NULL; // No cursor as yet
m_crLinkColour = RGB(0,0,238); // Blue
m_crVisitedColour = RGB(85,26,139); // Purple
m_crHoverColour = RGB(255,0,0); // Red
m_bOverControl = FALSE; // Cursor not yet over control
m_bVisited = FALSE; // Hasn't been visited yet.
m_nUnderline = ulHover; // Underline the link?
m_bAdjustToFit = TRUE; // Resize the window to fit the text?
m_strURL = _T("");
m_nTimerID = 100;
m_bNotifyParent = FALSE; // TRUE = notify parent
m_bIsURLEnabled = TRUE; // TRUE = navigate to url
m_bToolTip = TRUE; // TRUE = display tooltip
m_crBackground = (UINT) -1; // set to default (no bg color)
m_bAlwaysOpenNew = FALSE; // TRUE = always open new browser window
}
///////////////////////////////////////////////////////////////////////////////
// dtor
CXHyperLink::~CXHyperLink()
{
TRACE(_T("in CXHyperLink::~CXHyperLink\n"));
if (m_hLinkCursor)
DestroyCursor(m_hLinkCursor);
m_hLinkCursor = NULL;
m_UnderlineFont.DeleteObject();
if (m_Brush.GetSafeHandle())
m_Brush.DeleteObject();
}
/////////////////////////////////////////////////////////////////////////////
// CXHyperLink overrides
///////////////////////////////////////////////////////////////////////////////
// DestroyWindow
BOOL CXHyperLink::DestroyWindow()
{
KillTimer(m_nTimerID);
return CStatic::DestroyWindow();
}
///////////////////////////////////////////////////////////////////////////////
// PreTranslateMessage
BOOL CXHyperLink::PreTranslateMessage(MSG* pMsg)
{
m_ToolTip.RelayEvent(pMsg);
return CStatic::PreTranslateMessage(pMsg);
}
///////////////////////////////////////////////////////////////////////////////
// PreSubclassWindow
void CXHyperLink::PreSubclassWindow()
{
// We want to get mouse clicks via STN_CLICKED
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);
// Set the URL as the window text
if (m_strURL.IsEmpty())
GetWindowText(m_strURL);
// Check that the window text isn't empty. If it is, set it as the URL.
CString strWndText;
GetWindowText(strWndText);
if (strWndText.IsEmpty())
{
ASSERT(!m_strURL.IsEmpty()); // Window and URL both NULL. DUH!
SetWindowText(m_strURL);
}
CFont* pFont = GetFont();
if (!pFont)
{
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
if (hFont == NULL)
hFont = (HFONT) GetStockObject(ANSI_VAR_FONT);
if (hFont)
pFont = CFont::FromHandle(hFont);
}
ASSERT(pFont->GetSafeHandle());
// Create the underline font
LOGFONT lf;
pFont->GetLogFont(&lf);
m_StdFont.CreateFontIndirect(&lf);
lf.lfUnderline = (BYTE) TRUE;
m_UnderlineFont.CreateFontIndirect(&lf);
PositionWindow(); // Adjust size of window to fit URL if necessary
SetDefaultCursor(); // Try and load up a "hand" cursor
SetUnderline();
// Create the tooltip
if (m_bToolTip)
{
CRect rect;
GetClientRect(rect);
m_ToolTip.Create(this);
m_ToolTip.AddTool(this, m_strURL, rect, TOOLTIP_ID);
}
CStatic::PreSubclassWindow();
}
/////////////////////////////////////////////////////////////////////////////
// CXHyperLink message handlers
///////////////////////////////////////////////////////////////////////////////
// OnClicked
void CXHyperLink::OnClicked()
{
m_bOverControl = FALSE;
int result = HINSTANCE_ERROR + 1;
if (m_bIsURLEnabled)
result = (int)(INT_PTR)GotoURL(m_strURL, SW_SHOW, m_bAlwaysOpenNew);
m_bVisited = (result > HINSTANCE_ERROR);
if (!m_bVisited)
{
MessageBeep(MB_ICONEXCLAMATION); // Unable to follow link
ReportError(result);
}
else
SetVisited(); // Repaint to show visited colour
NotifyParent();
}
///////////////////////////////////////////////////////////////////////////////
// CtlColor
#ifdef _DEBUG
HBRUSH CXHyperLink::CtlColor(CDC* pDC, UINT nCtlColor)
#else
HBRUSH CXHyperLink::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
#endif
{
ASSERT(nCtlColor == CTLCOLOR_STATIC);
if (m_bOverControl)
pDC->SetTextColor(m_crHoverColour);
else if (m_bVisited)
pDC->SetTextColor(m_crVisitedColour);
else
pDC->SetTextColor(m_crLinkColour);
// transparent text.
pDC->SetBkMode(TRANSPARENT);
if (m_Brush.GetSafeHandle())
{
pDC->SetBkColor(m_crBackground);
return (HBRUSH) m_Brush;
}
else
{
return (HBRUSH)GetStockObject(NULL_BRUSH);
}
}
///////////////////////////////////////////////////////////////////////////////
// OnMouseMove
void CXHyperLink::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bOverControl) // Cursor has just moved over control
{
m_bOverControl = TRUE;
if (m_nUnderline == ulHover)
SetFont(&m_UnderlineFont);
Invalidate();
SetTimer(m_nTimerID, 100, NULL);
}
CStatic::OnMouseMove(nFlags, point);
}
///////////////////////////////////////////////////////////////////////////////
// OnTimer
void CXHyperLink::OnTimer(UINT nIDEvent)
{
CPoint p(GetMessagePos());
ScreenToClient(&p);
CRect rect;
GetClientRect(rect);
if (!rect.PtInRect(p))
{
m_bOverControl = FALSE;
KillTimer(m_nTimerID);
if (m_nUnderline != ulAlways)
SetFont(&m_StdFont);
rect.bottom+=10;
InvalidateRect(rect);
}
CStatic::OnTimer(nIDEvent);
}
///////////////////////////////////////////////////////////////////////////////
// OnSetCursor
BOOL CXHyperLink::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/)
{
if (m_hLinkCursor)
{
::SetCursor(m_hLinkCursor);
return TRUE;
}
return FALSE;
}
///////////////////////////////////////////////////////////////////////////////
// OnEraseBkgnd
BOOL CXHyperLink::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(rect);
if (m_crBackground != (UINT)-1)
pDC->FillSolidRect(rect, m_crBackground);
else
pDC->FillSoli
通过已安装exe程序名获取程序路径
4星 · 超过85%的资源 需积分: 31 54 浏览量
2010-10-02
20:56:55
上传
评论 1
收藏 66KB RAR 举报
liquanhai
- 粉丝: 563
- 资源: 23
最新资源
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
- C语言实现直接插入排序、希尔排序、选择排序、冒泡排序、堆排序、快速排序、归并排序、计数排序,并带图详解.zip
- 常用工具集参考用于图像等数据处理
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
前往页