是否有必要销毁工具提示?

发布于 2024-10-15 00:10:22 字数 1447 浏览 7 评论 0原文

在我的应用程序中,我正在处理 WM_HELP 消息,然后使用此方法为控件创建工具提示:

取自:http://msdn.microsoft.com/en-us/library/bb760252(v=vs.85).aspx

HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
    if (!toolID || !hDlg || !pszText)
    {
        return FALSE;
    }
    // Get the window of the tool.
    HWND hwndTool = GetDlgItem(hDlg, toolID);

    // Create the tooltip. g_hInst is the global instance handle.
    HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                              WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              hDlg, NULL, 
                              g_hInst, NULL);

   if (!hwndTool || !hwndTip)
   {
       return (HWND)NULL;
   }                              

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hDlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = pszText;
    SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);

    return hwndTip;
}

库 当我移动鼠标指针时,工具提示就会消失。

我的问题是:

  1. 工具提示是否已被破坏或只是隐藏了?
  2. 如果它是隐藏的那么如何销毁它以及何时销毁?

谢谢。

In my application I am handling the WM_HELP message and then creating a tooltip for a control using this method:

Taken from: http://msdn.microsoft.com/en-us/library/bb760252(v=vs.85).aspx

HWND CreateToolTip(int toolID, HWND hDlg, PTSTR pszText)
{
    if (!toolID || !hDlg || !pszText)
    {
        return FALSE;
    }
    // Get the window of the tool.
    HWND hwndTool = GetDlgItem(hDlg, toolID);

    // Create the tooltip. g_hInst is the global instance handle.
    HWND hwndTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL,
                              WS_POPUP |TTS_ALWAYSTIP | TTS_BALLOON,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              hDlg, NULL, 
                              g_hInst, NULL);

   if (!hwndTool || !hwndTip)
   {
       return (HWND)NULL;
   }                              

    // Associate the tooltip with the tool.
    TOOLINFO toolInfo = { 0 };
    toolInfo.cbSize = sizeof(toolInfo);
    toolInfo.hwnd = hDlg;
    toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    toolInfo.uId = (UINT_PTR)hwndTool;
    toolInfo.lpszText = pszText;
    SendMessage(hwndTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);

    return hwndTip;
}

The tooltip vanishes as soon as I move my mouse pointer.

My questions are:

  1. Is tooltip is destroyed or is it just hidden ?
  2. If it is hidden then how to destroy it and when?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城烟雨 2024-10-22 00:10:22

我已经有一段时间没有进行任何 WinAPI 编程了,但如果我没记错的话...

CreateWindowEx 的调用将 hDlg 作为 hWndParent 参数表示对话框窗口现在是工具提示的父窗口。

来自 DestroyWindow< 上的 MSDN 文档/code>函数它说:

如果指定的窗口是父窗口或所有者窗口,则 DestroyWindow 在销毁父窗口或所有者窗口时会自动销毁关联的子窗口或拥有的窗口。该函数首先销毁子窗口或拥有的窗口,然后销毁父窗口或所有者窗口。

因此,您可以假设您的工具提示窗口最终将被销毁。如果您调用 CreateToolTip 来响应每条 WM_HELP 消息,请务必小心,因为您最终会在内存中保留许多工具提示窗口,直到您的对话框关闭并且>DestroyWindow 最终被调用。

正如 vz0 指出的,您可以创建一次工具提示,挂在窗口句柄上,然后显示工具提示以响应帮助消息,而不是再次创建它。

在您对 vz0 答案的评论中您说:

工具提示有多种显示方式。例如:鼠标移动、超时等

所有这些只会导致窗口被隐藏,因此工具提示的句柄仍然有效,并且可以使用 ShowWindow 重新显示。

It's been a while since I've done any WinAPI programming but if my memory serves me...

The call to CreateWindowEx passes the hDlg as the hWndParent parameter meaning the dialog window is now the parent of the tooltip.

From the MSDN documentation on the DestroyWindow function it says:

If the specified window is a parent or owner window, DestroyWindow automatically destroys the associated child or owned windows when it destroys the parent or owner window. The function first destroys child or owned windows, and then it destroys the parent or owner window.

So you can assume your tooltip window will be destroyed eventually. Be careful if you are calling CreateToolTip in response to every WM_HELP message as you will end up with a number of tooltip windows hanging around in memory until your dialog is closed and DestroyWindow is finally called.

As vz0 pointed out you could create the tooltip once, hang on to the window handle, then show the tooltip in response to the help message rather than creating it again.

In your comment to vz0's answer you said:

there are multiple ways in which a tooltip goes awya. example: mouse move, timeout etc.

All of those only result in the window being hidden so the handle to the tooltip is still valid and can be redisplayed using ShowWindow.

假装爱人 2024-10-22 00:10:22

对于每个 CreateWindowEx 调用,您需要一个匹配 DestroyWindow 调用。

作为替代方案,您可以使用 ShowWindow 使用 SW_SHOWSW_HIDE 调用来显示和隐藏弹出窗口。

For every CreateWindowEx call you need a matching DestroyWindow call.

As an alternative, instead of creating and destroying the window every time you can use the ShowWindow call with SW_SHOW and SW_HIDE to show and hide the popup.

可爱暴击 2024-10-22 00:10:22

根据我的经验,我必须在工具提示上执行 DestroyWindow(),以便正确释放 HFONT(字体 GDI 资源)。两个窗口同时存在父子关系 - 但我的系统在运行时改变了这一点,这可能是罪魁祸首。如果你的系统能够概括它,那么这样做可能没有什么坏处。

In my experience, I had to DestroyWindow() on the tooltip so an HFONT (font GDI resource) was properly released. There was a parent-child bond of the two windows at one time - but my system changes this at run-time and could be to blame. Probably no harm in doing it if your system generalizes it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文