« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9704947 建立时间:2004年12月20日 |

| |
[c++]在CStatic中显示ToolTipText 原创空间
邢红瑞 发表于 2004/12/28 17:39:01 |
#if !defined(AFX_STATICLINK_H__E0A72AD3_528A_11D4_AB89_0090270D3A7F__INCLUDED_)#define AFX_STATICLINK_H__E0A72AD3_528A_11D4_AB89_0090270D3A7F__INCLUDED_
#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// StaticLink.h : header file//
/////////////////////////////////////////////////////////////////////////////// CStaticLink window
class CStaticLink : public CStatic{// Constructionpublic:CStaticLink();
// Attributespublic:void SetLinkCursor(HCURSOR hCursor) { m_hLinkCursor = hCursor; }void SetLink(LPCTSTR lpszLink);protected:// you can change these any time: COLORREFm_colorUnvisited; // color for unvisited COLORREFm_colorVisited; // color for visited BOOLm_bVisited; // whether visited or notHCURSORm_hLinkCursor;CToolTipCtrlm_tooltip;
// URL/filename for non-text controls (e.g., icon, bitmap) or when link is // different from window text. If you don't set this, CStaticIcon will // use GetWindowText to get the link. CStringm_strLink; CFontm_font; // underline font for text control
// Operationspublic:
// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CStaticLink)public:virtual BOOL PreTranslateMessage(MSG* pMsg);protected:virtual void PreSubclassWindow();//}}AFX_VIRTUAL
// Implementationpublic:virtual ~CStaticLink();
// Generated message map functionsprotected://{{AFX_MSG(CStaticLink)afx_msg void OnClicked();afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);//}}AFX_MSG
DECLARE_MESSAGE_MAP()};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STATICLINK_H__E0A72AD3_528A_11D4_AB89_0090270D3A7F__INCLUDED_)// StaticLink.cpp : implementation file//
#include "stdafx.h"#include "StaticLink.h"
#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif
/////////////////////////////////////////////////////////////////////////////// CStaticLink
CStaticLink::CStaticLink(){ m_colorUnvisited = RGB(0,0,255); // blue m_colorVisited = RGB(128,0,128); // purple m_bVisited = FALSE; // not visited yetm_hLinkCursor = ::LoadCursor(NULL,MAKEINTRESOURCE(32649));m_strLink = _T("mailto:ljw_jellyfish@126.com");}
CStaticLink::~CStaticLink(){}
BEGIN_MESSAGE_MAP(CStaticLink, CStatic)//{{AFX_MSG_MAP(CStaticLink)ON_CONTROL_REFLECT(STN_CLICKED, OnClicked)ON_WM_CTLCOLOR_REFLECT()ON_WM_SETCURSOR()//}}AFX_MSG_MAPEND_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////// CStaticLink message handlers
void CStaticLink::OnClicked() {CWaitCursor wait;
if (m_strLink.IsEmpty()) // if URL/filename not set.. GetWindowText(m_strLink); // ..get it from window text
// Call ShellExecute to run the file. // For an URL, this means opening it in the browser. // HINSTANCE h = ShellExecute(NULL, "open", m_strLink, NULL, NULL, SW_SHOWNORMAL); if ((UINT)h > 32){ m_bVisited = TRUE; // (not really--might not have found link) Invalidate(); // repaint to show visited color } else { MessageBeep(0); // unable to execute file! TRACE(_T("*** WARNING: CStaticLink: unable to execute file %s\n"),(LPCTSTR)m_strLink); }
}
HBRUSH CStaticLink::CtlColor(CDC* pDC, UINT nCtlColor){ ASSERT(nCtlColor == CTLCOLOR_STATIC); DWORD dwStyle = GetStyle(); if (!(dwStyle & SS_NOTIFY)){ // Turn on notify flag to get mouse messages and STN_CLICKED. // Otherwise, I'll never get any mouse clicks! ::SetWindowLong(m_hWnd, GWL_STYLE, dwStyle | SS_NOTIFY); } HBRUSH hbr = NULL; if ((dwStyle & 0xFF) <= SS_RIGHT) {
// this is a text control: set up font and colors if (!(HFONT)m_font) { // first time init: create font LOGFONT lf; GetFont()->GetObject(sizeof(lf), &lf); lf.lfUnderline = TRUE; m_font.CreateFontIndirect(&lf); }
// use underline font and visited/unvisited colors pDC->SelectObject(&m_font); pDC->SetTextColor(m_bVisited ? m_colorVisited : m_colorUnvisited); pDC->SetBkMode(TRANSPARENT);
// return hollow brush to preserve parent background color hbr = (HBRUSH)::GetStockObject(HOLLOW_BRUSH); } return hbr;
UNREFERENCED_PARAMETER(nCtlColor);}
BOOL CStaticLink::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) {// If a cursor was specified then use it!if (m_hLinkCursor != NULL){::SetCursor(m_hLinkCursor);return TRUE;}
return CStatic::OnSetCursor(pWnd, nHitTest, message);}
BOOL CStaticLink::PreTranslateMessage(MSG* pMsg) {{// Let the ToolTip process this message.m_tooltip.RelayEvent(pMsg);}
return CStatic::PreTranslateMessage(pMsg);}
void CStaticLink::PreSubclassWindow() {{// Create the ToolTip control.m_tooltip.Create(this);m_tooltip.Activate(TRUE);
// TODO: Use one of the following forms to add controls:m_tooltip.AddTool(this, m_strLink);m_tooltip.SetMaxTipWidth(120);}
CStatic::PreSubclassWindow();}
void CStaticLink::SetLink(LPCTSTR lpszLink){m_strLink = lpszLink;this->m_tooltip.UpdateTipText(lpszLink, this);}
在对话框或CFormView中添加一个Static,然后定义一个该类的成员对象,再子类化或者直接在DDX_xx中添加,然后更改对应的CStatic为该类就好 |
|
|