c++ - 按钮的工具提示

标签 c++ winapi

我使用 MSDN 中的函数 Create a Tooltip for a Control

HWND CreateToolTip(int toolID, HINSTANCE hInst, 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,
    hInst, NULL);

if (!hwndTool || !hwndTip) {
    return (HWND)NULL;
}

TOOLINFO toolInfo;
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;
}

然后在 WndProc WM_CREATE 我创建按钮 Button=CreateWindowEx( 0, L“按钮”, L“我的按钮”, WS_VISIBLE|WS_CHILD, 10, 10, 100, 24, hWnd, (HMENU)ID_TOOLTIP, 提示, 空);
最后创建tooltip tooltip_mess = CreateToolTip(ID_TOOLTIP, hInst, hWnd, (PTSTR)"Tooltip message");
但是不行,我看不到我的工具提示,我哪里做错了?

最佳答案

试试这个:

HWND CreateToolTip(int toolID, HWND hDlg, HINSTANCE hInst, PTSTR pszText)
{
    if (!toolID || !hDlg || !pszText)
    {
        return NULL;
    }

    // Get the window of the tool.
    HWND hwndTool = GetDlgItem(hDlg, toolID);
    if (!hwndTool)
    {
        return NULL;
    }                              

    // Create the tooltip. g_hInst is the global instance handle.
    HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                              WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              hDlg, NULL, 
                              hInst, NULL);

   if (!hwndTip)
   {
       return NULL;
   }                              

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hDlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = pszText;
    if (!SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo))
    {
        DestroyWindow(hwndTip);
        return NULL;
    }

    return hwndTip;
}

case WM_CREATE:
{
    Button = CreateWindowEx( 0, L"BUTTON", L"My Button", WS_VISIBLE|WS_CHILD, 10, 10, 100, 24, hWnd, (HMENU)ID_TOOLTIP, hInst, NULL); 

    tooltip_mess = CreateToolTip(ID_TOOLTIP, hWnd, hInst, L"Tooltip message");

    break;
}

if (tooltip_mess)
    SendMessage(tooltip_mess, TTM_ACTIVATE, TRUE, 0);

if (tooltip_mess)
    SendMessage(tooltip_mess, TTM_ACTIVATE, FALSE, 0);

关于c++ - 按钮的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19667854/

相关文章:

c++ - 尝试运行/编译我的 C++ 程序

c++ - 一些涉及类析构函数和删除运算符的内存管理问题?

c++ - 如何使用 SDL2 为每个线程设置一个共享的 OpenGL 上下文?

c - 如果进程终止但系统继续运行,WriteFile() 会是原子的吗?

c++ - 为什么 ShellExecute 找不到文件?

c++ - Opencv 行方法有一个奇怪的行为

c++ - g++ 在编译时挂起

winapi - 为多个监视器处理 WM_GETMINMAXINFO

c++ - 如何按需显示 shell notifyicon 气球工具提示?

c++ - 导出 outlook 联系人