cs_jfrancois
Messages postés
482
Date d'inscription
vendredi 26 août 2005
Statut
Membre
Dernière intervention
5 décembre 2009
2
26 avril 2008 à 15:47
Bonjour,
Voici un programme Windows minimaliste qui affiche une grille qui est un damier de 2 images (disponibles dans les ressources) :
#include <stdio.h>
#include <windows.h>
#include <commctrl.h>
// --- Bibliothèque pour "InitCommonControls()"
#pragma comment(lib,"comctl32.lib")
// --- Instance de l'application
static HINSTANCE g_hInstance = NULL;
// --- Prototypes des fonctions locales
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
// ===================
// Fonction principale
// ===================
int WINAPI WinMain
(
HINSTANCE hInstance // E:instance courante
,HINSTANCE hInstancePrev // E:instance précédente
,LPSTR pszCommandLine // E:ligne de commande
,int iDisplay // E:mode d'affichage
) // S:code retour
{
// --- Initialiser
g_hInstance = hInstance;
InitCommonControls();
static const char* szClassName = "GrilleImageClasse";
// --- Enregistrer la classe de la fenêtre
WNDCLASSEX WndClass;
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.style = 0;
WndClass.lpfnWndProc = WinProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = g_hInstance;
WndClass.hIcon = LoadIcon(g_hInstance,"ICONE_APP");
WndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
WndClass.lpszMenuName = NULL;
WndClass.lpszClassName = szClassName;
WndClass.hIconSm = NULL;
if (! RegisterClassEx(&WndClass)) return 0;
// --- Créer la fenêtre
HWND hWnd = CreateWindowEx
(
0
,szClassName
,"Grille d'images"
,WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX
,CW_USEDEFAULT,CW_USEDEFAULT // x,y
,CW_USEDEFAULT,CW_USEDEFAULT // largeur,hauteur
,(HWND)NULL
,(HMENU)NULL
,g_hInstance
,(LPVOID)NULL
);
if (hWnd == NULL) return 0;
ShowWindow(hWnd,iDisplay);
UpdateWindow(hWnd);
// --- Traiter les messages de la fenêtre
MSG Msg;
while (GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
// ==================================
// Traiter les messages de la fenêtre
// ==================================
LRESULT CALLBACK WinProc
(
HWND hWnd // E:handle de la fenêtre
,UINT uiMsg // E:message à traiter
,WPARAM wParam // E:paramètre WPARAM
,LPARAM lParam // E:paramètre LPARAM
) // S:code retour
{
static HWND hGrille = NULL;
static int iGrilleLargeur;
static int iGrilleHauteur;
static HBITMAP hImage1 = NULL;
static HBITMAP hImage2 = NULL;
static HDC hdcImage1 = NULL;
static HDC hdcImage2 = NULL;
static int iImageLargeur;
static int iImageHauteur;
switch (uiMsg)
{
// ----------------
// Créer la fenêtre
// ----------------
case WM_CREATE:
{
// --- Charger les 2 images
HDC hDC = GetDC(hWnd);
hdcImage1 = CreateCompatibleDC(hDC);
hImage1 = LoadBitmap(g_hInstance,(LPCTSTR)"IMAGE_1");
SelectObject(hdcImage1,hImage1);
hdcImage2 = CreateCompatibleDC(hDC);
hImage2 = LoadBitmap(g_hInstance,(LPCTSTR)"IMAGE_2");
SelectObject(hdcImage2,hImage2);
ReleaseDC(hWnd,hDC);
// --- Obtenir la taille des images
// --- (elles ont toutes la même taille !)
BITMAP Bitmap;
GetObject(hImage1,sizeof(BITMAP),&Bitmap);
iImageLargeur = Bitmap.bmWidth;
iImageHauteur = Bitmap.bmHeight;
// --- Obtenir la taille de la grille
// --- (nombre d'images entières en largeur et hauteur)
iGrilleLargeur = iImageLargeur * 5;
iGrilleHauteur = iImageHauteur * 4;
// --- Redimensionner la fenêtre
RECT Rect;
GetWindowRect(hWnd,&Rect);
int iBordX = GetSystemMetrics(SM_CXFIXEDFRAME);
int iBordY = GetSystemMetrics(SM_CYFIXEDFRAME);
int iTitre = GetSystemMetrics(SM_CYCAPTION);
int iMarge = 20; // marge autour de la grille
MoveWindow
(
hWnd
,Rect.left,Rect.top // même position sur l'écran
,iGrilleLargeur + (iMarge * 2) + (iBordX * 2) // largeur
,iGrilleHauteur + (iMarge * 2) + (iBordY * 2) + iTitre // hauteur
,TRUE
);
// --- Créer le contrôle statique de la grille
hGrille = CreateWindowEx
(
0
,"STATIC"
,""
,WS_VISIBLE | WS_CHILD | WS_BORDER
,iMarge,iMarge // x,y
,iGrilleLargeur,iGrilleHauteur // largeur,hauteur
,hWnd
,(HMENU)NULL
,g_hInstance
,NULL
);
}
return 0;
// -----------------
// Peindre la grille
// -----------------
case WM_PAINT :
{
// --- Début du remplissage
PAINTSTRUCT Paint;
HDC hDC = BeginPaint(hGrille,&Paint);
if (hDC == NULL) return 0;
// --- Remplir la grille avec une alternance des 2 images
int iLigne = 0;
for (int iY=0 ; iY
// ---------------------
// Quitter l'application
// ---------------------
case WM_DESTROY:
if (hImage1) DeleteObject((HGDIOBJ)hImage1);
if (hImage2) DeleteObject((HGDIOBJ)hImage2);
if (hdcImage1) DeleteDC(hdcImage1);
if (hdcImage2) DeleteDC(hdcImage2);
PostQuitMessage(0);
return 0;
}
// --- Retour du traitement par défaut
return DefWindowProc(hWnd,uiMsg,wParam,lParam);
}
Le fichier des ressources contient :
ICONE_APP ICON "icone.ico"
IMAGE_1 BITMAP "image 1.bmp"
IMAGE_2 BITMAP "image 2.bmp"
Jean-François