Creation toolbar et statusbar en win32 [vc++ 7.0]

Description

Si la compilation ne marche pas, il faut rajouter "comctl32.lib" dans les options du linker pour pouvoir gerer les controles internet.
Il s'agit juste de la creation d'une barre d'etat et d'une barre d'outils dans une fenetre windows avec affichage et destruction.

Source / Exemple :


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <commctrl.h>
#include "resource.h"

/* Identifiants */
#define ID_STATUSBAR	1
#define ID_TOOLBAR		2

/* Identifiants pour la barre d etat */
enum {
	ID_STATUS_ZONE_1,
	ID_STATUS_ZONE_2,
	ID_STATUS_ZONE_3,
	NB_STATUS_ZONE
};

/* Identifiant pour la barre d outils */
enum {
	ID_TOOLBAR_BTN_1,
	ID_TOOLBAR_BTN_2,
	NB_TOOLBAR_BTN
};

/* Composition de la barre d outils */
static TBBUTTON tbbtn[]= {
	{ID_TOOLBAR_BTN_1, ID_TOOLBAR_BTN_1, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0},
	{ID_TOOLBAR_BTN_2, ID_TOOLBAR_BTN_2, TBSTATE_ENABLED, TBSTYLE_BUTTON, 0L, 0}
};

HINSTANCE g_hInstance;
HWND CreateMainWindow(HINSTANCE hInstance);
HWND CreateStatusBar(HWND hParent);
HWND CreateToolBar(HWND hParent);
LRESULT APIENTRY MainWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam);

int WINAPI WinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
	HWND hWnd;
	MSG msg;

	g_hInstance = hInstance;

	/* Initialisation des controles */
	InitCommonControls();

	hWnd = CreateMainWindow(hInstance);

	if(hWnd == NULL)
		return EXIT_FAILURE;

	ShowWindow(hWnd, SW_MAXIMIZE);

	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return EXIT_SUCCESS;
}

HWND CreateMainWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wc;
	HWND hWnd;

	wc.cbSize		=	sizeof(WNDCLASSEX);
	wc.style		=	CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	=	(WNDPROC)MainWndProc;
	wc.cbClsExtra	=	0;
	wc.cbWndExtra	=	0;
	wc.hInstance	=	hInstance;
	wc.hIcon		=	LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));
	wc.hCursor		=	LoadCursor(NULL,IDC_ARROW);
	wc.hbrBackground	=	(HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName		=	MAKEINTRESOURCE(IDR_MENU1);
	wc.lpszClassName	=	"MainWnd";
	wc.hIconSm			=	LoadIcon(hInstance,MAKEINTRESOURCE(IDI_MAIN));

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL,
			"Enregistrement classe de fenêtre",
			"Erreur",
			MB_OK | MB_ICONERROR);

		return NULL;
	}

	hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,
		"MainWnd",
		"Application avec Toolbar et Statusbar",
		WS_OVERLAPPEDWINDOW,
		0,0,
		800,600,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(hWnd == NULL)
	{
		MessageBox(NULL,
			"Création de la fenêtre",
			"Erreur",
			MB_OK | MB_ICONERROR);

		return NULL;
	}

	return hWnd;
}

