ATL写的一个超链接类

 

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值