windowsAPI popup trace tip(toolTip)

时间:2021-10-14 23:08:15

class UIHELPER_EXPORT ToolTipWindow : public chMessageHandler
{
DECLARE_PROCESS_OBJECT(ToolTipWindow);
public:
bool updateTipText(const chConstStringA text);
private:
bool initTipsWindowsBywinAPI();
void AddWindowTool(HWND hwndTip);
bool ShowTipWindow();
bool HideTipWindow();
BOOL onProcessMessage(msgObject& objMessage);
public:
virtual BOOL TryThreadMessage(msgObject& objMessage);
public:
ToolTipWindow();
~ToolTipWindow();
private:
HWND hwndTip;
TOOLINFOW toolInfo;
chStringW m_strText;
};

#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")

IMPLEMENT_PROCESS_OBJECT(ToolTipWindow);
bool ToolTipWindow::initTipsWindowsBywinAPI()
{
chASSERT(hwndTip == NULL);

hwndTip = CreateWindowEx(NULL,
TOOLTIPS_CLASS,
NULL,
WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL
);
::SetWindowPos(hwndTip, HWND_TOPMOST,0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
AddWindowTool(hwndTip);
return true;
}
void ToolTipWindow::AddWindowTool(HWND hwndTip)
{
memset(&toolInfo, 0, sizeof(TOOLINFO));
toolInfo.cbSize = sizeof(TOOLINFO);
toolInfo.uFlags = TTF_IDISHWND | TTF_TRACK;
toolInfo.uId = NULL;//(UINT_PTR)hParrent;
toolInfo.lpszText = NULL;

::SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &toolInfo);
}
bool ToolTipWindow::updateTipText(const chConstStringA text)
{
chASSERT(hwndTip != NULL);
m_strText = chUTF82W(text);
toolInfo.lpszText = (LPWSTR)m_strText.c_str();
::SendMessage(hwndTip, TTM_UPDATETIPTEXTW, 0, (LPARAM)&toolInfo);
return true;
}
bool ToolTipWindow::ShowTipWindow()
{
POINT currentPoint;
GetCursorPos(&currentPoint);
SYZ_TRACE("getCursorPos x==[%d], y==[%d]", currentPoint.x, currentPoint.y);

::SendMessage(hwndTip, TTM_TRACKPOSITION, 0, (LPARAM) MAKELONG(currentPoint.x + 5, currentPoint.y + 10));
::SendMessage(hwndTip, TTM_TRACKACTIVATE,(WPARAM)TRUE,(LPARAM)&toolInfo);
return TRUE;
}
bool ToolTipWindow::HideTipWindow()
{
::SendMessage(hwndTip, TTM_TRACKACTIVATE,(WPARAM)FALSE,(LPARAM)&toolInfo);
return TRUE;
}
BOOL ToolTipWindow::onProcessMessage(msgObject& objMessage)
{
switch(objMessage.message)
{
case TOOLTIPWINDOWS_UPDATETIPTEXT:
updateTipText(chStringA((LPCSTR)objMessage.GetExtraData(), objMessage.GetExtraSize()));
break;
case TOOLTIPWINDOWS_SHOWTIPWINDOW:
ShowTipWindow();
break;
case TOOLTIPWINDOWS_HIDETIPWINDOW:
HideTipWindow();
break;
default:
break;
}
return TRUE;
}
BOOL ToolTipWindow::TryThreadMessage(msgObject& objMessage)
{
switch(objMessage.message)
{
case TOOLTIPWINDOWS_UPDATETIPTEXT:
updateTipText((LPCSTR)objMessage.GetExtraData());
break;
case TOOLTIPWINDOWS_SHOWTIPWINDOW:
ShowTipWindow();
break;
case TOOLTIPWINDOWS_HIDETIPWINDOW:
HideTipWindow();
break;
default:
break;
}
return TRUE;
}
ToolTipWindow::ToolTipWindow()
{
hostThread().AddMessageHandler(this);
hwndTip = NULL;
initTipsWindowsBywinAPI();
msgJoinMessageGroup(szToolTipWindowsGroupName, MSG_METHOD(this, ToolTipWindow::onProcessMessage));
}
ToolTipWindow::~ToolTipWindow()
{
if (hwndTip != NULL)
{
::SendMessage(hwndTip, WM_DESTROY, 0, 0);
::DestroyWindow(hwndTip);
hwndTip = NULL;
}
}