ATL写的一个超链接类

来源:互联网 发布:淘宝网注册网店步骤 编辑:程序博客网 时间:2024/05/01 04:45

 

temp

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
      MESSAGE_HANDLER( WM_SETCURSOR, OnSetCursor)
      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 OnSetCursor( UINT, WPARAM, LPARAM, BOOL
& ) {
       SetCursor(LoadCursor(
0, MAKEINTRESOURCE(32649)));
       
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