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

标签 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);
}       

最佳答案

更新:

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

项目 -> 属性 -> 链接器 -> list -> 生成 list 文件:是

一旦获得正确的 list ,就可以正常调用该函数:

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, 0, NULL);

    TTTOOLINFO ti = { 0 };
    ti.cbSize = sizeof(TTTOOLINFO);
    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);
}

您应该确保 list 正确。如果错误,那么您可能会在这里以及许多其他地方遇到麻烦。

<小时/>

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

如果 list 错误,您必须为 TTOOLINFO::sbSize 设置正确的值,在我的例子中恰好是 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;
}

关于c - 如何在winapi中使用工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31020298/

相关文章:

c - 使用定界符将一个命令与一组命令分开 #

c - 生成两个限制之间的随机数

c - MODULEENTRY32 谁该做什么?

visual-studio-2010 - Visual Studio 2010 在启动时崩溃,即使在安全模式下

asp.net - MySQL 数据访问层 : How do you store your queries?

c++ - 无法读取代码页不匹配的 FileVersionInfo

c - pthread_cond_wait 和 pthread_mutex_lock 的优先级?

c++ - 为什么原生 C++ 与 C++ 互操作性表现不佳?

c++ - Win32 : Storing Multi-Line Text in a Buffer

c - Win32 CreateWindowEx() 窗口图标显示不正确