Creating a Tooltip Control

The following example creates a tooltip control and adds several tools to it. The example creates a grid of rectangles in the client area of a window and then uses the TTM_ADDTOOL message to add each rectangle to the tooltip control. Note that the window procedure for the owner of the tooltip control must handle mouse messages and pass them on to the tooltip control by using the TTM_RELAYEVENT message.

// DoCreateTooltip - creates a tooltip control and adds some tools

// to it.

// Returns the handle of the tooltip control if successful or NULL
// otherwise.

// hwndOwner - handle of the owner window

//

// Global variable

// g_hinst - handle of the application instance

extern HINSTANCE g_hinst;

HWND DoCreateTooltip(HWND hwndOwner)

{

HWND hwndTT; // handle of tooltip

int row, col; // rows and columns

TOOLINFO ti; // tool information

int id = 0; // offset to string identifiers

static char *szTips[NUM_TIPS] = // tip text

{

"Cut", "Copy", "Paste", "Undo", "Open", "Save"

};

// Ensure that the common control DLL is loaded, and create

// a tooltip control.

InitCommonControls();

hwndTT = CreateWindow(TOOLTIPS_CLASS, (LPSTR) NULL, TTS_ALWAYSTIP,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, (HMENU) NULL, g_hinst, NULL);

if (hwndTT == (HWND) NULL)

return (HWND) NULL;

// Divide the client area into a grid of rectangles, and add each

// rectangle to the tooltip.

for (row = 0; row < MAX_ROWS ; row++ )

for (col = 0; col < MAX_COLS; col++) {

ti.cbSize = sizeof(TOOLINFO);

ti.uFlags = 0;

ti.hwnd = hwndOwner;

ti.hinst = g_hinst;

ti.uId = (UINT) id;

ti.lpszText = (LPSTR) szTips[id++];

ti.rect.left = col * CX_COLUMN;

ti.rect.top = row * CY_ROW;

ti.rect.right = ti.rect.left + CX_COLUMN;

ti.rect.bottom = ti.rect.top + CY_ROW;

if (!SendMessage(hwndTT, TTM_ADDTOOL, 0,

(LPARAM) (LPTOOLINFO) &ti))

return NULL;

}

return hwndTT;

}