分类: C/C++
2012-01-27 01:45:24
2.在对话框实现文件写入以下代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /*----------------------------------------------------------------------------- Description: 创建控件的Tooltip -----------------------------------------------------------------------------*/ HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText) { if (!toolID || !hDlg || !pszText) { return FALSE; } HWND hwndTool = GetDlgItem(hDlg, toolID); HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_ALWAYSTIP | TTS_BALLOON, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hDlg, NULL, AfxGetInstanceHandle(), NULL); if (!hwndTool || !hwndTip) { return (HWND)NULL; } TOOLINFO toolInfo = {0}; toolInfo.cbSize = sizeof(TOOLINFO); toolInfo.hwnd = hDlg; toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS; toolInfo.uId = (UINT_PTR)hwndTool; toolInfo.lpszText = pszText; SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo); return hwndTip; } |
3.在对话框OnInitDialog()函数添加代码:
1 | CreateToolTip(IDOK, m_hWnd, _T("这是按钮的ToolTip语句")); |
如何创建一个矩形区域的工具提示控件 How to Create a Tooltip for a Rectangular Area
1.创建一个基于对话框的工程TestToolTip;
2.在对话框实现文件写入以下代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | /*----------------------------------------------------------------------------- Description: 创建矩形区域ToolTip -----------------------------------------------------------------------------*/ void CreateToolTipForRect(HWND hwndParent) { HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwndParent, NULL, AfxGetInstanceHandle(), NULL); SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); TOOLINFO ti = {0}; ti.cbSize = sizeof(TOOLINFO); ti.uFlags = TTF_SUBCLASS; ti.hwnd = hwndParent; ti.hinst = AfxGetInstanceHandle(); ti.lpszText = _T("这是你的ToolTip语句"); GetClientRect(hwndParent, &ti.rect); SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&ti); } |
代码中,第9行的设置WS_POPUP风格,当CreateWindowEx一个Tooltip时,总是具有WS_POPUP和WS_EX_TOOLWINDOW风格,不管在创建的时候你是否指定。TTS_NOPREFIX指示当光标在一个工具上时显示工具提示,不管工具提示的属主窗口是否是处于活动状态。没有这个风格,则只有当工具的属主窗口是活动的时候才会显示工具提示控件,否则不显示。TTS_ALWAYSTIP这个风格禁止系统将&字符从一个字符串中去掉。如果一个工具提示控件没有TTS_NOPREFIX风格,则系统自动去掉&字符,让应用程序用同一个字符串作为菜单项和工具提示控件中的文本。
3.在对话框OnInitDialog()函数添加代码:
1 2 | CreateToolTipForRect(m_hWnd); //对话框提示 CreateToolTipForRect(GetDlgItem(IDOK)->m_hWnd); //“确定”按钮提示 |
4.编译运行,鼠标移到对话框上,暂停鼠标移动,截图如下:
如何实现跟踪工具提示控件 How to Implement Tracking Tooltips
1.创建一个基于对话框的工程TestToolTip;
2.在对话框实现文件写入以下代码:
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | HWND CreateTrackingToolTip(int toolID, HWND hDlg, WCHAR *pText) { HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hDlg, NULL, AfxGetInstanceHandle(), NULL); if (!hwndTT) { return NULL; } g_toolItem.cbSize = sizeof(TOOLINFO); g_toolItem.uFlags = TTF_IDISHWND | TTF_TRACK | TTF_ABSOLUTE; g_toolItem.hwnd = hDlg; g_toolItem.hinst = AfxGetInstanceHandle(); g_toolItem.lpszText = pText; g_toolItem.uId = (UINT_PTR)hDlg; GetClientRect(hDlg, &g_toolItem.rect); SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&g_toolItem); return hwndTT; } |
1 | g_hwndTrackingTT = CreateTrackingToolTip(IDOK, m_hWnd, _T("ToolTip")); |
1 2 3 4 5 6 7 8 9 | /*----------------------------------------------------------------------------- Description: 鼠标移出窗口,停用ToolTip -----------------------------------------------------------------------------*/ void CTestToolTipDlg::OnMouseLeave() { ::SendMessage(g_hwndTrackingTT, TTM_TRACKACTIVATE, (WPARAM)FALSE, (LPARAM)&g_toolItem); g_TrackingMouse = FALSE; CDialog::OnMouseLeave(); } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | /*----------------------------------------------------------------------------- Description: 鼠标移动,ToolTip更新信息 -----------------------------------------------------------------------------*/ void CTestToolTipDlg::OnMouseMove(UINT nFlags, CPoint point) { static int oldX, oldY; int newX, newY; if (!g_TrackingMouse) { TRACKMOUSEEVENT tme = {sizeof(TRACKMOUSEEVENT)}; tme.hwndTrack = m_hWnd; tme.dwFlags = TME_LEAVE; TrackMouseEvent(&tme); ::SendMessage(g_hwndTrackingTT, TTM_TRACKACTIVATE, (WPARAM)TRUE, (LPARAM)&g_toolItem); g_TrackingMouse = TRUE; } newX = point.x; newY = point.y; if ((newX != oldX) || (newY != oldY)) { oldX = newX; oldY = newY; WCHAR coords[12]; swprintf_s(coords, ARRAYSIZE(coords), _T("%d, %d"), newX, newY); g_toolItem.lpszText = coords; ::SendMessage(g_hwndTrackingTT, TTM_SETTOOLINFO, 0, (LPARAM)&g_toolItem); POINT pt = {newX, newY}; ::ClientToScreen(m_hWnd, &pt); ::SendMessage(g_hwndTrackingTT, TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(pt.x + 10, pt.y - 20)); } CDialog::OnMouseMove(nFlags, point); } |