Procédure de création d’un contrôle Onglet dans la fenêtre principale

L’exemple de cette section montre comment créer un contrôle Onglet et l’afficher dans la zone cliente de la fenêtre principale de l’application. L’application affiche une troisième fenêtre (un contrôle statique) dans la zone d’affichage du contrôle Onglet. La fenêtre parente positionne et dimensionne le contrôle Onglet et le contrôle statique lorsqu’elle traite le message WM_SIZE.

Cet exemple comporte sept onglets, un pour chaque jour de la semaine. Lorsque l’utilisateur sélectionne un onglet, l’application affiche le nom du jour correspondant dans le contrôle statique.

Bon à savoir

Technologies

Prérequis

  • C/C++
  • Programmation de l’interface utilisateur Windows

Instructions

Créer un contrôle Onglet dans la fenêtre principale

La fonction suivante crée le contrôle Onglet et ajoute un onglet pour chaque jour de la semaine. Les noms des jours sont définis en tant que ressources de chaîne, numérotés consécutivement à partir de IDS_SUNDAY (défini dans le fichier d’en-tête de ressource de l’application). La fenêtre parente et le contrôle Onglet doivent avoir le style de fenêtre WS_CLIPSIBLINGS. La fonction d’initialisation de l’application appelle cette fonction après avoir créé la fenêtre principale et le contrôle d’onglet.

#define DAYS_IN_WEEK 7

// Creates a tab control, sized to fit the specified parent window's client
//   area, and adds some tabs. 
// Returns the handle to the tab control. 
// hwndParent - parent window (the application's main window). 
// 
HWND DoCreateTabControl(HWND hwndParent) 
{ 
    RECT rcClient; 
    INITCOMMONCONTROLSEX icex;
    HWND hwndTab; 
    TCITEM tie; 
    int i; 
    TCHAR achTemp[256];  // Temporary buffer for strings.
 
    // Initialize common controls.
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC = ICC_TAB_CLASSES;
    InitCommonControlsEx(&icex);
    
    // Get the dimensions of the parent window's client area, and 
    // create a tab control child window of that size. Note that g_hInst
    // is the global instance handle.
    GetClientRect(hwndParent, &rcClient); 
    hwndTab = CreateWindow(WC_TABCONTROL, L"", 
        WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 
        0, 0, rcClient.right, rcClient.bottom, 
        hwndParent, NULL, g_hInst, NULL); 
    if (hwndTab == NULL)
    { 
        return NULL; 
    }
 
    // Add tabs for each day of the week. 
    tie.mask = TCIF_TEXT | TCIF_IMAGE; 
    tie.iImage = -1; 
    tie.pszText = achTemp; 
 
    for (i = 0; i < DAYS_IN_WEEK; i++) 
    { 
        // Load the day string from the string resources. Note that
        // g_hInst is the global instance handle.
        LoadString(g_hInst, IDS_SUNDAY + i, 
                achTemp, sizeof(achTemp) / sizeof(achTemp[0])); 
        if (TabCtrl_InsertItem(hwndTab, i, &tie) == -1) 
        { 
            DestroyWindow(hwndTab); 
            return NULL; 
        } 
    } 
    return hwndTab; 
} 

La fonction suivante crée le contrôle statique qui réside dans la zone d’affichage du contrôle Onglet. La fonction d’initialisation de l’application appelle cette fonction après avoir créé la fenêtre principale et le contrôle Onglet.

Notez que le contrôle statique est positionné dans la zone d’affichage du contrôle Onglet, mais qu’il est lui-même un frère du contrôle Onglet, et non un enfant. Cela permet au contrôle statique de participer à l’ordre de tabulation de la fenêtre parente partagée. Cela n’est pas significatif pour un contrôle statique, mais il s’agit d’une bonne pratique dans l’éventualité qu’il soit remplacé par un contrôle accessible au clavier tel qu’un bouton.

// Creates a child window (a static control) to occupy the tab control's 
//   display area. 
// Returns the handle to the static control. 
// hwndTab - handle of the tab control. 
// 
HWND DoCreateDisplayWindow(HWND hwndTab) 
{ 
    HWND hwndStatic = CreateWindow(WC_STATIC, L"", 
        WS_CHILD | WS_VISIBLE | WS_BORDER, 
        100, 100, 100, 100,        // Position and dimensions; example only.
        GetParent(hwndTab), NULL, g_hInst, // g_hInst is the global instance handle
        NULL); 
    return hwndStatic; 
}

Les exemples de fonctions suivants sont appelés à partir de la procédure de fenêtre de l’application. L’application appelle la fonction OnSize lors du traitement du message WM_SIZE pour positionner et dimensionner le contrôle Onglet afin qu’il corresponde à la zone cliente de la fenêtre principale.

Lorsqu’un onglet est sélectionné, le contrôle Onglet envoie un message WM_NOTIFY indiquant le code de notification TCN_SELCHANGE. La fonction OnNotify de l’application traite ce code de notification en définissant le texte du contrôle statique.

// Handles the WM_SIZE message for the main window by resizing the 
//   tab control. 
// hwndTab - handle of the tab control.
// lParam - the lParam parameter of the WM_SIZE message.
//
HRESULT OnSize(HWND hwndTab, LPARAM lParam)
{
    RECT rc; 

    if (hwndTab == NULL)
        return E_INVALIDARG;

    // Resize the tab control to fit the client are of main window.
     if (!SetWindowPos(hwndTab, HWND_TOP, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), SWP_SHOWWINDOW))
        return E_FAIL;

    return S_OK;
}

// Handles notifications from the tab control, as follows: 
//   TCN_SELCHANGING - always returns FALSE to allow the user to select a 
//     different tab.  
//   TCN_SELCHANGE - loads a string resource and displays it in a static 
//     control on the selected tab.
// hwndTab - handle of the tab control.
// hwndDisplay - handle of the static control. 
// lParam - the lParam parameter of the WM_NOTIFY message.
//
BOOL OnNotify(HWND hwndTab, HWND hwndDisplay, LPARAM lParam)
{
    TCHAR achTemp[256]; // temporary buffer for strings

    switch (((LPNMHDR)lParam)->code)
        {
            case TCN_SELCHANGING:
                {
                    // Return FALSE to allow the selection to change.
                    return FALSE;
                }

            case TCN_SELCHANGE:
                { 
                    int iPage = TabCtrl_GetCurSel(hwndTab); 

                    // Note that g_hInst is the global instance handle.
                    LoadString(g_hInst, IDS_SUNDAY + iPage, achTemp,
                        sizeof(achTemp) / sizeof(achTemp[0])); 
                    LRESULT result = SendMessage(hwndDisplay, WM_SETTEXT, 0,
                        (LPARAM) achTemp); 
                    break;
                } 
        }
        return TRUE;
}

Utilisation des contrôles Onglet

Démonstration des contrôles courants Windows (CppWindowsCommonControls)