VC實現對話方塊上資訊的顯示

來源:互聯網
上載者:User

利用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()- >SendMessage
(WM_SETMESSAGESTRING ,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);
}

五、在 Stdafx.h 檔案中加入以下指令:

#include 〈 afxpriv.h>

#include 〈 afxres.h>

六 將該對話方塊作為一個 SDI 或 MDI 應用程式的主架構的子視窗,產生這樣一個對話方塊後,當你把滑鼠移到某個控制項 ( 必須有相應的字串資源 )上時,就會出現該控制項的 ToolTip 和狀態條資訊。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.