本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告

戒除浮躁,读好书,交益友


我的分类(专题)

日志更新

最新评论

留言板

链接

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为该类就好


阅读全文(7228) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.047 second(s), page refreshed 144758734 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号