2013-10-29 96 views
2

我使用功能从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; 
} 

然后在WndProcWM_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, hInst, hWnd, (PTSTR)"Tooltip message");
但它不工作,我看不到我的提示,其中我做错了?

+1

你通过自身激活提示找到一条TTM_ACTIVATE消息? –

+0

SendMessage(tooltip_mess,TTM_ACTIVATE,TRUE,0);但相同的结果 – user2650128

+0

我需要在WM_NOTIFY中写入什么? – user2650128

回答

1

试试这个:

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); 
+0

尝试了这段代码,但写了tooltip_mess = CreateToolTip(ID_TOOLTIP,hInst,hWnd,L“Tooltip message”);没有hInst,如果我正确的功能只接收3个参数。不幸的是我看不到工具提示。哪里需要写“如果”? – user2650128

+0

我更新了我的答案。至于'if'语句,当你需要显示/隐藏按钮上的tooptip时,你可以将它们放在需要它们的地方。 –

+0

if(!SendMessage(hwndTip,TTM_ADDTOOL,0,(LPARAM)&toolInfo))always DestroyWindow它是正常的吗?我想做一些类似于工具栏的东西,当鼠标点击按钮时 - >显示工具提示。当我单击按钮时,我尝试显示工具提示,但出现错误提示“变量'tooltip_mess'正在被使用而未被初始化。” – user2650128