Introduction :
Dans ce chapitre, nous allons aborder le contrôle ToolTips.
On appelle contrôle ToolTips les bulles d'aide qui apparaissent quand on positionne
le curseur de la souris au dessus d'un contrôle.
Création et initialisation du contrôle :
Le contrôle ToolTips ce crée avec CreateWindow comme tout autres contrôles.
Il peut gérer plusieurs tips (bulle d'aide). En fait c'est le même contrôle qui est
caché, déplassé, rendu visible et ou son texte est modifié selon le contexte.
Pour chaque bulle d'aide on doit remplir une structure de type TOOLINFO
qui doit nottament être rempli avec le handle du contrôle à qui est affecté la bulle d'aide
mais aussi avec ses coordonnées.
Elle sera enregistré dans le contrôle Tooltips en lui envoyant cette structure par adresse
à l'aide d'un message TTM_ADDTOOL.
TOOLINFO ti;
RECT rect;
InitCommonControls();
hTTip = CreateWindow(TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, NULL, hInst, NULL);
GetClientRect (hwnd, &rect);
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hwnd;
ti.hinst = hInst;
ti.uId = 0;
ti.lpszText = "C'est la fenêtre principale.";
ti.rect.left = rect.left;
ti.rect.top = rect.top;
ti.rect.right = rect.right;
ti.rect.bottom = rect.bottom;
SendMessage(hTTip, TTM_ADDTOOL, 0, (LPARAM) &ti);
GetClientRect (hwnd, &rect);
ti.hwnd = hBtn;
ti.uId = 0;
ti.lpszText = "C'est un bouton, vous pouvez appuyer dessus.";
ti.rect.left = rect.left;
ti.rect.top = rect.top;
ti.rect.right = rect.right;
ti.rect.bottom = rect.bottom;
SendMessage(hTTip, TTM_ADDTOOL, 0, (LPARAM) &ti);
Code complet :
main.c :
#include <windows.h>
#include <commctrl.h>
LONG APIENTRY WndProc(HWND hwnd, UINT uMsg, UINT wParam, LONG lParam);
HINSTANCE hInst;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
HWND hwndMain ;
MSG msg;
WNDCLASS wc;
hInst = hInstance;
wc.style = 0;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
wc.lpszMenuName = MAKEINTRESOURCE(1); ;
wc.lpszClassName = "MainWndClass";
if (!RegisterClass(&wc)) return FALSE;
hwndMain = CreateWindow("MainWndClass", "WinApi",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
360, 280, NULL, NULL, hInstance, NULL);
ShowWindow(hwndMain, nCmdShow);
UpdateWindow(hwndMain);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LONG APIENTRY WndProc(HWND hwnd, UINT uMsg, UINT wParam, LONG lParam)
{
static HWND hBtn;
static HWND hTTip;
switch ( uMsg ) {
case WM_CREATE:
{
TOOLINFO ti;
RECT rect;
hBtn = CreateWindow("button", "Bouton", WS_CHILD | WS_VISIBLE,
100, 100, 96, 24, hwnd, NULL, hInst, NULL);
InitCommonControls();
hTTip = CreateWindow(TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_NOPREFIX | TTS_BALLOON,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, NULL, hInst, NULL);
SendMessage(hTTip, TTM_SETTITLE, TTI_INFO , (LPARAM)"Titre");
GetClientRect (hwnd, &rect);
ti.cbSize = sizeof(TOOLINFO);
ti.uFlags = TTF_SUBCLASS;
ti.hwnd = hwnd;
ti.hinst = hInst;
ti.uId = 0;
ti.lpszText = "C'est la fenêtre principale.";
ti.rect.left = rect.left;
ti.rect.top = rect.top;
ti.rect.right = rect.right;
ti.rect.bottom = rect.bottom;
SendMessage(hTTip, TTM_ADDTOOL, 0, (LPARAM) &ti);
GetClientRect (hwnd, &rect);
ti.hwnd = hBtn;
ti.uId = 0;
ti.lpszText = "C'est un bouton, vous pouvez appuyer dessus.";
ti.rect.left = rect.left;
ti.rect.top = rect.top;
ti.rect.right = rect.right;
ti.rect.bottom = rect.bottom;
SendMessage(hTTip, TTM_ADDTOOL, 0, (LPARAM) &ti);
return 0;
}
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
J'ai testé les compilations avec C++ Builder et DevC++
A vos PC.
CGi
|