⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bcgurllinkbutton.cpp

📁 一个完整的编辑器的代码(很值得参考
💻 CPP
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This source code is a part of BCGControlBar library.
// You may use, compile or redistribute it as part of your application 
// for free. You cannot redistribute it as a part of a software development 
// library without the agreement of the author. If the sources are 
// distributed along with the application, you should leave the original 
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES on your own risk.
// 
// Stas Levin <stas@iet.co.il>
//*******************************************************************************

// BCGURLLinkButton.cpp : implementation file
//

#include "stdafx.h"
#include "bcgcontrolbar.h"
#include "BCGURLLinkButton.h"
#include "globals.h"
#include "bcglocalres.h"
#include "bcgbarres.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBCGURLLinkButton

CBCGURLLinkButton::CBCGURLLinkButton()
{
	m_bHover = FALSE;
	m_bPressed = FALSE;

	//---------------------------
	// Initialize underline font:
	//---------------------------
	CFont font;
	font.CreateStockObject (DEFAULT_GUI_FONT);

	LOGFONT lf;
	font.GetLogFont (&lf);
	lf.lfUnderline = TRUE;

	m_fontDefault.CreateFontIndirect (&lf);

	//-------------------
	// Initialize colors:
	//-------------------
	m_clrRegular = ::GetSysColor (COLOR_HIGHLIGHT);

#ifdef COLOR_HOTLIGHT

	OSVERSIONINFO osvi;
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	::GetVersionEx (&osvi);
	if (osvi.dwMajorVersion >= 5)
	{
		m_clrHover = ::GetSysColor (COLOR_HOTLIGHT);
	}
	else
#endif
	{
		m_clrHover = RGB (255, 0, 0);	// Light red
	}
}

CBCGURLLinkButton::~CBCGURLLinkButton()
{
}

BEGIN_MESSAGE_MAP(CBCGURLLinkButton, CButton)
	//{{AFX_MSG_MAP(CBCGURLLinkButton)
	ON_WM_SETCURSOR()
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_CANCELMODE()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBCGURLLinkButton message handlers

void CBCGURLLinkButton::DrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	ASSERT (lpDIS != NULL);

	CDC* pDC = CDC::FromHandle (lpDIS->hDC);
	ASSERT_VALID (pDC);

	CRect rect = lpDIS->rcItem;

	// Set font:
	CFont* pOldFont = pDC->SelectObject (&m_fontDefault);
	ASSERT (pOldFont != NULL);

	// Set text parameters:
	pDC->SetTextColor (m_bHover ? m_clrHover : m_clrRegular);
	pDC->SetBkMode (TRANSPARENT);

	// Obtain label:
	CString strLabel;
	GetWindowText (strLabel);

	pDC->DrawText (strLabel, rect, DT_SINGLELINE);

	pDC->SelectObject (pOldFont);
}
//******************************************************************************************
BOOL CBCGURLLinkButton::OnSetCursor(CWnd* /*pWnd*/, UINT /*nHitTest*/, UINT /*message*/) 
{
	if (globalData.m_hcurHand == NULL)
	{
		CBCGLocalResource locaRes;
		globalData.m_hcurHand = AfxGetApp ()->LoadCursor (IDC_BCGBARRES_HAND);
	}

	::SetCursor (globalData.m_hcurHand);
	return TRUE;
}
//******************************************************************************************
void CBCGURLLinkButton::OnClicked() 
{
	CWaitCursor wait;

	CString strURL = m_strURL;
	if (strURL.IsEmpty ())
	{
		GetWindowText (strURL);
	}

	if (::ShellExecute (NULL, NULL, m_strPrefix + strURL, NULL, NULL, NULL) < (HINSTANCE) 32)
	{
		TRACE(_T("Can't open URL: %s\n"), strURL);
	}

	Release ();
}
//******************************************************************************************
void CBCGURLLinkButton::OnCancelMode() 
{
	CButton::OnCancelMode();

	m_bPressed = FALSE;
	Release ();
}
//******************************************************************************************
void CBCGURLLinkButton::OnMouseMove(UINT nFlags, CPoint point) 
{
	if ((nFlags & MK_LBUTTON) == 0 || m_bPressed)
	{
		CRect rectClient;
		GetClientRect (rectClient);

		if (rectClient.PtInRect (point))
		{
			if (!m_bHover)
			{
				m_bHover = TRUE;
				SetCapture ();

				Invalidate ();
				UpdateWindow ();
			}
		}
		else
		{
			if (m_bHover)
			{
				Release ();
			}
		}
	}
	
	CButton::OnMouseMove(nFlags, point);
}
//*******************************************************************************************
void CBCGURLLinkButton::SetURL (LPCTSTR lpszURL)
{
	if (lpszURL == NULL)
	{
		m_strURL.Empty ();
	}
	else
	{
		m_strURL = lpszURL;
	}
}
//*******************************************************************************************
void CBCGURLLinkButton::SetTooltip (LPCTSTR lpszToolTip)
{
	ASSERT (GetSafeHwnd () != NULL);

	if (m_wndToolTip.GetSafeHwnd () == NULL)
	{
		m_wndToolTip.Create (this, TTS_ALWAYSTIP);
	}
	else
	{
		m_wndToolTip.DelTool (this);
	}

	if (lpszToolTip != NULL)
	{
		m_wndToolTip.AddTool (this, lpszToolTip);
	}
}
//*******************************************************************************************
BOOL CBCGURLLinkButton::PreTranslateMessage(MSG* pMsg) 
{
	if (m_wndToolTip.GetSafeHwnd () != NULL)
	{
		m_wndToolTip.RelayEvent (pMsg);
	}
	
	return CButton::PreTranslateMessage(pMsg);
}
//*******************************************************************************************
void CBCGURLLinkButton::Release ()
{
	m_bHover = FALSE;
	::ReleaseCapture ();

	Invalidate ();
	UpdateWindow ();
}
//*******************************************************************************************
void CBCGURLLinkButton::SetURLPrefix (LPCTSTR lpszPrefix)
{
	ASSERT (lpszPrefix != NULL);
	m_strPrefix = lpszPrefix;
}
//*******************************************************************************************
void CBCGURLLinkButton::SizeToContent (BOOL bVCenter, BOOL bHCenter)
{
	ASSERT_VALID (this);
	ASSERT (GetSafeHwnd () != NULL);

	CClientDC dc (this);

	// Set font:
	CFont* pOldFont = dc.SelectObject (&m_fontDefault);
	ASSERT (pOldFont != NULL);

	// Obtain label:
	CString strLabel;
	GetWindowText (strLabel);

	CRect rectClient;
	GetClientRect (rectClient);

	CRect rectText = rectClient;
	dc.DrawText (strLabel, rectText, DT_SINGLELINE | DT_CALCRECT);

	if (bVCenter || bHCenter)
	{
		ASSERT (GetParent ()->GetSafeHwnd () != NULL);
		MapWindowPoints (GetParent (), rectClient);

		int dx = bHCenter ? (rectClient.Width () - rectText.Width ()) / 2 : 0;
		int dy = bVCenter ? (rectClient.Height () - rectText.Height ()) / 2 : 0;

		SetWindowPos (NULL, rectClient.left + dx, rectClient.top + dy, 
			rectText.Width (), rectText.Height (),
			SWP_NOZORDER | SWP_NOACTIVATE);
	}
	else
	{
		SetWindowPos (NULL, -1, -1, rectText.Width (), rectText.Height (),
			SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
	}

	dc.SelectObject (pOldFont);
}
//*******************************************************************************************
void CBCGURLLinkButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_bPressed = TRUE;
	CButton::OnLButtonDown(nFlags, point);
}
//*******************************************************************************************
void CBCGURLLinkButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bPressed = FALSE;
	CButton::OnLButtonUp(nFlags, point);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -