超链接静态控件(部分代码)

来源:互联网 发布:淘宝托管 编辑:程序博客网 时间:2024/06/08 10:33

1.成员函数实现代码

CLinkStatic::CLinkStatic()
{
 m_bLink=TRUE;                                                                //设置超链接为真
 m_bmouseover=FALSE;                                                 
 m_hCurHand=AfxGetApp()->LoadCursor(IDC_HANDCUR);      //载入手型光标
 m_crText=GetSysColor(COLOR_WINDOWTEXT);                     //获取默认颜色(系统颜色)
 ::GetObject((HFONT)GetStockObject(DEFAULT_GUI_FONT),sizeof(m_lf),&m_lf);  //获取默认字体(系统字体)
 m_font.CreateFontIndirect(&m_lf);                                                                 //设置文本字体
    m_hBrush=::CreateSolidBrush(GetSysColor(COLOR_3DFACE));                   //设置画刷
}


 

void CLinkStatic::SetTextColor(COLORREF crText)
{
 m_crText=crText;                                                                  //设置字体颜色
 RedrawWindow();
}

 


 

void CLinkStatic::SetFontUnderline(BOOL bSet)                              //设置是否有下划线
{
 m_lf.lfUnderline=bSet;
 m_font.DeleteObject();
 m_font.CreateFontIndirect(&m_lf);
 RedrawWindow();
}

 


 

void CLinkStatic::SetLinkUrl(CString url)                                //设置超链接地址
{
 m_linkurl=url;
}

 


 

void CLinkStatic::OnMouseMove(UINT nFlags, CPoint point)
{
 SetTimer(1,10,NULL);                                                                  //设置计时器
 CStatic::OnMouseMove(nFlags, point);
}

 


 

void CLinkStatic::OnTimer(UINT nIDEvent)
{
 // TODO: Add your message handler code here and/or call default             //判断光标是否在控件上
 POINT pt;
 CRect rc;
 GetCursorPos(&pt);
 GetWindowRect(&rc);
 if(!rc.PtInRect(pt))
 {
  m_bmouseover=FALSE;
  KillTimer(1);
 }
 else
 {
  m_bmouseover=TRUE;
 }
 if(!m_bmouseover)
  SetFontUnderline(FALSE);
 CStatic::OnTimer(nIDEvent);
}

 


 

BOOL CLinkStatic::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)     //设置光标
{
 if(m_bmouseover)
 {
  ::SetCursor(m_hCurHand);
  SetFontUnderline(true);
  return true;
 }
 return CStatic::OnSetCursor(pWnd, nHitTest, message);
}

 


 

void CLinkStatic::OnLButtonDown(UINT nFlags, CPoint point)                                 //单击启动超链接
{
 // TODO: Add your message handler code here and/or call default
 ShellExecute(NULL,"open",m_linkurl,NULL,NULL,SW_SHOWNORMAL);
 CStatic::OnLButtonDown(nFlags, point);
}

 


 

HBRUSH CLinkStatic::CtlColor(CDC* pDC, UINT nCtlColor)                                       //设置显示样式
{
 if(CTLCOLOR_STATIC==nCtlColor)
 {
  pDC->SelectObject(&m_font);
  pDC->SetTextColor(m_crText);
  pDC->SetBkMode(TRANSPARENT);                     //没用????
 }
 return m_hBrush;
}

 


 

void CLinkStatic::SetLink(BOOL bLink)                                                                    //是否启动超链接
{
 m_bLink=bLink;
 if(bLink)
  ModifyStyle(0,SS_NOTIFY);
 else
  ModifyStyle(SS_NOTIFY,0);
}

原创粉丝点击