gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:16:05 24 4
gpt4 key购买 nike

我尝试过在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com