【发布时间】:2015-06-24 07:25:09
【问题描述】:
我曾尝试在 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);
}
【问题讨论】:
-
添加一些错误检查并重试。
-
程序无法编译。
-
我还是编译成功了!而不是错误,只是工具提示没有出现!
-
我们不知道“不工作”是什么意思。请更加努力地描述您希望发生的事情以及实际发生的事情。请添加错误检查。请学会调试你的代码。
-
是的,我的情况是我没有向工具提示控制窗口发送 addtool 消息:SendMessage(hwndTT, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
标签: c visual-studio-2010 winapi