例如:IDC_DBBUTTON1 = “This is 肖天鹏的第一自制按钮\n天鹏",
其中字符串“This is肖天鹏的第一自制按钮“将在鼠标移到控件上时显示在状态条上,字符串“天鹏"将作为 ToolTip 显示。
三、建立消息映射
在对话框的头文件 (*.H) 中
加入以下代码:
protected: void SetStatusText(UINT nID=0); //{{AFX_MSG(CFileOp1) afx_msg void OnDestroy(); afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message); //}}AFX_MSG afx_msg BOOL OnTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult ); DECLARE_MESSAGE_MAP() 在对话框的实现文件 (*.CPP) 中加入以下代码: BEGIN_MESSAGE_MAP(CFileOp1, CDialog) //{{AFX_MSG_MAP(CFileOp1) ON_WM_DESTROY() ON_WM_SETCURSOR() //}}AFX_MSG_MAP ON_NOTIFY_EX(TTN_NEEDTEXT,0,OnTipNotvify) END_MESSAGE_MAP() |
四、编辑消息处理函数
BOOL CFileOp1::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) {// TODO: Add your message handler code here and/or call default if(pWnd==this) SetStatusText(); else {TOOLTIPTEXT m_psttt; m_psttt.hdr.hwndFrom=m_hWnd; m_psttt.hdr.idFrom=pWnd->GetDlgCtrlID(); m_psttt.hdr.code=TTN_NEEDTEXT; m_psttt.uFlags= TTF_IDISHWND; SetStatusText(pWnd->GetDlgCtrlID()); this->SendMessage(WM_NOTIFY,m_psttt.hdr.idFrom,(LPARAM)&m_psttt); } return CDialog::OnSetCursor(pWnd, nHitTest, message);} void CFileOp1::OnDestroy() {SetStatusText(); CDialog::OnDestroy();} void CFileOp1::SetStatusText(UINT nID) {if(nID==0) nID=AFX_IDS_IDLEMESSAGE; CWnd *pWnd=AfxGetMainWnd()->GetDescendantWindow (AFX_IDW_STATUS_BAR); if(pWnd) {AfxGetMainWnd()->SendMessage(WM_SETMESS AGESTRING ,nID); pWnd->SendMessage(WM_IDLEUPDATECMDUI); pWnd->UpdateWindow();}} BOOL CFileOp1::OnTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult ) { TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR; UINT nID =pNMHDR->idFrom; if (pTTT->uFlags & TTF_IDISHWND) { nID = ::GetDlgCtrlID((HWND)nID); if (nID) { TCHAR szFullText[256]; CString StrTipText; AfxLoadString(nID,szFullText); AfxExtractSubString(StrTipText,szFullText,1,′\n′); if(!StrTipText.IsEmpty()) strcpy(pTTT->lpszText,StrTipText); pTTT->hinst = AfxGetResourceHandle(); return(TRUE); } } return(FALSE);} |