声明:转载 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvc60/html/atlwindow.aspThe following example defines CStaticLink, a static control that, when clicked, opens the Web page that it names. All messages from a CStaticLink control are reflected back to it by the parent window (in this case, a dialog box—see ATL Dialog Box Classes). In addition to handling reflected WM_COMMAND messages, CStaticLink handles reflected WM_CTLCOLORSTATIC messages so that it can color itself differently, depending on whether it has been clicked or not.#include "stdafx.h" #include "resource.h"CComModule _Module;class CStaticLink : public CWindowImpl<CStaticLink> { /* Based on CStaticLink by Paul DiLascia, C++ Q&A, Microsoft Systems Journal 12/1997. Turns static controls into clickable "links" -- when the control is clicked, the file/program/webpage named in the control's text (or set by SetLinkText()) is opened via ShellExecute(). Static control can be either text or graphic (bitmap, icon, etc.). */ public: DECLARE_WND_SUPERCLASS( _T("StaticLink"), _T("Static") ) CStaticLink() : m_colorUnvisited( RGB(0,0,255) ), m_colorVisited( RGB(128,0,128) ), m_bVisited( FALSE ), m_hFont( NULL ) { } void SetLinkText( LPCTSTR szLink ) { USES_CONVERSION; m_bstrLink = T2OLE( szLink ); } BEGIN_MSG_MAP(CStaticLink) // uses message reflection: WM_* comes back as OCM_* MESSAGE_HANDLER( OCM_COMMAND, OnCommand ) MESSAGE_HANDLER( OCM_CTLCOLORSTATIC, OnCtlColor ) MESSAGE_HANDLER( WM_DESTROY, OnDestroy ) // not a reflected message DEFAULT_REFLECTION_HANDLER() END_MSG_MAP() LRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL& ) { if( m_hFont ) DeleteObject( m_hFont ); return 0; } LRESULT OnCommand( UINT, WPARAM wParam, LPARAM, BOOL& ) { USES_CONVERSION; int code = HIWORD( wParam ); if( code == STN_CLICKED || code == STN_DBLCLK ){ if( m_bstrLink.Length() == 0 ){ GetWindowText( &m_bstrLink ); } if( (int)ShellExecute( *this, _T("open"), OLE2T(m_bstrLink), NULL, NULL, SW_SHOWNORMAL ) > 32 ){ m_bVisited = TRUE; // return codes > 32 => success Invalidate(); }else{ MessageBeep( 0 ); ATLTRACE( _T("Error: CStaticLink couldn't open file") ); } } return 0; } LRESULT OnCtlColor( UINT, WPARAM wParam, LPARAM, BOOL& ) { // notify bit must be set to get STN_* notifications ModifyStyle( 0, SS_NOTIFY ); HBRUSH hBr = NULL; if( (GetStyle() & 0xff) <= SS_RIGHT ){ // it's a text control: set up font and colors if( !m_hFont ){ LOGFONT lf; GetObject( GetFont(), sizeof(lf), &lf ); lf.lfUnderline = TRUE; m_hFont = CreateFontIndirect( &lf ); } HDC hDC = (HDC)wParam; SelectObject( hDC, m_hFont ); SetTextColor( hDC, m_bVisited ? m_colorVisited : m_colorUnvisited ); SetBkMode( hDC, TRANSPARENT ); hBr = (HBRUSH)GetStockObject( HOLLOW_BRUSH ); } return (LRESULT)hBr; }private: COLORREF m_colorUnvisited; COLORREF m_colorVisited; BOOL m_bVisited; HFONT m_hFont; CComBSTR m_bstrLink; }; // CStaticLink
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvc60/html/atlwindow.aspThe following example defines CStaticLink, a static control that, when clicked, opens the Web page that it names. All messages from a CStaticLink control are reflected back to it by the parent window (in this case, a dialog box—see ATL Dialog Box Classes). In addition to handling reflected WM_COMMAND messages, CStaticLink handles reflected WM_CTLCOLORSTATIC messages so that it can color itself differently, depending on whether it has been clicked or not.#include "stdafx.h"
#include "resource.h"CComModule _Module;class CStaticLink : public CWindowImpl<CStaticLink> {
/*
Based on CStaticLink by Paul DiLascia, C++ Q&A, Microsoft Systems
Journal 12/1997.
Turns static controls into clickable "links" -- when the control is
clicked, the file/program/webpage named in the control's text (or
set by SetLinkText()) is opened via ShellExecute(). Static control
can be either text or graphic (bitmap, icon, etc.).
*/
public:
DECLARE_WND_SUPERCLASS( _T("StaticLink"), _T("Static") ) CStaticLink() :
m_colorUnvisited( RGB(0,0,255) ),
m_colorVisited( RGB(128,0,128) ),
m_bVisited( FALSE ),
m_hFont( NULL )
{
} void SetLinkText( LPCTSTR szLink ) {
USES_CONVERSION;
m_bstrLink = T2OLE( szLink );
} BEGIN_MSG_MAP(CStaticLink)
// uses message reflection: WM_* comes back as OCM_*
MESSAGE_HANDLER( OCM_COMMAND, OnCommand )
MESSAGE_HANDLER( OCM_CTLCOLORSTATIC, OnCtlColor )
MESSAGE_HANDLER( WM_DESTROY, OnDestroy ) // not a reflected message
DEFAULT_REFLECTION_HANDLER()
END_MSG_MAP() LRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL& ) {
if( m_hFont ) DeleteObject( m_hFont );
return 0;
} LRESULT OnCommand( UINT, WPARAM wParam, LPARAM, BOOL& ) {
USES_CONVERSION;
int code = HIWORD( wParam );
if( code == STN_CLICKED || code == STN_DBLCLK ){
if( m_bstrLink.Length() == 0 ){
GetWindowText( &m_bstrLink );
}
if( (int)ShellExecute( *this, _T("open"),
OLE2T(m_bstrLink), NULL, NULL, SW_SHOWNORMAL ) > 32 ){
m_bVisited = TRUE; // return codes > 32 => success
Invalidate();
}else{
MessageBeep( 0 );
ATLTRACE( _T("Error: CStaticLink couldn't open file") );
}
}
return 0;
} LRESULT OnCtlColor( UINT, WPARAM wParam, LPARAM, BOOL& ) {
// notify bit must be set to get STN_* notifications
ModifyStyle( 0, SS_NOTIFY );
HBRUSH hBr = NULL;
if( (GetStyle() & 0xff) <= SS_RIGHT ){
// it's a text control: set up font and colors
if( !m_hFont ){
LOGFONT lf;
GetObject( GetFont(), sizeof(lf), &lf );
lf.lfUnderline = TRUE;
m_hFont = CreateFontIndirect( &lf );
}
HDC hDC = (HDC)wParam;
SelectObject( hDC, m_hFont );
SetTextColor( hDC, m_bVisited ? m_colorVisited
: m_colorUnvisited );
SetBkMode( hDC, TRANSPARENT );
hBr = (HBRUSH)GetStockObject( HOLLOW_BRUSH );
}
return (LRESULT)hBr;
}private:
COLORREF m_colorUnvisited;
COLORREF m_colorVisited;
BOOL m_bVisited;
HFONT m_hFont;
CComBSTR m_bstrLink;
}; // CStaticLink