用于复选框的c++工具提示函数

C++ Tooltip function for checkbox

本文关键字:工具提示 函数 c++ 复选框 用于      更新时间:2023-10-16

实际上,我的目的是为复选框函数设置一个工具提示,尽管它似乎不起作用。:/

复选框在资源文件中使用格式BS_AUTOSTATE对每个复选框进行管理。虽然我想为这些复选框初始化一个工具提示,一个朋友建议使用下面的方法,

text = dialog_message(IDC_FILE);
SetWindowText(GetDlgItem(m_hWnd, IDC_FILE), text));

不幸的是,它不起作用。任何人都有其他的想法来实现没有任何其他依赖的工具提示。

From @andlab的链接:如何为控件或矩形区域创建工具提示:
使用工具提示控件

工具提示需要4.70或更高版本的通用控件。确保项目清单设置正确。

可以使用下面的CreateToolTip函数:

CreateToolTip(hWnd, hWndDialogItem, L"TESTING");

对话框示例:

#include "windows.h"
#include "resource.h"
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
void CreateToolTip(
    HWND hWndParent, /*HWND handle for the parent window, for example dialog box*/
    HWND hControlItem, /*HWND handle for the control item, for example checkbox*/
    PTSTR pszText /*text for the tool-tip*/)
{
    if (!hControlItem || !hWndParent || !pszText)
        return;
    // 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,
        hWndParent, NULL, GetModuleHandle(0), NULL);
    if (!hwndTip)
        return;
    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hWndParent;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hControlItem;
    toolInfo.lpszText = pszText;
    if (!SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo))
    {
        //OutputDebugString(L"TTM_ADDTOOL failednWrong project manifest!");
        MessageBox(0, TEXT("TTM_ADDTOOL failednWrong project manifest!"), 0, 0);
    }
}
BOOL CALLBACK DialogProc(HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_INITDIALOG:
    {
        HWND hDlgItem = GetDlgItem(hDlg, IDOK);
        if (hDlgItem)
        {
            CreateToolTip(hDlg, hDlgItem, L"TESTING");
        }
        else
        {
            MessageBox(0, TEXT("Cannot find dialog item with IDOK identifier"), 0, 0);
        }
        break;
    }
    case WM_COMMAND:
        switch (wp)
        {
        case IDOK:
            EndDialog(hDlg, wp);
            break;
        case IDCANCEL:
            EndDialog(hDlg, wp);
            break;
        }
    }
    return FALSE;
}
int APIENTRY wWinMain(HINSTANCE hinst, HINSTANCE, LPTSTR, int nCmdShow)
{
    DialogBox(hinst, MAKEINTRESOURCE(IDD_DIALOG1), 0, DialogProc);
    return 0;
}