如何在移动鼠标时重绘 MFC 工具提示?
How to redraw MFC tooltip when a mouse is moved?
这是一个 c++/MFC 项目。
我需要显示光标的位置。所以每次鼠标移动时我都必须重绘工具提示。
我可以绘制工具提示,但绘制的工具提示未被清除。
鼠标移动时会调用OnShowTooltip()
函数
void OnShowToolTip(const CPoint& ptMousePosition,CString strText)
{
UpdateData(true);
if (bToolTip)
{
unsigned int uid = 0; // for ti initialization
// CREATE A TOOLTIP WINDOW
hwndTT = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX ,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL
);
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
//ti.cbSize = sizeof(TOOLINFO);
ti.cbSize = TTTOOLINFO_V1_SIZE;
ti.uFlags = TTF_TRACK;
ti.hwnd = NULL;
ti.hinst = NULL;
ti.uId = uid;
ti.lpszText = (LPSTR)(LPCSTR) strText;
// ToolTip control will cover the whole window
ti.rect.left = 0;
ti.rect.top = 0;
ti.rect.right = 0;
ti.rect.bottom = 0;
// SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
::SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
::SendMessage(hwndTT, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(ptMousePosition.x, ptMousePosition.y));
::SendMessage(hwndTT, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
bToolTip=false;
}else{
::SendMessage(hwndTT, TTM_TRACKACTIVATE, false, (LPARAM)(LPTOOLINFO) &ti);
bToolTip=true;
}
}
我有一个处理鼠标事件的函数,如下所示
BOOL DoDragPoint2D(CPoint point,CString strPositions)
{
switch (msg.message)
{
case WM_MOUSEMOVE:
bToolTip = TRUE;
OnShowToolTip(point,strPositions);
break;
case WM_LBUTTONUP:
bToolTip = FALSE;
OnShowToolTip(point,strPositions);
}
}
虽然我把bTooltip
设为FALSE
。但它不能删除工具提示。
此外,我尝试调用 Invalidate()
函数,但在停止配音之前工具提示仍然显示。
每次鼠标移动时,都会创建一个新的工具提示。
您只需要在 WM_CREATE
事件中创建一次工具提示。
然后发送TTM_SETTOOLINFO
消息更新ti.lpszText
。
ep.
case WM_CREATE:
{
unsigned int uid = 0; // for ti initialization
hwndTT = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL
);
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
//ti.cbSize = sizeof(TOOLINFO);
ti.cbSize = TTTOOLINFO_V1_SIZE;
ti.uFlags = TTF_TRACK;
ti.hwnd = NULL;
ti.hinst = NULL;
ti.uId = uid;
ti.lpszText = const_cast <wchar_t*>(L"");
// ToolTip control will cover the whole window
ti.rect.left = 0;
ti.rect.top = 0;
ti.rect.right = 0;
ti.rect.bottom = 0;
::SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&ti);
}
break;
case WM_MOUSEMOVE:
...
ti.lpszText = (LPSTR)(LPCSTR) strText;
::SendMessage(hwndTT, TTM_SETTOOLINFO, 0, (LPARAM)&ti);
::SendMessage(hwndTT, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(ptMousePosition.x, ptMousePosition.y));
::SendMessage(hwndTT, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
...
这是一个 c++/MFC 项目。 我需要显示光标的位置。所以每次鼠标移动时我都必须重绘工具提示。 我可以绘制工具提示,但绘制的工具提示未被清除。
鼠标移动时会调用OnShowTooltip()
函数
void OnShowToolTip(const CPoint& ptMousePosition,CString strText)
{
UpdateData(true);
if (bToolTip)
{
unsigned int uid = 0; // for ti initialization
// CREATE A TOOLTIP WINDOW
hwndTT = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX ,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL
);
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
//ti.cbSize = sizeof(TOOLINFO);
ti.cbSize = TTTOOLINFO_V1_SIZE;
ti.uFlags = TTF_TRACK;
ti.hwnd = NULL;
ti.hinst = NULL;
ti.uId = uid;
ti.lpszText = (LPSTR)(LPCSTR) strText;
// ToolTip control will cover the whole window
ti.rect.left = 0;
ti.rect.top = 0;
ti.rect.right = 0;
ti.rect.bottom = 0;
// SEND AN ADDTOOL MESSAGE TO THE TOOLTIP CONTROL WINDOW
::SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
::SendMessage(hwndTT, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(ptMousePosition.x, ptMousePosition.y));
::SendMessage(hwndTT, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
bToolTip=false;
}else{
::SendMessage(hwndTT, TTM_TRACKACTIVATE, false, (LPARAM)(LPTOOLINFO) &ti);
bToolTip=true;
}
}
我有一个处理鼠标事件的函数,如下所示
BOOL DoDragPoint2D(CPoint point,CString strPositions)
{
switch (msg.message)
{
case WM_MOUSEMOVE:
bToolTip = TRUE;
OnShowToolTip(point,strPositions);
break;
case WM_LBUTTONUP:
bToolTip = FALSE;
OnShowToolTip(point,strPositions);
}
}
虽然我把bTooltip
设为FALSE
。但它不能删除工具提示。
此外,我尝试调用 Invalidate()
函数,但在停止配音之前工具提示仍然显示。
每次鼠标移动时,都会创建一个新的工具提示。
您只需要在 WM_CREATE
事件中创建一次工具提示。
然后发送TTM_SETTOOLINFO
消息更新ti.lpszText
。
ep.
case WM_CREATE:
{
unsigned int uid = 0; // for ti initialization
hwndTT = CreateWindowEx(WS_EX_TOPMOST,
TOOLTIPS_CLASS,
NULL,
TTS_NOPREFIX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL
);
// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
//ti.cbSize = sizeof(TOOLINFO);
ti.cbSize = TTTOOLINFO_V1_SIZE;
ti.uFlags = TTF_TRACK;
ti.hwnd = NULL;
ti.hinst = NULL;
ti.uId = uid;
ti.lpszText = const_cast <wchar_t*>(L"");
// ToolTip control will cover the whole window
ti.rect.left = 0;
ti.rect.top = 0;
ti.rect.right = 0;
ti.rect.bottom = 0;
::SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)(LPTOOLINFO)&ti);
}
break;
case WM_MOUSEMOVE:
...
ti.lpszText = (LPSTR)(LPCSTR) strText;
::SendMessage(hwndTT, TTM_SETTOOLINFO, 0, (LPARAM)&ti);
::SendMessage(hwndTT, TTM_TRACKPOSITION, 0, (LPARAM)(DWORD) MAKELONG(ptMousePosition.x, ptMousePosition.y));
::SendMessage(hwndTT, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
...