文章类型: VC&C++
关键词: VC,解决,dll,ocx,CButtonST,显示,Tooltip,提示功能
内容摘要: VC下解决在dll或ocx中使用CButtonST并显示Tooltip提示功能

VC下解决在dll或ocx中使用CButtonST并显示Tooltip提示功能

2018/6/4 16:43:58    来源:apple    阅读:

ST本身具备显示Tooltip的功能,但是限于在exe程序中,要是在dll或者ocx等程序中,则Tooltip显示不出来,问题在于对于dll中的资源来说,根本无法响应pretranslatemessage 函数 ,所以tooltips显示不出来。网上给出了两个办法解决:1 消息钩子;2 dll 导出函数;


个人觉得都太麻烦,于是修改了CButtonST类,将其成员m_Tooltip类型从MFC的CTooltipCtrl改为自己实现的CTipWnd(继承自CStatic),一劳永逸的解决了在dll工程中使用CButtonST显示不了Tooltip的问题。修改后的CButtonST可直接拿来使用,并显示Tooltip。

下面给出源码和关键代码:

//目的:解决CBUTTONST有关“SetTooltipText”无效的问题。
//目的:解决了提示框显示位置超范围和显示位置不合适的问题。
void CButtonST::ShowTooltip(BOOL bShow)
{
	if(m_ToolTip.GetSafeHwnd() == NULL)
		return;
	
	if(bShow)
	{
		if(m_ToolTip.IsShow())
			return;

		m_ToolTip.SetShowFlag(TRUE);

		//目的:获取提示框的默认显示位置。
		CRect rc;
		m_ToolTip.GetWindowRect(&rc);
		ClientToScreen(&rc);

		CRect rect;
		GetClientRect(&rect);
		ClientToScreen(&rect);

		rc.OffsetRect(rect.left - rc.left, rect.bottom - rc.top + 10);

		/*
		//目的:测试提示框的位置。
		rc.top = rc.top - 50;
		rc.bottom = rc.bottom - 50;
		rc.left = rc.left - 50;
		rc.right = rc.right - 50;*/
		//对话框窗体大小及其屏幕坐标
		//目的:获取主窗体的大小。Add by 石民生 2018.06.04
		CRect rectDlg;
		CWnd* pWnd = AfxGetMainWnd();
		pWnd->GetWindowRect(rectDlg);//获得窗体在屏幕上的位置

		//目的:提示框的默认显示位置上移5px。
		rc.top = rc.top - 5;
		rc.bottom = rc.bottom - 5;

		if (rc.top < 0)
		{
			rc.OffsetRect(0, 0 - rc.top);
		}
		if (rc.left < 0)
		{
			rc.OffsetRect(0 - rc.left, 0);
		}

		//目的:提示框的最右端不能超出窗体的右端位置。
		if (rc.right > (rectDlg.left + rectDlg.right))
		{
			int offsetPx = rc.right - (rectDlg.left + rectDlg.right);
			rc.left = rc.left - offsetPx;
			rc.right = rc.right - offsetPx;
		}

		//目的:提示框的最底端不能超出窗体的底边。
		if (rc.bottom > (rectDlg.top + rectDlg.bottom))
		{
			int offsetPy = rc.bottom - (rectDlg.top + rectDlg.bottom);
			rc.top = rc.top - offsetPy;
			rc.bottom = rc.bottom - offsetPy;
		}

		m_ToolTip.MoveWindow(&rc);
		m_ToolTip.ShowWindow(SW_SHOW);		
	}
	else
	{
		m_ToolTip.ShowWindow(SW_HIDE);
		m_ToolTip.SetShowFlag(FALSE);
	}
}

函数定义如下:

CTipWnd m_ToolTip; // Tooltip

void SetTooltipText(LPCTSTR lpszText, BOOL bActivate = TRUE);

  // This function sets the text to show in the button tooltip.
  //
  // Parameters:
  //		[IN]	lpszText
  //				Pointer to a null-terminated string containing the text to show.
  //		[IN]	bActivate
  //				If TRUE the tooltip will be created active.
  //
void CButtonST::SetTooltipText(LPCTSTR lpszText, BOOL bActivate)
{
	// We cannot accept NULL pointer
	if (lpszText == NULL) return;

	// Initialize ToolTip
	if (m_ToolTip.GetSafeHwnd() == NULL)
		m_ToolTip.Create();
	m_ToolTip.SetTipText(CString(lpszText));
} // End of SetTooltipText

设置文本内容提示的实现:

void CTipWnd::SetTipText(CString strTipText)
{
	m_strTipText = strTipText;
	m_uBGImageID = IDB_TOOLTIP_BG;

	//根据TooltipText设置窗口大小
	CDC* pDC = GetDC();
	pDC->SelectObject(&m_oFont);
	CSize size = pDC->GetTextExtent(m_strTipText);
	CRect rect(0, 0, size.cx + 10, size.cy + 10);
	MoveWindow(&rect);
}


源码下载:

TooltipBtnST_解决按钮提示不显示问题.zip


↑ 上一篇文章:win7提示在此页面上的Activex控件交互可能不安全怎么办 关键词:win7,提示,在此页面上,Activex,控件,交互,.. 发布日期:2018/6/1 14:58:45
↓ 下一篇文章:.NET网站发布后出现Access to the path : D:/... is denied 解决方案 关键词:.NET,网站,发布,Access,to,the,pat.. 发布日期:2018/6/12 11:56:36
相关文章:
VC基于MFC的OCX中使用对话框显示GIF动画图片和使用时间定时器 关键词:VC,MFC,OCX,DLL,资源模块,句柄,切换,GIF,动画,图片,定时器,对话框,DoMo.. 发布日期:2018-09-07 16:13
VC++动态链接库(DLL)编程深入浅出(转载并进行整理更新内容) 关键词:VC++动态链接库(DLL)编程深入浅出,转载并进行整理更新内容 发布日期:2016-08-24 15:09
关于百度富文本编辑器ueditor的.NET版本地图片上传提示uploader类同时存在于两个dll中的解决方法 关键词:百度,富文本编辑器,ueditor,.NET,Uploader类型同时存在两个dll中,uplo.. 发布日期:2016-05-15 00:01
相关目录:.NETVC&C++软件开发
我要评论
正在加载评论信息......