Sign in to follow this  
talin29

EXTREME frustration with tooltips and _TrackMouseEvent( ), please help

Recommended Posts

Hello, I've been experimenting with tooltips, trying to figure them out. I almost have it, except the tooltip doesn't "behave" like a normal tooltip. When in the client area, even if the mouse is stationary, it constantly updates the tooltip, so it never disappears. I think it's a problem with TRACKMOUSEEVENT in WM_MOUSEMOVE, but I don't know how to proceed from here. Here's the code from the whole program, compilable. Hopefully someone in the future can benefit from this as well. I've searched and experimented for 4 days, and cannot quite get this right. :( Any help would be greatly appreciated. Thanks! :) EDIT: I forgot to mention, that the popup delay and the time before it updates the tooltip again are linked. If I set dwHoverTime to 3000, of course it takes 3 seconds to popup, but it also updates again after 3 seconds. Before, when I was using createwindow and destroywindow when I'd update, it would constantly "blink" inside the client area cause it for some reason keeps updating continuously. Also, I noticed, when using balloontip tooltips, it appears upside down. :(
#include <windows.h>
#include <commctrl.h>

#define TTS_BALLOON 0x40
HWND hwndtooltip = 0;

BOOL createtooltipwindow(char *text)
{
	unsigned int uid = 0;
	TOOLINFO ti;
	POINT point;

	hwndtooltip = CreateWindowEx(WS_EX_TOPMOST,
								TOOLTIPS_CLASS,
								NULL,
								WS_POPUP,	
								CW_USEDEFAULT,
								CW_USEDEFAULT,
								CW_USEDEFAULT,
								CW_USEDEFAULT,
								NULL,
								NULL,
								NULL,
								NULL
							   );

	SendMessage(hwndtooltip,TTM_SETMAXTIPWIDTH,0,300); 

	// INITIALIZE MEMBERS OF THE TOOLINFO STRUCTURE
	ti.cbSize = sizeof(TOOLINFO);
	ti.uFlags = TTF_TRACK | TTF_SUBCLASS;
	ti.hwnd = NULL;
	ti.hinst = NULL;
	ti.uId = uid;
	//ti.lpszText = (LPSTR)(LPCSTR) "Test tooltip text\r\nTesting";
	ti.lpszText = (LPSTR)(LPCSTR) text;
    // ToolTip control will cover the whole window
	ti.rect.left = 0;
	ti.rect.top = 0;
	ti.rect.right = 0;
	ti.rect.bottom = 0;

	GetCursorPos(&point);
	SendMessage(hwndtooltip, TTM_ADDTOOL, 0, (LPARAM) (LPTOOLINFO) &ti);
	SendMessage(hwndtooltip, TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(point.x+15, point.y+15));
	SendMessage(hwndtooltip, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
	//SendMessage(hwndtooltip, TTM_SETDELAYTIME, (WPARAM) (DWORD)TTDT_AUTOPOP, (LPARAM) MAKELONG(3000, 0));
	return TRUE;
}

void updatetooltipwindow(char *text)
{
	unsigned int uid = 0;
	TOOLINFO ti;
	POINT point;

	ti.cbSize = sizeof(TOOLINFO);
	ti.uFlags = TTF_TRACK | TTF_SUBCLASS;
	ti.hwnd = NULL;
	ti.hinst = NULL;
	ti.uId = uid;
	ti.lpszText = (LPSTR)(LPCSTR) text;

	GetCursorPos(&point);
	SendMessage(hwndtooltip, TTM_TRACKPOSITION, 0, (LPARAM)MAKELONG(point.x+15, point.y+15));
	SendMessage(hwndtooltip, TTM_TRACKACTIVATE, true, (LPARAM)(LPTOOLINFO) &ti);
	SendMessage(hwndtooltip, TTM_UPDATETIPTEXT, 0, (LPARAM) (LPTOOLINFO)&ti);
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		case WM_CREATE:
		break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        case WM_NOTIFY:
            switch (((LPNMHDR)lParam)->code)
            {
            case TCN_SELCHANGE:
                {
                    break;
                } 
            }
        break;

		case WM_MOUSEHOVER:
			if(hwndtooltip == 0)
			{
			createtooltipwindow("Mouse hovering\r\nOver client area");
			}
			updatetooltipwindow("Mouse hovering\r\nOver client area");
			break;

		case WM_MOUSEMOVE:

			TRACKMOUSEEVENT tme ;
			tme.cbSize    = sizeof ( TRACKMOUSEEVENT ) ;
			tme.dwFlags   = TME_LEAVE | TME_HOVER ;
			tme.hwndTrack = hwnd;
			tme.dwHoverTime = 500;
			_TrackMouseEvent ( & tme ) ;
			break;

		case WM_LBUTTONDOWN:
			if(hwndtooltip == 0)
			{
				createtooltipwindow("Left mouse button pressed");
			}
			updatetooltipwindow("Left mouse button pressed");
			break;

		case WM_MOUSELEAVE:
			DestroyWindow(hwndtooltip);
			hwndtooltip = 0;
			break;

        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;
    
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "myWindowClass";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
 
    RegisterClassEx(&wc);
 
   
    hwnd = CreateWindowEx(
        NULL,
        "myWindowClass",
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 300, 250,
        NULL, NULL, hInstance, NULL);
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}





[Edited by - talin29 on August 13, 2007 11:58:03 PM]

Share this post


Link to post
Share on other sites
Nobody? I'm trying to figure out tooltips, because I want to use them in an owner-drawn area of a dialog, for use with cells or hex grids.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this