提示控件的使用

//我们知道,当我们在使用一个软件时,我们移动鼠标,当光标移到一个按钮或其他控件上时,一般会出现一个矩形的提示框,列出相关信息,下面我们利用提示控件实现这一功能。

下面的代码我是参考其他资料完善的。

#include "stdafx.h"
#include "resource.h"
#include "Windows.h"
#include "tchar.h"
#include "commctrl.h"
#pragma comment(lib,"comctl32.lib")

 
TCHAR ToolTipsClassName[] = _T("Tooltips_class32");
TCHAR MainDialogText1[] = _T("This is the upper left area of the dialog");
TCHAR MainDialogText2[] = _T("This is the upper right area of the dialog");
TCHAR MainDialogText3[] = _T("This is the lower left area of the dialog");
TCHAR MainDialogText4[] = _T("This is the lower right area of the dialog");

HINSTANCE g_hInstance;
HWND hwndTool;

void SetDlgToolArea(HWND hDlg,LPTOOLINFO pTi,LPSTR lpText,DWORD id,RECT *lprect)
{
 if(id == 0)
 {
  pTi->rect.left = 0;
  pTi->rect.top = 0;
  pTi->rect.right = (lprect->right - lprect->left)/2;
  pTi->rect.bottom = (lprect->bottom - lprect->top)/2;

 }
 else if(id == 1)
 {
  pTi->rect.left = (lprect->right - lprect->left)/2 + 1;
  pTi->rect.top = 0;
  pTi->rect.right = (lprect->right - lprect->left);
  pTi->rect.bottom = (lprect->bottom - lprect->top);
 }
 else if(id == 2)
 {
  pTi->rect.top = (lprect->bottom - lprect->top)/ 2 + 1;
  pTi->rect.left = 0;
  pTi->rect.right = (lprect->right - lprect->left)/2;
  pTi->rect.bottom = lprect->bottom - lprect->top;
 }
 else
 {
  pTi->rect.left = (lprect->right - lprect->left)/2 + 1;
  pTi->rect.top = (lprect->bottom - lprect->top)/2 + 1;
  pTi->rect.bottom = lprect->bottom - lprect->top;
  pTi->rect.right = lprect->right - lprect->left;
 }

 pTi->lpszText = lpText;
 SendMessage(hwndTool,TTM_ADDTOOL,NULL,(LPARAM)pTi);
}

BOOL CALLBACK EnumChild( HWND hwnd,
 LPARAM lParam
 )
{
 CHAR buffer[256] = {0};
 LPTOOLINFO pTi = (LPTOOLINFO)lParam;
 pTi->uId = (UINT)hwnd;
 pTi->uFlags |= TTF_IDISHWND;
 GetWindowText(hwnd,buffer,255);
 pTi->lpszText = buffer;
 SendMessage(hwndTool,TTM_ADDTOOL,NULL,(LPARAM)pTi);
 return TRUE;
}
INT_PTR CALLBACK DialogProc(
 HWND hwndDlg,
 UINT uMsg,
 WPARAM wParam,
 LPARAM lParam
 )
{

 TOOLINFO ti;
 DWORD id;
 RECT rect;
 switch(uMsg)
 {
 case WM_INITDIALOG:
  InitCommonControls();
  hwndTool = CreateWindowEx(NULL,ToolTipsClassName,NULL,TTS_ALWAYSTIP,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,g_hInstance,NULL);
  id = 0;
  ti.cbSize = sizeof(TOOLINFO);
  ti.uFlags = TTF_SUBCLASS;
  ti.hwnd = hwndDlg;
  GetWindowRect(hwndDlg,&rect);
  SetDlgToolArea(hwndDlg,&ti,MainDialogText1,id,&rect);
  id++;
  SetDlgToolArea(hwndDlg,&ti,MainDialogText2,id,&rect);
  id++;
  SetDlgToolArea(hwndDlg,&ti,MainDialogText3,id,&rect);
  id++;
  SetDlgToolArea(hwndDlg,&ti,MainDialogText4,id,&rect);
  EnumChildWindows(hwndDlg,EnumChild,(LPARAM)&ti);

  break;
 case WM_CLOSE:
  EndDialog(hwndDlg,NULL);
  break;
 default:
  return FALSE;
 }
 return TRUE;
}
int WINAPI WinMain(     HINSTANCE hInstance,
 HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
 int nCmdShow
 )
{
 g_hInstance = hInstance;
 DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_ABOUTBOX),NULL,(DLGPROC)DialogProc,NULL);
 return 0;
}

 

 

 

 

ps:MSDN上是这样写的,已经调试过,可以直接使用:

void CreateToolTipForRect(HWND hCtrl,TCHAR *szText)//hCtrl是控件的句柄,szText是鼠标滑动到该控件上显示的文本
{
 // Create a tooltip.
 HINSTANCE hInst=(HINSTANCE)GetWindowLong(hCtrl,GWL_HINSTANCE);
 HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST,
  TOOLTIPS_CLASS, NULL,
  WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,       
  CW_USEDEFAULT, CW_USEDEFAULT,
  CW_USEDEFAULT, CW_USEDEFAULT,
  hCtrl, NULL, hInst,NULL);

 SetWindowPos(hwndTT, HWND_TOPMOST,
  0, 0, 0, 0,
  SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

 // Set up "tool" information.
 // In this case, the "tool" is the entire parent window.
 TOOLINFO ti = { 0 };
 ti.cbSize = sizeof(TOOLINFO);
 ti.uFlags = TTF_SUBCLASS;
 ti.hwnd = hCtrl;
 ti.hinst = hInst;
 ti.lpszText = szText;
 GetClientRect (hCtrl, &ti.rect);

 // Associate the tooltip with the "tool" window.
 SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);   
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值