?
Solved

TreeView tooltips with Win32 api (not mfc)

Posted on 2004-10-06
4
Medium Priority
?
1,542 Views
Last Modified: 2013-12-03
I'm attempting to get the ToolTips for a TreeView to work. I was able to intercept a message when the mouse hovers over the TreeView item, but I didn't know what to do next.
 
case WM_NOTIFY:
  pnmhdr = (LPNMHDR) lParam;
  // TreeView
  if (pnmhdr->hwndFrom == hWndTree) {
    if (pnmhdr->code == TVN_SELCHANGED || pnmhdr->code == TVN_KEYDOWN) {
      LoadDocument(lParam);
    }
    else if (code == TVN_GETINFOTIP) {  // Gets here
      NMLVGETINFOTIP* nmTip = (NMLVGETINFOTIP*)lParam;
      nmTip.pszText = greeting;  // set to "Hello"
      nmTip.cchTextMax = 20;
//      SendMessageA(????????????);
      }
  }
  break;// WM_NOTIFY
 
Once the TVN_GETINFOTIP message is handled, what next?  I was trying to just put up a "Hello" message at first. I've gotten something similar to work with MFC ListControl, but want to use native Win32 api calls.
0
Comment
Question by:ee-user
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12281948
The problem is this line:

   nmTip.pszText = greeting;  // set to "Hello"

nmTip.pszText is a pointer to a buffer that you should copy your new string into,
whereas all you're doing is making the pointer point elsewhere, which has no effect.
You need to do this instead:

   strcpy(nmTip.pszText, greeting);
0
 

Author Comment

by:ee-user
ID: 12315667
strcpy(nmTip->pszText, str);
results in access error.

With a List control, the infotip shows up with:
nmTip->pszText = str;
0
 
LVL 14

Accepted Solution

by:
RichieHindle earned 500 total points
ID: 12316981
Sorry, I got that completely wrong.  The real problem is here:

NMLVGETINFOTIP* nmTip = (NMLVGETINFOTIP*)lParam;

You're using NMLVGETINFOTIP (ListView) rather than NMTVGETINFOTIP (TreeView).  Change the 'L's to 'T's and you should see it working.

Here's a complete working sample:

/* treeview.c - compile with cl treeview.c -link user32.lib comctl32.lib */

#define STRICT
#include <windows.h>
#include <commctrl.h>
#include <string.h>

HWND g_mainWnd = NULL;
HWND g_treeWnd = NULL;
HANDLE g_hInst = NULL;

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hinstPrev, LPSTR lpszCmdLine, int nCmdShow)
{
    WNDCLASS wc;
    MSG msg;
    INITCOMMONCONTROLSEX iccex;
   
    g_hInst = hInstance;

    iccex.dwSize = sizeof(iccex);
    iccex.dwICC = ICC_WIN95_CLASSES ;
    InitCommonControlsEx(&iccex);
   
    memset(&wc, '\0', sizeof(wc));
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.hInstance   = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor  = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszClassName = "EE_TreeViewExample";
    RegisterClass(&wc);

    g_mainWnd = CreateWindowEx(0, "EE_TreeViewExample", "TreeView Example",
                               WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,  0, CW_USEDEFAULT, 0,
                               NULL, NULL, hInstance, NULL);
    ShowWindow(g_mainWnd, SW_SHOW);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
   
    return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    TVITEM tvi;
    TVINSERTSTRUCT tvins;
    LPNMHDR pnmhdr;
    char debugMessage[1000];
   
    switch (uMsg)
    {
    case WM_CREATE:
        g_treeWnd = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, "",
                                   WS_CHILD | WS_VISIBLE | TVS_INFOTIP,
                                   0, 0, 10, 10, hWnd, NULL, g_hInst, 0);
        memset(&tvi, '\0', sizeof(tvi));
        tvi.mask = TVIF_TEXT;
        tvi.pszText = "Hello world";
        tvi.cchTextMax = lstrlen(tvi.pszText);
        tvins.item = tvi;
        tvins.hInsertAfter = NULL;
        tvins.hParent = TVI_ROOT;
        SendMessage(g_treeWnd, TVM_INSERTITEM, 0, (LPARAM) (LPTVINSERTSTRUCT) &tvins);
        break;

    case WM_SIZE:
        MoveWindow(g_treeWnd, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
        break;

    case WM_NOTIFY:
        pnmhdr = (LPNMHDR) lParam;
        if (pnmhdr->hwndFrom == g_treeWnd && pnmhdr->code == TVN_GETINFOTIP)
        {
            NMTVGETINFOTIP* nmTip = (NMTVGETINFOTIP*)lParam;
            nmTip->pszText = "Hello";
            nmTip->cchTextMax = 5;
        }
       
        break;
     
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
   
    default:
        return (DefWindowProc(hWnd, uMsg, wParam, lParam));
    }
   
    return 0;
}
0
 

Author Comment

by:ee-user
ID: 12533307
THANKS!

Sorry to be slow checking your answer ... and realizing my error with using "LV" rather than "TV". That cleared up everything :-)

One of those "forehead, meet palm" blunders on my part :-(
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question