Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在winapi中使用工具提示?_C_Visual Studio 2010_Winapi - Fatal编程技术网

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

C 如何在winapi中使用工具提示?,c,visual-studio-2010,winapi,C,Visual Studio 2010,Winapi,我曾尝试在winapi中使用工具提示,但不起作用!这是我的代码,我的工具提示不显示!你能告诉我为什么它不起作用吗?我正在使用VisualStudio2010 #include <windows.h> #include <commctrl.h> #pragma comment(linker,"\"/manifestdependency:type='win32' \ name='Microsoft.Windows.Common-Controls' version='6.0.

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

#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);
}       
#包括
#包括
#pragma注释(链接器“\”/manifestdependency:type='win32'\
name='Microsoft.Windows.Common控件'version='6.0.0.0'\
processorArchitecture='*'publicKeyToken='6595b64144ccf1df'语言='*'\“”)
LRESULT回调WndProc2(HWND、UINT、WPARAM、LPARAM);
作废CreateMyTooltip(HWND);
HWND CreateToolTip(HWND hDlg,int tooID);
无效添加工具提示(int toolID、PTSTR pszText、HWND hDlg);
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
LPSTR lpCmdLine,int nCmdShow)
{
味精;
WNDCLASS wc={0};
wc.lpszClassName=L“工具提示”;
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc2;
wc.hCursor=加载光标(0,IDC_箭头);
注册类(&wc);
CreateWindow(wc.lpszClassName,L“工具提示”,
WS|U重叠窗口| WS|U可见,
100、100、200、150、0、0、0);
hinst-hInstance;
while(GetMessage(&msg,NULL,0,0)){
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}
LRESULT回调WndProc2(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
开关(msg)
{
案例WM_创建:
创建我的工具提示(hwnd);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
}
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
无效CreateMyTooltip(HWND HWND)
{
INITCOMMONCONTROLSEX iccex;
HWND-hwndt;
工具信息技术;
wchar_t工具提示[30]=L“主窗口”;
RECT-RECT;
iccex.dwICC=ICC_WIN95_类;
iccex.dwSize=sizeof(INITCOMMONCONTROLSEX);
InitCommonControlsEx(&iccex);
hwndTT=CreateWindowEx(WS_EX_TOPMOST,工具提示_类,空,
WS|u POPUP | TTS|u NOPREFIX | TTS|u ALWAYSTIP,
0,0,0,0,hwnd,NULL,hinst,NULL);
设置窗口位置(hwndTT、HWND_最上面、0、0、0、,
SWP|u NOMOVE | SWP|u NOSIZE | SWP|u NOACTIVATE);
GetClientRect(hwnd和rect);
ti.cbSize=sizeof(工具信息);
ti.uFlags=TTF_子类| TTF_IDISHWND;
ti.hwnd=hwnd;
ti.hinst=NULL;
ti.uId=0;
ti.lpszText=工具提示;
ti.rect.left=rect.left;
ti.rect.top=rect.top;
ti.rect.right=rect.right;
ti.rect.bottom=rect.bottom;
发送消息(hwndTT、TTM_ADDTOOL、0、(LPRAM)(LPTOOLINFO)和ti);
发送消息(hwndTT、TTM_激活、真、空);
发送消息(hwndTT,TTM_弹出窗口,0,0);
}       

更新:

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

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

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

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);
}
你应该确保清单是正确的。如果这是错误的,那么你可以在这里遇到麻烦,在许多其他地方


这是一个古老的答案:(不推荐!)

如果manifest是错误的,您必须为
TTOOLINFO::sbSize
设置正确的值,在我的例子中,它恰好是
TTTOOLINFOW\u V2\u 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;
}
#包括
#包括
#pragma注释(链接器“\”/manifestdependency:type='win32'name='Microsoft.Windows.Common Controls'version='6.0.0.0'processorArchitecture='*'publicKeyToken='6595b64144ccf1df'language='*'\“”)
#pragma注释(lib,“comctl32.lib”)
辛辛斯特;
无效CreateMyTooltip(HWND hparent)
{
HWND hwndTT=CreateWindowEx(WS_EX_TOPMOST,工具提示_类,空,
WS|u POPUP | TTS|u NOPREFIX | TTS|u ALWAYSTIP,0,0,0,hparent,NULL,g|hinst,NULL);
TTTOOLINFO ti={0};
//ti.cbSize=sizeof(TTTOOLINFO);
//*********************************************************
//特定编译器选项的特定设置(Unicode/VC2013)
//*********************************************************
ti.cbSize=TTTOOLINFOW\u V2\u尺寸;
ti.uFlags=TTF_子类;
ti.hwnd=hparent;
ti.lpszText=文本(“工具提示字符串”);
GetClientRect(hparent和ti.rect);
如果(!SendMessage(hwndTT,TTM_ADDTOOL,0,(lpram)和ti))
MessageBox(0,文本(“失败:TTM_ADDTOOL”),0,0);
}
LRESULT回调WndProc(HWND HWND,UINT msg,WPARAM WPARAM,LPARAM LPARAM)
{
开关(msg)
{
案例WM_创建:
创建我的工具提示(hwnd);
打破
案例WM_销毁:
PostQuitMessage(0);
打破
违约:
返回DefWindowProc(hwnd、msg、wParam、lParam);
}
返回0;
}
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、LPSTR lpCmdLine、int nCmdShow)
{
g_hinst=hInstance;
WNDCLASS wc={0};
wc.lpszClassName=文本(“工具脚本测试”);
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc;
wc.hCursor=加载光标(0,IDC_箭头);
注册类(&wc);
CreateWindow(wc.lpszClassName,文本(“工具提示”),WS_重叠窗口| WS_可见,
100,100,200,150,0,0,g_hinst,0);
味精;
while(GetMessage(&msg,NULL,0,0))
{
翻译信息(&msg);
发送消息(&msg);
}
返回(int)msg.wParam;
}

添加一些错误检查并重试。程序未编译。我仍然编译成功!而不是错误,只是工具提示不出现!我们不知道“不工作”是什么意思。请更加努力地描述你希望发生的事情,以及实际发生的事情。请添加错误检查。请学习调试代码。是的,我的问题是我没有向工具提示控制窗口发送addtool消息:SendMessage(hwndTT,TTM_addtool,0,(LPARAM)(LPTOOLINFO)&ti);嗨,Shemirani,我有一个问题:我能控制工具提示的出现和拥有吗?@trantuan我更新了我的答案。请尝试修复您的舱单。然后是t