如何在winapi中使用工具提示?

时间:2015-06-24 07:25:10

标签: c visual-studio-2010 winapi

我曾尝试在winapi中使用工具提示,但它没有用!这是我的代码,我的工具提示没有显示!你能告诉我为什么它不起作用吗?我正在使用visual studio 2010.

#include <windows.h>
#include <commctrl.h>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);
void CreateMyTooltip(HWND);
HWND CreateToolTip(HWND hDlg, int tooID);
void AddToolTip(int toolID,  PTSTR pszText, HWND hDlg);
HINSTANCE hinst;

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow )
{
    MSG  msg ;    
    WNDCLASS wc = {0};
    wc.lpszClassName = L"Tooltip" ;
    wc.hInstance     = hInstance ;
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
    wc.lpfnWndProc   = WndProc2 ;
    wc.hCursor       = LoadCursor(0, IDC_ARROW);

    RegisterClass(&wc);
    CreateWindow( wc.lpszClassName, L"Tooltip",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        100, 100, 200, 150, 0, 0, hInstance, 0);  
    hinst - hInstance;
    while( GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int) msg.wParam;
}

LRESULT CALLBACK WndProc2( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch(msg)  
    {
    case WM_CREATE:
        CreateMyTooltip(hwnd);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void CreateMyTooltip (HWND hwnd)
{

    INITCOMMONCONTROLSEX iccex; 
    HWND hwndTT;                

    TOOLINFO ti;
    wchar_t tooltip[30] = L"A main window";
    RECT rect;                 

    iccex.dwICC = ICC_WIN95_CLASSES;
    iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    InitCommonControlsEx(&iccex);

    hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,
        WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,        
        0, 0, 0, 0, hwnd, NULL, hinst, NULL );

    SetWindowPos(hwndTT, HWND_TOPMOST, 0, 0, 0, 0,
        SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

    GetClientRect (hwnd, &rect);

    ti.cbSize = sizeof(TOOLINFO);
    ti.uFlags = TTF_SUBCLASS | TTF_IDISHWND;
    ti.hwnd = hwnd;
    ti.hinst = NULL;
    ti.uId = 0;
    ti.lpszText = tooltip;
    ti.rect.left = rect.left;    
    ti.rect.top = rect.top;
    ti.rect.right = rect.right;
    ti.rect.bottom = rect.bottom;

    SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti); 
    SendMessage(hwndTT, TTM_ACTIVATE, true, NULL);
    SendMessage(hwndTT, TTM_POPUP, 0, 0);
}       

1 个答案:

答案 0 :(得分:1)

<强>更新

清单似乎有错误。如果您使用的是Visual Studio,请转到项目属性:

项目 - &gt;属性 - &gt;链接器 - &gt;清单 - &gt;生成清单文件:是

获得正确的清单后,您可以正常调用该函数:

TTTOOLINFOW_V2_SIZE

您应确保清单正确无误。如果这是错的,那么你可以在这里和其他很多地方遇到麻烦。

这是旧答案:(不推荐!)

如果清单错误,您必须为#include <windows.h> #include <commctrl.h> #pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") #pragma comment(lib, "comctl32.lib") HINSTANCE g_hinst; void CreateMyTooltip(HWND hparent) { HWND hwndTT = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, 0, 0, 0, 0, hparent, NULL, g_hinst, NULL); TTTOOLINFO ti = { 0 }; //ti.cbSize = sizeof(TTTOOLINFO); //********************************************************* // Specific settings for specific compiler options (Unicode/VC2013) //********************************************************* ti.cbSize = TTTOOLINFOW_V2_SIZE; ti.uFlags = TTF_SUBCLASS; ti.hwnd = hparent; ti.lpszText = TEXT("Tooltip string"); GetClientRect(hparent, &ti.rect); if (!SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM)&ti)) MessageBox(0,TEXT("Failed: TTM_ADDTOOL"),0,0); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CREATE: CreateMyTooltip(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hinst = hInstance; WNDCLASS wc = { 0 }; wc.lpszClassName = TEXT("TooltipTest"); wc.hInstance = hInstance; wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpfnWndProc = WndProc; wc.hCursor = LoadCursor(0, IDC_ARROW); RegisterClass(&wc); CreateWindow(wc.lpszClassName, TEXT("Tooltip"), WS_OVERLAPPEDWINDOW|WS_VISIBLE, 100, 100, 200, 150, 0, 0, g_hinst, 0); MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } 设置正确的值,在我的情况下恰好是{{1}}

{{1}}