IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

API Windows en C

Le contrôle ToolTips

Le contrôle ToolTips. ♪

Article lu   fois.

L'auteur

Profil ProSite personnel

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

I. 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.

Image non disponible

II. Création et initialisation du contrôle

Le contrôle ToolTips se crée avec CreateWindow comme tout autre contrôle. Il peut gérer plusieurs tips (bulle d'aide). En fait c'est le même contrôle qui est caché, déplacé, rendu visible et où son texte est modifié selon le contexte. Pour chaque bulle d'aide, on doit remplir une structure de type TOOLINFO qui doit notamment être remplie avec le handle du contrôle auquel est affectée la bulle d'aide, mais aussi avec ses coordonnées. Elle sera enregistrée dans le contrôle Tooltips en lui envoyant cette structure par adresse à l'aide d'un message TTM_ADDTOOL.

 
Sélectionnez
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
       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);

III. Code complet

III-A. main.c

 
Sélectionnez
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
#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++

À vos PC.

CGi

Retour au sommaire.

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Copyright © 2006 CGi. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents, images, etc. sans l'autorisation expresse de l'auteur. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.