利用 VC++ 的 AppWizard,可以很容易地实现工具条和菜单项的 ToolTip 或在状态条上显 示帮助信息,但要在对话框的控件上显示 ToolTip 并在状态条上显示控件信息并不是那么容易, 其实,利用 VC++ 中的 WM_SETCURSOR 与 TTN_NEEDTEXT 消息就可达到目的。具体操作如下:
一 利用 VC++ 的 MFC AppWizard 生成一个 SDI 或 MDI 的应用程序
二 编辑对话框控件的字符串资源
例如:
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,OnTipNotify) 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()- >SendMessag
[1] [2] [3] 下一页
|