加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

ToolTip提示多行显示

(2011-03-13 19:09:51)
标签:

tooltip

多行显示

分类: VC

编程遇到一个这样的问题,ToolTip提示框中想多行显示,开始就直接想,在想换行的地方加\n,发现没效果,在http://topic.csdn.net/t/20030324/19/1571119.html上看到有人讨论,有人摘过来一段解释,可能是MSDN上的,按说明做了,解决了问题。

Multiline   ToolTips
Multiline   ToolTips   allow   text   to   be   displayed   on   more   than   one   line.   They   are   supported   by   version   4.70   and   later   of   the   common   controls.   Your   application   creates    multiline   ToolTip   by   responding   to    TTN_GETDISPINFO   notification   message.   To   force   the   ToolTip   control   to   use   multiple   lines,   send    TTM_SETMAXTIPWIDTH   message,   specifying   the   width   of   the   display   rectangle.   Text   that   exceeds   this   width   will   wrap   to   the   next   line   rather   than   widening   the   display   region.   The   rectangle   height   will   be   increased   as   needed   to   accommodate   the   additional   lines.   The   ToolTip   control   will   wrap   the   lines   automatically,   or   you   can   use    carriage   return/line   feed   combination,   \r\n,   to   force   line   breaks   at   particular   locations.
Note   that   the   text   buffer   specified   by   the   szText   member   of   the   NMTTDISPINFO   structure   can   accommodate   only   80   characters.   If   you   need   to   use    longer   string,   point   the   lpszText   member   of   NMTTDISPINFO   to    buffer   containing   the   desired   text.
The   following   code   fragment   is   an   example   of    simple   TTN_GETDISPINFO   notification   handler.   It   creates    multiline   ToolTip   by   setting   the   display   rectangle   to   300   pixels   and   setting   the   lpszText   member   of   NMTTDISPINFO   to   point   to    buffer   with   the   desired   text.
Example
char   szLongMessage[    =
"This   is    long   message   for   the   ToolTip,   which   will   automatically   "
"be   wrapped   when   it   exceeds   the   maximum   tip   width.     "
"Alternatively,   you   can   use    \r\n "
"carriage   return/line   feed   combination\r\n "
"to   force   line   breaks   at   specific\r\n "
"locations. ";
switch   (lpnmhdr-> code)   {
    case   TTN_GETDISPINFO:
        lpttd    (LPNMTTDISPINFO)lpnmhdr;
        SendMessage(lpnmhdr-> hwndFrom,   TTM_SETMAXTIPWIDTH,   0,   300);
        lpttd-> lpszText    szLongMessage;
        return   0;
...
  //Other   notification   handlers   go   here,   as   needed.
}

 

翻译一下吧,正好练练英语---

多行提示:

多行提示允许文本以多行被显示。这个功能是由4.70版本的控件及以后开始支持的。程序通过响应一个TTN_GETDISPINFO通知消息来创建多行提示的。为了使ToolTip控件能使用多行显示功能,需要发送TTM_SETMAXTIPWIDTH消息来指定提示框的宽度。超过此宽度的文本将在下行显示,而不是加宽显示区域。提示框的高度也将自动增加到所需要的大小以适应额外加的行。另外,ToolTip控件换行是自动的,也可以通过使用回车换行符(\r\n)来强制换行。

 

注意,由NMTTDISPINFO数据结构的szText成员指定文本缓冲区只能容纳80个字符。如果你需要使用更长的字符串,那么就将NMTTDISPINFO的lpszText成员指向一个包含理想文本的缓冲区。

 

下面的代码段是一个简单的使用TTN_GETDISPINFO通知句柄的例子,它通过设置显示框的宽度为300个像素来创建一个多行ToolTip对象,并设置NMTTDISPINFO的lpszText成员指向一个包含理想文本的缓冲区。

 

eg.

char   szLongMessage[    =
"This   is    long   message   for   the   ToolTip,   which   will   automatically   "
"be   wrapped   when   it   exceeds   the   maximum   tip   width.     "
"Alternatively,   you   can   use    \r\n "
"carriage   return/line   feed   combination\r\n "
"to   force   line   breaks   at   specific\r\n "
"locations. ";
switch   (lpnmhdr-> code)   {
    case   TTN_GETDISPINFO:
        lpttd    (LPNMTTDISPINFO)lpnmhdr;
        SendMessage(lpnmhdr-> hwndFrom,   TTM_SETMAXTIPWIDTH,   0,   300);
        lpttd-> lpszText    szLongMessage;
        return   0;
...
  //Other   notification   handlers   go   here,   as   needed.
}

 

翻译结束-----------

 

以上程序估计使用的估计是Windows编程,对于MFC,这一点很简单:

先按之前讲过的方法http://blog.sina.com.cn/s/blog_6163bdeb0100pcld.html建立ToolTip文本提示对象;

然后调用其成员函数m_tooltip.SetMaxTipWidth(600);设置文本框的最大宽度,注意里边的数值单位为像素,所以要通过不断测试来选定最理想的宽度;

然后调用m_tooltip.Activate(TRUE);就可以正常显示多行了(换行是自动的,也可以通过使用回车换行符(\r\n)来强制换行)。

 

使用ToolTip有几点需要注意一下:

如果我想在需要显示的时候显示提示,可以使用m_tooltip.Activate(TRUE);函数,但发现如果已经是TRUE了,再Activate,提示框就不显示了,此时可以调用m_tooltip.UpdateTipText(str,GetDlgItem(IDC_BUTTON_METHOD_TIP));更新一下要显示内容,虽然内容可能没有变,然后再Activate(TRUE)就能正常显示了;

另外可以调用m_tooltip.SetDelayTime(0);函数来设置,提示显示的等待时间,设为0,就表示不等待,立即显示。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有