如何在MFC中获得多行工具提示 [英] How do I get a multi line tooltip in MFC
问题描述
现在,当我将鼠标悬停在编辑框上时,会弹出一个工具提示.问题在于此工具提示包含多个错误消息,并且它们都排成一行.我需要每个错误消息都在自己的行上.错误消息包含在CString中,并用新行将它们分开.
Right now, I have a tool tip that pops up when I hover over an edit box. The problem is that this tool tip contains multiple error messages and they are all in one long line. I need to have each error message be on its own line. The error messages are contained in a CString with a new line seperating them.
下面是我现有的代码.
BOOL OnToolTipText(UINT, NMHDR* pNMHDR, LRESULT* pResult)
{
ASSERT(pNMHDR->code == TTN_NEEDTEXTA || pNMHDR->code == TTN_NEEDTEXTW);
// need to handle both ANSI and UNICODE versions of the message
TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;
// TCHAR szFullText[256];
CString strTipText=_T("");
UINT nID = pNMHDR->idFrom;
if (pNMHDR->code == TTN_NEEDTEXTA && (pTTTA->uFlags & TTF_IDISHWND) ||
pNMHDR->code == TTN_NEEDTEXTW && (pTTTW->uFlags & TTF_IDISHWND))
{
// idFrom is actually the HWND of the tool
nID = ::GetDlgCtrlID((HWND)nID);
}
//m_errProjAccel[ch] contains 1 or more error messages each seperated by a new line.
if((int)nID >= ID_PROJECTED_ACCEL1 && (int)nID < ID_PROJECTED_ACCEL1 + PROJECTED_ROWS -1 ) {
int ch = nID - ID_PROJECTED_ACCEL1;
strTipText = m_errProjAccel[ch];
}
#ifndef _UNICODE
if (pNMHDR->code == TTN_NEEDTEXTA)
lstrcpyn(pTTTA->szText, strTipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
else
_mbstowcsz(pTTTW->szText, strTipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
#else
if (pNMHDR->code == TTN_NEEDTEXTA)
_wcstombsz(pTTTA->szText, strTipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
else
lstrcpyn(pTTTW->szText, strTipText, sizeof(pTTTA->szText)/sizeof(pTTTA->szText[0]));
#endif
*pResult = 0;
// bring the tooltip window above other popup windows
::SetWindowPos(pNMHDR->hwndFrom, HWND_TOP, 0, 0, 0, 0,
SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOOWNERZORDER);
return TRUE; // message was handled
}
推荐答案
TTM_SETMAXTIPWIDTH消息发送到ToolTip控件,以响应TTN_GETDISPINFO
通知,以强制其使用多行.在您的字符串中,应使用\r\n
分隔行.
Creating multiline tooltips is explained here in the MSDN library - read the "Implementing Multiline ToolTips" section. You should send a TTM_SETMAXTIPWIDTH
message to the ToolTip control in response to a TTN_GETDISPINFO
notification to force it to use multiple lines. In your string you should separate lines with \r\n
.
此外,如果您的文本超过80个字符,则应使用NMTTDISPINFO
结构的lpszText
成员,而不是复制到szText
数组中.
Also, if your text is more than 80 characters, you should use the lpszText
member of the NMTTDISPINFO
structure instead of copying into the szText
array.
这篇关于如何在MFC中获得多行工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!