用MFC实现静态文本超链接的方法

来源:互联网 发布:打印机无法网络打印 编辑:程序博客网 时间:2024/05/17 21:35

原文出处:http://blog.csdn.net/cddchina/article/details/8073345


1、首先导入鼠标图标

可以在C:\Windows\Cursors文件里随便拷贝一个.cur格式的图标放到你的工程文件rc里,然后在资源视图-》添加资源-》选择Cursor -》导入你刚才复制的文件,几下ID;


  1. (假定静态文本ID为ID_STATIC)  
  2.   
  3. 首先,设置鼠标的形状及响应鼠标点击  
  4.   
  5. 第一步,在Dlg类中定义一个protect成员变量HCURSOR  m_hCursor,  
  6.   
  7. 在构造函数里(或者在OnInitDialog()里)调用语句  
  8.   
  9. m_hCursor=AfxGetApp()->LoadCursor(IDI_CURLINK);加载鼠标;  
  10.   
  11. 第二步,在Dlg类中定义一个protect成员变量RECT  m_StaticRect,表示静态文本的坐标,在成员函数OnInitDialog()里调用语句  
  12.   
  13. GetDlgItem(IDC_STATIC)->GetWindowRect(&m_StaticRect);  
  14.   
  15. ScreenToClient(&m_StaticRect);  
  16.   
  17. 第三步,加载鼠标移动消息,在OnMouseMove()里调用语句  
  18.   
  19.   
  20. if( point.x > m_StaticRect.left && point.x < m_StaticRect.right &&  
  21.     point.y > m_StaticRect.top  && point.y < m_StaticRect.bottom )   
  22.     {   
  23.        SetCursor(m_hCursor);   
  24.     }  
  25. 第四步,加载鼠标单击消息,在OnLButtonDown()里添加语句  
  26.   
  27.   
  28. if( point.x > m_StaticRect.left && point.x < m_StaticRect.right &&point.y > m_StaticRect.top  && point.y < m_StaticRect.bottom )  
  29. {  
  30. SetCursor(m_hCursor);  
  31. ShellExecute(NULL,"open",TEXT("http://www.tlu.edu.cn"),NULL,NULL, SW_SHOWNORMAL);  
  32. }  
  33. 注意,若是邮箱,则改为ShellExecute(NULL, "open",_T("mailto:apeng332@sohu.com"), NULL, NULL, SW_SHOWNORMAL);  
  34.   
  35. 在鼠标单击抬起的时候最好也加上SetCursor(m_hCursor);使得鼠标的移动更流畅,鼠标右键消息也可以加上SetCursor(m_hCursor)。  
  36.   
  37. 其次,设置静态文本的字体与颜色  
  38.   
  39. 第一步,字体的设置:在Dlg类中定义一个protect成员变量CFont m_Font表示字体,在类的成员函数OnInitDialog()里调用m_Font.CreateFont(……),注意参数很多,参考MSDN。接着编写两行代码:  
  40.   
  41. CStatic *m_static=(CStatic *)GetDlgItem(IDC_STATIC);  
  42.   
  43. m_static->SetFont(&m_Font,false);  
  44.   
  45. 第二步,颜色的设置:在Dlg中加载WM_CTLCOLOR消息,在此消息的响应函数OnCtlColor(……)里添加如下代码:  
  46.   
  47.   
  48. HBRUSH CScreenShotsDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  49. {  
  50.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  51.     if (pWnd->GetDlgCtrlID() == IDC_STATIC)  
  52.      {  
  53.        pDC->SetTextColor(RGB(255, 0, 0));  
  54.      }  
  55.     return hbr;  
  56. }  
  57. 即可  

0 0