设置静态文本为超链接

1、 添加成员变量m_RectLink,用来保存文本框的坐标,添加两个静态文本控件,将默认ID改掉:

  1. class CSampleDlg : public CDialog   
  2. {   
  3. // Construction   
  4. public :   
  5.      CSampleDlg(CWnd* pParent = NULL);    // standard constructor   
  6.   
  7.      // Implementation   
  8. protected :   
  9.      HICON m_hIcon;   
  10.        
  11.      //用于保存静态文本框的屏幕坐标   
  12.      RECT m_pRectLink;     
  13.      ......   
  14. }  

 

2、获取static控件的区域,在初始化函数里添加代码如下:

  1. BOOL CSampleDlg::OnInitDialog()   
  2. {   
  3.      CDialog::OnInitDialog();   
  4.   
  5.      // Add "About..." menu item to system menu.   
  6.   
  7.      // IDM_ABOUTBOX must be in the system command range.   
  8.      ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);   
  9.      ASSERT(IDM_ABOUTBOX < 0xF000);   
  10.   
  11.      CMenu* pSysMenu = GetSystemMenu(FALSE);   
  12.      if (pSysMenu != NULL)   
  13.      {   
  14.          CString strAboutMenu;   
  15.          strAboutMenu.LoadString(IDS_ABOUTBOX);   
  16.          if (!strAboutMenu.IsEmpty())   
  17.          {   
  18.              pSysMenu->AppendMenu(MF_SEPARATOR);   
  19.              pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);   
  20.          }   
  21.      }   
  22.   
  23.      // Set the icon for this dialog.   The framework does this automatically   
  24.      //   when the application's main window is not a dialog   
  25.      SetIcon(m_hIcon, TRUE);          // Set big icon   
  26.      SetIcon(m_hIcon, FALSE);         // Set small icon   
  27.        
  28.      // TODO: Add extra initialization here   
  29.   
  30.      //将静态文本的屏幕坐标存放在m_pRectLink中   
  31.      GetDlgItem(IDC_LINK) -> GetWindowRect(&m_pRectLink);   
  32.   
  33.      //将屏幕坐标转换为客户坐标   
  34.      ScreenToClient(&m_pRectLink);   
  35.   
  36.      return TRUE;   // return TRUE   unless you set the focus to a control   
  37. }  

 

3、添加Windows消息WM_MOUSEMOVE,代码如下:

 

  1. void CSampleDlg::OnMouseMove( UINT nFlags, CPoint point)   
  2. {   
  3.      // TODO: Add your message handler code here and/or call default   
  4.      //下面设置鼠标在静态文本区时,将光标设成小手状   
  5.   
  6.      if (point.x > m_pRectLink.left && point.x < m_pRectLink.right && point.y > m_pRectLink.top && point.y < m_pRectLink.bottom )   
  7.      //此处添加判断坐标算法   
  8.      {   
  9.          HCURSOR hCursor;   
  10.          hCursor = AfxGetApp() -> LoadCursor(IDC_HAND);   
  11.   
  12.          //将鼠标设为小手状   
  13.          SetCursor(hCursor);   
  14.      }   
  15.      CDialog::OnMouseMove(nFlags, point);   
  16. }  

 

4、添加Windows消息WM_LBUTTONDOWN,代码如下:

 

  1. void CSampleDlg::OnLButtonDown( UINT nFlags, CPoint point)   
  2. {   
  3.      // TODO: Add your message handler code here and/or call default   
  4.      //此处添加判断坐标算法   
  5.      if (point.x > m_pRectLink.left && point.x < m_pRectLink.right && point.y > m_pRectLink.top && point.y < m_pRectLink.bottom)   
  6.      {   
  7.          //鼠标左键按下   
  8.          if (nFlags == MK_LBUTTON)      
  9.          {   
  10.              //为改善鼠标效果,此处加入以上变换鼠标形状的代码   
  11.              ShellExecute(0, NULL, "http://hi.baidu.com/wangguang246" , NULL,NULL, SW_NORMAL);   
  12.                
  13.              //也可以添加电子邮件的链接   
  14.              ShellExecute(0, NULL, "mailto:koma0769@vip.qq.com" , NULL,NULL, SW_NORMAL);   
  15.          }   
  16.      }   
  17.        
  18.      CDialog::OnLButtonDown(nFlags, point);   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值