LRESULT APIENTRY MainWndProc(HWND hWnd, UINT iMsg, UINT wParam, LONG lParam)
{
	static HWND hToolbar = NULL;
	static HWND hStatusbar = NULL;
	static UINT iClick1 = 0;
	static UINT iClick2 = 0;

	switch(iMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(EXIT_SUCCESS);
		break;
	/* Message recu lors du redimmensionnement de la fenetre */
	case WM_WINDOWPOSCHANGED:
		/* On repositionne la barre d etat */
		SetWindowPos(hStatusbar,
			NULL,
			CW_USEDEFAULT,CW_USEDEFAULT,
			CW_USEDEFAULT,CW_USEDEFAULT,
			SWP_NOZORDER | SWP_NOMOVE);
		/* et la barre d'outils */
		SetWindowPos(hToolbar,
			NULL,
			CW_USEDEFAULT,CW_USEDEFAULT,
			CW_USEDEFAULT,CW_USEDEFAULT,
			SWP_NOZORDER | SWP_NOMOVE);
		break;
	case WM_CREATE:
		/* Apres creation de la fenetre on ajoute la barre d etat */
		/* et la barre d outil */
		hStatusbar = CreateStatusBar(hWnd);
		hToolbar = CreateToolBar(hWnd);
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDM_QUITTER:
			PostQuitMessage(EXIT_SUCCESS);
			break;
		/* Message du menu : bouton "Barre d'etat" */
		case IDM_STATUS:
			{
				UINT iState;

				/* On recupere l etat du bouton IDM_STATUS ("Barre d'etat") */
				iState = GetMenuState(GetMenu(hWnd),
					IDM_STATUS,
					MF_BYCOMMAND);

				if(iState == MF_CHECKED)
				{
					/* On vient de cocher le bouton */
					CheckMenuItem(GetMenu(hWnd),
						IDM_STATUS,
						MF_BYCOMMAND | MF_UNCHECKED);
					ShowWindow(hStatusbar, SW_HIDE);
				}
				else
				{
					/* On vient de decocher le bouton */
					CheckMenuItem(GetMenu(hWnd),
						IDM_STATUS,
						MF_BYCOMMAND | MF_CHECKED);
					ShowWindow(hStatusbar, SW_SHOW);
				}
			}
			break;
		/* Message du menu : bouton "Barre d'outils" */
		case IDM_TOOLBAR:
			{
				UINT iState;

				/* On recupere l etat du bouton IDM_TOOLBAR ("Barre d'outil") */
				iState = GetMenuState(GetMenu(hWnd),
					IDM_TOOLBAR,
					MF_BYCOMMAND);
				if(iState == MF_CHECKED)
				{
					/* On vient de cocher le bouton */
					CheckMenuItem(GetMenu(hWnd),
						IDM_TOOLBAR,
						MF_BYCOMMAND | MF_UNCHECKED);
					ShowWindow(hToolbar, SW_HIDE);
				}
				else
				{
					/* On vient de decocher le bouton */
					CheckMenuItem(GetMenu(hWnd),
						IDM_TOOLBAR,
						MF_BYCOMMAND | MF_CHECKED);
					ShowWindow(hToolbar, SW_SHOW);
				}
			}
			break;
		/* Message de la barre d'outils : Bouton 1 */
		case ID_TOOLBAR_BTN_1:
			{
				char cMsg[50];

				/* On increment le compteur de clic */
				++iClick1;

				sprintf(cMsg, "Nombre de clics sur 1 : %d", iClick1);

				/* On affiche le nombre de clic dans la barre d'etat */
				SendMessage(hStatusbar,
					SB_SETTEXT,
					ID_STATUS_ZONE_1,
					(LPARAM)(LPSTR)cMsg);
				/* On affiche le dernier bouton clique dans la barre d'etat */
				SendMessage(hStatusbar,
					SB_SETTEXT,
					ID_STATUS_ZONE_3,
					(LPARAM)(LPSTR)"Dernier clic sur 1");
			}
			break;
		/* Message de la barre d'outils : Bouton 2 */
		case ID_TOOLBAR_BTN_2:
			{
				char cMsg[50];

				/* On increment le compteur de clic */
				++iClick2;

				sprintf(cMsg, "Nombre de clics sur 2 : %d", iClick2);

				/* On affiche le nombre de clic dans la barre d'etat */
				SendMessage(hStatusbar,
					SB_SETTEXT,
					ID_STATUS_ZONE_2,
					(LPARAM)(LPSTR)cMsg);
				/* On affiche le dernier bouton clique dans la barre d'etat */
				SendMessage(hStatusbar,
					SB_SETTEXT,
					ID_STATUS_ZONE_3,
					(LPARAM)(LPSTR)"Dernier clic sur 2");
			}
			break;
		}
		break;
	default:
		return DefWindowProc(hWnd, iMsg, wParam, lParam);
	}

	return FALSE;
}

HWND CreateStatusBar(HWND hParent)
{
	HWND hStatusbar;
	int iStatusPart[NB_STATUS_ZONE];

	/* Creation de la barre d'etat */
	hStatusbar = CreateStatusWindow(WS_CHILD | WS_BORDER | WS_VISIBLE,
		NULL,
		hParent,
		ID_STATUSBAR);

	if(hStatusbar == NULL)
	{
		/* Erreur, on quitte l'application */
		MessageBox(hParent,
			"Création de la barre d'état\n"
			"L'application va se terminer",
			"Erreur",
			MB_OK | MB_ICONERROR);

		PostQuitMessage(EXIT_FAILURE);

		return NULL;
	}

	/* Definition des zones de la barre d'etat */
	/* Zone 1 de taille 300 */
	iStatusPart[ID_STATUS_ZONE_1] = 300;
	/* Zone 2 de taille 300 */
	iStatusPart[ID_STATUS_ZONE_2] = 600;
	/* Zone 3 : -1 pour utiliser toute la place restante */
	iStatusPart[ID_STATUS_ZONE_3] = -1;

	/* Decoupage de la barre d'etat */
	SendMessage(hStatusbar,
		SB_SETPARTS,
		(WPARAM)NB_STATUS_ZONE,
		(LPARAM)&iStatusPart);

	return hStatusbar;
}

HWND CreateToolBar(HWND hParent)
{
	HWND hToolbar;

	/* Creation de la barre d'outil */
	hToolbar = CreateToolbarEx(hParent,
		WS_CHILD | WS_VISIBLE,
		ID_TOOLBAR,
		NB_TOOLBAR_BTN,
		g_hInstance,
		IDB_BITMAP1,
		(LPCTBBUTTON) &tbbtn,
		NB_TOOLBAR_BTN,
		16,16,
		16,16,
		sizeof(TBBUTTON));

	if(hToolbar == NULL)
	{
		/* Erreur on quitte l'application */
		MessageBox(hParent,
			"Création de la barre d'outils\n"
			"L'application va se terminer",
			"Erreur",
			MB_OK | MB_ICONERROR);

		PostQuitMessage(EXIT_FAILURE);

		return NULL;
	}

	return hToolbar;
}

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.