实现效果:
当前实例通过对CStatic类的扩展,实现文本框带链接,可以设置背景文本颜色等属性。这些属性是通过下面代码实现
COLORREF bk = RGB(192, 192 ,192);
COLORREF tx = RGB( 0, 96, 142);
m_Static.bkColor( bk );
m_Static.textColor( tx );
m_Static.doCustomCursor();// Show a custom cursor
m_Static.onClickDoShellCommand("www.gkbc8.com");
效果如下图:
上位机MFC实现带链接效果的文本
实现过程
1.新建一对话框工程,添加静态文本控制,如上图,将例程根目录三文件STATICEX.CPP,STATICEX.H,LOGFONT.H复制使用,
或复制帖子尾部的代码,再导入文件到工程。
2.准备好后,可以发再自己工程多出几个集成类,如CStaticEx。
然后是这个类的使用,在主对话框中包含类头文件,添加其实例
#include "StaticEx.h"
CStaticEx m_Static;
最后是添加实例的初始化代码
COLORREF bk = RGB(192, 192 ,192);
COLORREF tx = RGB( 0, 96, 142);
m_Static.bkColor( bk );
m_Static.textColor( tx );
m_Static.doCustomCursor();// Show a custom cursor
m_Static.onClickDoShellCommand("www.gkbc8.com");// Have the ctrl respond to button clicks
这样就可以编译了,编译时提示缺少一个鼠标图像资源IDC_CURSOR_HAND,可以自己添加,或复制使用。
例程中还设置了对话框背景与静态文本控件背景保持一致
主要是准备了一画刷CBrush m_bkBrush;m_bkBrush.CreateSolidBrush(bk); 来填充对话框背景
HBRUSH CCTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor==CTLCOLOR_DLG)
return m_bkBrush;
return hbr;
}
下面是文件源代码,可以复制使用或直接下载例程:
- // Description:
- //
- // Extended CStatic class. Allows easy customization of the following:
- //
- // 1. COLORREF CStaticEx::bkColor( COLORREF crColor )
- // - Sets back ground color of the control
- //
- // 2. COLORREF CStaticEx::textColor( COLORREF crColor )
- // - Sets text or foreground color of the control
- //
- // 3. COLORREF bkColor() const
- // - Returns back ground color of control
- //
- // 4. COLORREF textColor() const
- // - Returns text (or foreground) color of control
- //
- // 5. void onClickDoShellCommand( const CString& linkName )
- //
- // (Note: the idea for this part of the control
- // came fom Paul DiLascia in the December issue
- // of MSJ )
- //
- // - Use control as a link to open a link (this may
- // be an exe if you like...
- //
- // COLORREF setLinkColorVisited( COLORREF crColor )
- // - Sets the visit color, returns previous setting
- //
- // COLORREF setLinkColorUnvisited( COLORREF crColor )
- // - Sets the unvisited color, returns previous setting
- //
- //
- //
- #if !defined(AFX_STATICEX_H__CC617FC0_4FE2_11D1_9E2E_40E806C10000__INCLUDED_)
- #define AFX_STATICEX_H__CC617FC0_4FE2_11D1_9E2E_40E806C10000__INCLUDED_
- #if _MSC_VER >= 1000
- #pragma once
- #endif // _MSC_VER >= 1000
- typedef void (*CWND_INIT_FUNCTION)();
- /////////////////////////////////////////////////////////////////////////////
- // CStaticEx window
- class CStaticEx : public CStatic
- {
- // Construction
- public:
- CStaticEx();
- // Copy Semantics
- // Block copy construction...
- //
- // No m_hWnd defined for object.
- //
- private:
- CStaticEx( const CStaticEx& );
- public:
- // Allow basics to be copied...
- CStaticEx& operator = ( const CStaticEx& );
- public:
- DECLARE_DYNCREATE( CStaticEx )
- // Attributes
- // Turn off and on showing off the default cursor
- //
- void doCustomCursor( bool custom = true )
- {
- m_doCustomCursor = custom;
- }
- // Font control
- //
- void setFont( const LOGFONT* lpLogFont );
- void setFont( LONG fontHeight = -8,
- LONG fontWeight = FW_NORMAL,
- UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
- LPCSTR faceName = _T("MS Sans Serif" ) );
-
- void onClickDoShellCommand( const CString& linkAddress = "mailto:bberry@javanet.com" );
- // This method will send a windows message if onClickSendMessage() has
- // defined a message to send
- //
- void onClickShowModalDialog( CDialog* dlg = 0 )
- { m_dialog = dlg;
- }
- // On a windows click event show this window object
- //
- void onClickShowWindow( CWnd* cwnd = 0 )
- {
- ASSERT( cwnd && ::IsWindow(cwnd->m_hWnd) );
- m_showThisCwnd = cwnd;
- }
- // Send a windows message when a OnClick event occurs
- //
- void onClickSendMessage( CWnd* cwnd = 0, UINT wm_message = 0, WPARAM flags = 0 )
- {
- ASSERT( cwnd&& ::IsWindow(cwnd->m_hWnd) );
- m_sendMsgToThisCwnd = cwnd;
- m_msg_flags = flags;
- m_wm_message = wm_message;
- }
- // Turn click events on or off
- //
- void doOnClickEvents( bool doEvents = true )
- {
- DWORD dwStyle = GetStyle();
- if ( doEvents )
- {
- if ( !(dwStyle & SS_NOTIFY) )
- ::SetWindowLong( m_hWnd, GWL_STYLE, dwStyle | SS_NOTIFY );
- }
- else
- {
- if ( (dwStyle & SS_NOTIFY) ) {
- dwStyle ^= SS_NOTIFY;
- ::SetWindowLong( m_hWnd, GWL_STYLE, dwStyle );
- }
- }
- }
- // Control coloring of the static object
- //
- COLORREF bkColor ( COLORREF );
- COLORREF bkColor() const { return m_crBkColor; }
-
- COLORREF textColor( COLORREF );
- COLORREF textColor() const { return m_crTextColor; }
- // Set up shell link colors
- //
- COLORREF setLinkColorVisited( COLORREF );
- COLORREF setLinkColorUnvisited( COLORREF );
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CStaticEx)
- //}}AFX_VIRTUAL
- // Implementation
- virtual ~CStaticEx();
- // Generated message map functions
- protected:
- //{{AFX_MSG(CStaticEx)
- //}}AFX_MSG
- afx_msg void OnClicked();
- afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
- afx_msg BOOL OnSetCursor( CWnd* pWnd, UINT nHitTest, UINT message );
-
- DECLARE_MESSAGE_MAP()
- // Customize your brush
- //
- virtual BOOL CreateBrushType();
- private:
- CWnd* m_showThisCwnd; // a window to show when a click event
- // occurs
- CWnd* m_sendMsgToThisCwnd; // the window to send messages to when
- // a click event occurs
- UINT m_wm_message; // the windows message event
- // initialize the m_showThisCwnd object
- WPARAM m_msg_flags; // data to send on click events
- CDialog* m_dialog; // a dialog to show on show on a click event
- bool m_doCustomCursor; // don't show the hand cursor
- CBrush m_brBkGround; // the back ground brush
- BOOL m_didClickEvent; // use to flag if a click event ever happened
- CString m_shellCommandArg; // The actual link
- COLORREF m_crBkColor;
- COLORREF m_crTextColor;
- COLORREF m_crOnClickColor;
- //
- // COLORREF m_crUnvisited = m_crTextColor;
- //
- CFont* m_pCFont;
- };
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
- #endif // !defined(AFX_STATICEX_H__CC617FC0_4FE2_11D1_9E2E_40E806C10000__INCLUDED_)
复制代码- // Description:
- //
- // Extended CStatic class. Allows easy customization of the following:
- //
- // 1. COLORREF bkColor( COLORREF crColor )
- // - Sets back ground color of the control
- //
- // 2. COLORREF textColor( COLORREF crColor )
- // - Sets text or foreground color of the control
- //
- // 3. COLORREF bkColor() const
- // - Returns back ground color of control
- //
- // 4. COLORREF textColor() const
- // - Returns text (or foreground) color of control
- //
- // 5. void setFont( const LOGFONT* lpLogFont )
- // - Sets the font of the static control.
- //
- // 6. void onClickDoShellCommand( const CString& linkName )
- //
- // (Note: the idea for this part of the control
- // came fom Paul DiLascia in the December issue
- // of MSJ )
- //
- // - Use control as a link to open a link (this may
- // be an exe if you like...
- //
- // 7. COLORREF setLinkColorVisited( COLORREF crColor )
- // - Sets the visit color, returns previous setting
- //
- // 8. COLORREF setLinkColorUnvisited( COLORREF crColor )
- // - Sets the unvisited color, returns previous setting
- //
- // *** Set new font for this control ***
- //
- // 9. void setFont( const LOGFONT* lpLogFont );
- //
- // 10. void setFont( LONG fontHeight = -8,
- // LONG fontWeight = FW_NORMAL,
- // UCHAR pitchAndFamily = DEFAULT_PITCH | FF_DONTCARE,
- // LPCSTR faceName = _T("MS Sans Serif"
- // );
- //
- #include "stdafx.h"
- #include "resource.h"
- #include "LogFont.h"
- #include "StaticEx.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CStaticEx
- IMPLEMENT_DYNCREATE( CStaticEx, CStatic )
- BEGIN_MESSAGE_MAP(CStaticEx, CStatic)
- //{{AFX_MSG_MAP(CStaticEx)
- //}}AFX_MSG_MAP
- ON_WM_SETCURSOR()
- ON_WM_CTLCOLOR_REFLECT()
- ON_CONTROL_REFLECT( STN_CLICKED, OnClicked )
- END_MESSAGE_MAP()
- CStaticEx::CStaticEx()
- {
- // Use system colors for defaults
- //
- m_crTextColor = ::GetSysColor( COLOR_WINDOWTEXT );
- m_crBkColor = ::GetSysColor( COLOR_3DFACE );
- m_crOnClickColor = RGB( 0, 128, 192 );
- // The default brush type; SOLID
- //
- CreateBrushType();
- // Uses default font
- //
- m_pCFont = 0;
- // Link was not visited
- //
- m_didClickEvent = FALSE;
- // Set to popup a dialog when this control is clicked
- //
- m_dialog = 0;
- // Show a new window when this control is clicked
- //
- m_showThisCwnd = 0;
- m_sendMsgToThisCwnd = 0;
- // defines a message that may be sent to m_showThisCwnd
- //
- m_msg_flags = 0;
- m_wm_message = 0;
- // Do use the hand cursor
- //
- m_doCustomCursor = false;
- }
- CStaticEx::~CStaticEx()
- {
- if ( m_pCFont ) delete m_pCFont;
- }
- // Note: Copy construction is blocked for this class.
- // This is because there would be no defined
- // m_hWnd during the construction of the object.
- //
- // CStaticEx::CStaticEx( const CStaticEx& o )
- //
- // Allow = operator to be used for copying basics.
- //
- CStaticEx& CStaticEx::operator = ( const CStaticEx& o )
- {
- _ASSERT( o != *this ); // You probably did not mean to do this...
- if ( o == *this ) return *this; // copying self...
-
- m_shellCommandArg = o.m_shellCommandArg;
- m_crOnClickColor = o.m_crOnClickColor;
- bkColor( o.m_crBkColor );
- textColor( o.m_crTextColor );
- if ( o.m_pCFont ) {
- CLogFont pLogFont;
- o.m_pCFont->GetLogFont( &pLogFont );
- setFont( &pLogFont );
- }
- m_dialog = o.m_dialog;
- m_showThisCwnd = o.m_showThisCwnd;
- m_didClickEvent = o.m_didClickEvent;
- m_sendMsgToThisCwnd = o.m_sendMsgToThisCwnd;
- m_msg_flags = o.m_msg_flags;
- m_wm_message = o.m_wm_message;
- m_doCustomCursor = o.m_doCustomCursor;
- return *this;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CStaticEx message handlers
- void CStaticEx::setFont( const LOGFONT* lpLogFont )
- {
- _ASSERT( lpLogFont ); // logfont is not defined!!!
- if ( !lpLogFont ) return;
- if ( m_pCFont ) delete m_pCFont;
-
- m_pCFont = new CFont;
- m_pCFont->CreateFontIndirect( lpLogFont );
- SetFont( m_pCFont );
- }
- void CStaticEx::setFont( LONG fontHeight /* = -8 */,
- LONG fontWeight /* = FW_NORMAL */,
- UCHAR pitchAndFamily /* = DEFAULT_PITCH | FF_DONTCARE*/,
- LPCSTR faceName /* = _T("MS Sans Serif") */ )
- {
- if ( m_pCFont )
- {
- delete m_pCFont;
- }
- m_pCFont = new CFont;
- const CLogFont lf( fontHeight,
- FW_NORMAL,
- pitchAndFamily,
- faceName
- );
- m_pCFont->CreateFontIndirect( &lf );
- SetFont( m_pCFont );
- }
- COLORREF CStaticEx::bkColor( COLORREF crColor )
- {
- _ASSERT(::IsWindow(m_hWnd));
- COLORREF cr = m_crBkColor;
-
- m_crBkColor = crColor;
-
- m_brBkGround.DeleteObject();
-
- CreateBrushType();
-
- Invalidate();
- return cr;
- }
- COLORREF CStaticEx::textColor( COLORREF crColor )
- {
- _ASSERT(::IsWindow(m_hWnd));
-
- COLORREF cr = m_crTextColor;
-
- m_crTextColor = crColor;
-
- Invalidate();
-
- return cr;
- }
- COLORREF CStaticEx::setLinkColorVisited( COLORREF crColor )
- {
- COLORREF c = m_crOnClickColor;
- m_crOnClickColor = crColor;
- return c;
- }
- COLORREF CStaticEx::setLinkColorUnvisited( COLORREF crColor )
- {
- m_crTextColor = crColor;
- return textColor( crColor );
- }
- BOOL CStaticEx::CreateBrushType()
- {
- return m_brBkGround.CreateSolidBrush( m_crBkColor );
- }
- void CStaticEx::onClickDoShellCommand( const CString& linkAddress /* = "bberry@javanet.com" */ )
- {
- m_shellCommandArg = linkAddress;
- }
- HBRUSH CStaticEx::CtlColor(CDC* pDC, UINT nCtlColor)
- {
- // Setup for receiving mouse input; we only turn it
- // on if it is needed...
- //
- if ( !m_shellCommandArg.IsEmpty() )
- {
- doOnClickEvents();
- pDC->SetTextColor( m_didClickEvent ? m_crOnClickColor : m_crTextColor );
- }
- else
- pDC->SetTextColor( m_crTextColor );
-
- pDC->SetBkColor ( m_crBkColor );
- return (HBRUSH)m_brBkGround;
- }
- void CStaticEx::OnClicked()
- {
- if ( m_showThisCwnd )
- {
- if ( m_showThisCwnd->IsWindowVisible() )
- m_showThisCwnd->ShowWindow( SW_HIDE );
- else
- m_showThisCwnd->ShowWindow( SW_SHOW );
- }
- if ( m_dialog )
- {
- m_dialog->DoModal();
- }
- if ( !m_shellCommandArg.IsEmpty() )
- {
- // Call ShellExecute to run the link...
- //
- HINSTANCE h = ShellExecute( 0, "open", m_shellCommandArg, 0, 0, SW_SHOWNORMAL );
- if ( (UINT)h > 32 ) {
- m_didClickEvent = TRUE;
- Invalidate();
- }
- else {
- MessageBeep(0);
- AfxMessageBox( "Unable to open " + m_shellCommandArg + '.', MB_OK | MB_ICONINFORMATION );
- }
- }
- if ( m_wm_message )
- {
- ::SendMessage( m_sendMsgToThisCwnd->m_hWnd, m_wm_message, m_msg_flags, 0 );
- }
- }
- BOOL CStaticEx::OnSetCursor( CWnd* pWnd, UINT nHitTest, UINT message )
- {
- HCURSOR hCursor;
- if ( m_doCustomCursor )
- {
- hCursor = AfxGetApp()->LoadCursor( IDC_CURSOR_HAND );
- }
- else
- {
- hCursor = AfxGetApp()->LoadStandardCursor( IDC_ARROW );
- }
- _ASSERT( hCursor );
- ::SetCursor( hCursor );
- return TRUE;
- }
复制代码
|