?
Solved

TreeView tooltips with Win32 api (not mfc)

Posted on 2004-10-06
4
Medium Priority
?
1,489 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

800 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