Display Information on the VC implementation dialog box

Source: Internet
Author: User
Using the AppWizard of VC, you can easily implement ToolTip of tool bar and menu item, or display help information on the status bar, however, it is not easy to display ToolTip and control information on status bar on the control of the dialog box. Now we can use the WM_SETCURSOR and TTN_NEEDTEXT messages in VC to achieve our goal. The procedure is as follows:

1. Use MFC AppWizard of VC to generate an SDI or MDI Application

  

2. Edit the string resource of the dialog box Control

For example: IDC_DBBUTTON1 = "This is Xiao tianpeng's first homemade button tianpeng ",

The string "This is Xiao tianpeng's first homemade button" will be displayed on the status bar when you move the mouse over the control, and the string "tianpeng" will be displayed as a ToolTip.

3. Establish message ing

In the header file (*. H) of the dialog box

Add the following code:
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 ()
Add the following code to the implementation file (*. CPP) in the dialog box:
BEGIN_MESSAGE_MAP (CFileOp1, CDialog)
// {AFX_MSG_MAP (CFileOp1)
ON_WM_DESTROY ()
ON_WM_SETCURSOR ()
//} AFX_MSG_MAP
On_policy_ex (TTN_NEEDTEXT, 0, OnTipNotvify)
END_MESSAGE_MAP ()
4. edit message processing functions
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 ,′′);
If (! StrTipText. IsEmpty ())
Strcpy (pTTT-> lpszText, StrTipText );
PTTT-> hinst = AfxGetResourceHandle ();
Return (TRUE );}}
Return (FALSE );}
  
  

5. Use this dialog box as a subwindow of the main framework of the SDI or MDI application. After such a dialog box is generated, move the cursor to a control (corresponding string resources are required) the ToolTip and status bar of the control are displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.