level 1
wszwszwsz白羊
楼主
源代码:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_BUTTON 101
HINSTANCE g_hInst;
// 函数用于创建工具提示
HWND CreateToolTip(HWND hwndParent, HWND hwndTool, PTSTR pszText)
{
if (!hwndTool || !hwndParent || !pszText)
{
return NULL;
}
// 创建Tooltip控件
HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, NULL,
g_hInst, NULL);
if (!hwndTip) return NULL;
// 设置工具提示信息
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hwndParent;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hwndTool;
toolInfo.lpszText = pszText;
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
SendMessage(hwndTip, TTM_SETMAXTIPWIDTH, 0, 200); // 最大宽度
return hwndTip;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
HWND hButton = CreateWindow(L"BUTTON", L"BUTTON",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
100, 100, 100, 30,
hwnd, (HMENU)ID_BUTTON, g_hInst, NULL);
// 创建工具提示
TCHAR text[64] = L"This is a tooltip example.";
CreateToolTip(hwnd, hButton, text);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow)
{
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);
g_hInst = hInstance;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"SimpleWindowClass";
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0, wc.lpszClassName, L"ToolTip Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInstance, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
将鼠标指针移动到BUTTON上后,也没有出现Tooltip控件,有无大神解答一下为什么.....![[乖]](/static/emoticons/u4e56.png)
2025年01月22日 05点01分
1
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_BUTTON 101
HINSTANCE g_hInst;
// 函数用于创建工具提示
HWND CreateToolTip(HWND hwndParent, HWND hwndTool, PTSTR pszText)
{
if (!hwndTool || !hwndParent || !pszText)
{
return NULL;
}
// 创建Tooltip控件
HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
hwndParent, NULL,
g_hInst, NULL);
if (!hwndTip) return NULL;
// 设置工具提示信息
TOOLINFO toolInfo = { 0 };
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = hwndParent;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)hwndTool;
toolInfo.lpszText = pszText;
SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
SendMessage(hwndTip, TTM_SETMAXTIPWIDTH, 0, 200); // 最大宽度
return hwndTip;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
HWND hButton = CreateWindow(L"BUTTON", L"BUTTON",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
100, 100, 100, 30,
hwnd, (HMENU)ID_BUTTON, g_hInst, NULL);
// 创建工具提示
TCHAR text[64] = L"This is a tooltip example.";
CreateToolTip(hwnd, hButton, text);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR, int nCmdShow)
{
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&icex);
g_hInst = hInstance;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"SimpleWindowClass";
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0, wc.lpszClassName, L"ToolTip Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInstance, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
将鼠标指针移动到BUTTON上后,也没有出现Tooltip控件,有无大神解答一下为什么.....