HyperLinker.cpp
上传用户:yangb198
上传日期:2021-03-29
资源大小:14k
文件大小:5k
- // HyperLinker.cpp : implementation file
- //
- #include "stdafx.h"
- #include "HyperLinkerDemo.h"
- #include "HyperLinker.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CHyperLinker
- CHyperLinker::CHyperLinker()
- {
- m_bAboveControl = FALSE;
- m_bUnderLine = FALSE;
- m_bVisited = FALSE;
- }
- CHyperLinker::~CHyperLinker()
- {
- }
- BEGIN_MESSAGE_MAP(CHyperLinker, CStatic)
- //{{AFX_MSG_MAP(CHyperLinker)
- ON_WM_LBUTTONDOWN()
- ON_WM_SETCURSOR()
- ON_WM_MOUSEMOVE()
- ON_WM_CTLCOLOR_REFLECT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CHyperLinker message handlers
- void CHyperLinker::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- OpenUsingShellExecute(); // shell外部程序
- m_bVisited = TRUE; // 表示应经点击过超级链接文本框
- Invalidate(); // 调用CtrlColor重绘文本
- CStatic::OnLButtonDown(nFlags, point);
- }
- BOOL CHyperLinker::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
- {
- // TODO: Add your message handler code here and/or call default
- HCURSOR LinkCurlor = ::AfxGetApp()->LoadCursor(IDC_CUR_HAND);
- ::SetCursor(LinkCurlor);
- return TRUE;
- return CStatic::OnSetCursor(pWnd, nHitTest, message);
- }
- void CHyperLinker::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CRect rect;
- GetClientRect(rect); // 得到当前文本框的矩形区域
- static BOOL bIsIn = FALSE; // 判断前一次鼠标是否已经在文本框区域
- if (rect.PtInRect(point)) // 在区域内
- {
- m_bAboveControl = TRUE; // 记录鼠标已经停留在超级链接文本框的上方
- if (!bIsIn) // 如果是第一次停留,则用其他颜色重绘文本
- {
- SetCapture(); // 设置鼠标捕获
- bIsIn = TRUE;
- Invalidate(); // 调用CtrlColor重绘文本
- }
- }
- else
- {
- m_bAboveControl = FALSE; // 记录鼠标不在超级链接文本框的上方
- if (bIsIn) // 如果是第一次离开区域,则用其他颜色重绘文本
- {
- ReleaseCapture(); // 释放鼠标捕获
- bIsIn = FALSE;
- Invalidate(); // 调用CtrlColor重绘文本
- }
- }
- CStatic::OnMouseMove(nFlags, point);
- }
- HBRUSH CHyperLinker::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- // TODO: Change any attributes of the DC here
- ASSERT(nCtlColor == CTLCOLOR_STATIC);
-
- DWORD dwStyle = GetStyle();
- if (!(dwStyle & SS_NOTIFY))
- {
- ::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle | SS_NOTIFY);
- }
-
- HBRUSH hbr = NULL;
- if ((dwStyle & 0xFF) <= SS_RIGHT)
- {
- // modify the font to be underline
- if (!((HFONT)m_Font))
- {
- LOGFONT lf;
- GetFont()->GetObject(sizeof(lf), &lf);
- lf.lfUnderline = m_bUnderLine;
- m_Font.CreateFontIndirect(&lf);
- }
- pDC->SelectObject(&m_Font);
- // set the text color
- if (m_bVisited)
- {
- pDC->SetTextColor(m_VisitedColor);
- }
- else
- {
- if (m_bAboveControl)
- {
- pDC->SetTextColor(m_CoverColor);
- }
- else
- {
- pDC->SetTextColor(m_InitColor);
- }
- }
- pDC->SetBkMode(TRANSPARENT);
- hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH);
- }
- // TODO: Return a non-NULL brush if the parent's handler should not be called
- return hbr;
- }
- void CHyperLinker::SetAttrbute(CString url, COLORREF InitColor, COLORREF VisitedColor,
- COLORREF CoverColor, BOOL bUnderLine)
- {
- m_sURL = url;
- m_InitColor = InitColor;
- m_VisitedColor = VisitedColor;
- m_CoverColor = CoverColor;
- m_bUnderLine = bUnderLine;
- }
- BOOL CHyperLinker::OpenUsingShellExecute()
- {
- HINSTANCE hRun = ShellExecute(GetParent()->GetSafeHwnd(),
- _T("open"), m_sURL, NULL, NULL, SW_SHOW);
- if((int)hRun <= 32)
- {
- AfxMessageBox(_T("提供的超级链接或者指定的文件无法执行"), MB_OK, 0);
- return FALSE;
- }
- return TRUE;
- }
- void CHyperLinker::PreSubclassWindow()
- {
- // TODO: Add your specialized code here and/or call the base class
- DWORD dwStyle = GetStyle();
- ::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY); // 设置动态窗口属性
- CStatic::PreSubclassWindow();
- }
- void CHyperLinker::SetFont(CFont *font)
- {
- // 设置字体
- memcpy(&m_Font, font, sizeof(CFont));
-
- // 根据字体的长度和宽度设置静态框的长度和宽度
- CDC *pDC = GetDC();
- CFont *pOldfont = pDC->SelectObject(&m_Font);
- CString str;
- GetWindowText(str);
- CSize size = pDC->GetTextExtent(str, str.GetLength());
- SetWindowPos(NULL, 0, 0, size.cx, size.cy, SWP_NOMOVE);
- pDC->SelectObject(pOldfont);
- ReleaseDC(pDC);
- }