I. Introduction▲
Code d'un Tout noir tout blanc en pure Windows API.
Le but du jeu est de mettre toutes les cases en blanc. Sachant que lorsque l'on clique sur une case elle change de couleur (du noir au blanc ou vice versa) idem pour les cases adjacentes au nord, sud, est et ouest de la case cliquée.
II. Code complet▲
Sélectionnez
#include <windows.h>
#define LARG 64
/* Tableau */
#define NBC_X 7
#define NBC_Y 7
/* ID de menu */
#define IDM_QUIT 1
#define IDM_NEW 2
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
HINSTANCE hinst;
LRESULT CALLBACK MainWndProc
(
HWND, UINT, WPARAM, LPARAM);
BOOL APIENTRY DlgProc
(
HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam);
void
Play
(
int
tab[NBC_Y][NBC_X], int
y, int
x);
int
WINAPI WinMain
(
HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int
nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wc;
HMENU hMenu, hSousMenu;
RECT winRect;
hinst =
hinstance;
wc.style =
0
;
wc.lpfnWndProc =
MainWndProc;
wc.cbClsExtra =
0
;
wc.cbWndExtra =
0
;
wc.hInstance =
hinstance;
wc.hIcon =
LoadIcon
(
NULL
, IDI_APPLICATION);
wc.hCursor =
LoadCursor
(
NULL
, IDC_ARROW);
wc.hbrBackground =
(
HBRUSH)(
COLOR_WINDOW+
1
);
wc.lpszMenuName =
NULL
;
wc.lpszClassName =
"
MaWinClass
"
;
if
(!
RegisterClass
(&
wc)) return
FALSE;
hSousMenu =
CreateMenu
(
);
AppendMenu
(
hSousMenu, MF_STRING, IDM_NEW, "
Nouvelle partie
"
);
AppendMenu
(
hSousMenu, MF_MENUBREAK, -
1
, NULL
);
AppendMenu
(
hSousMenu, MF_STRING, IDM_QUIT, "
Quitter
"
);
hMenu =
CreateMenu
(
);
AppendMenu
(
hMenu,MF_POPUP,(
UINT)hSousMenu,"
Fichier
"
);
SetRect
(&
winRect, 0
, 0
, LARG*
NBC_X, LARG*
NBC_Y);
AdjustWindowRect
(&
winRect, WS_CAPTION, TRUE);
hwnd =
CreateWindow
(
"
MaWinClass
"
, "
Black And White
"
,
WS_SYSMENU |
WS_CAPTION |
WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT,
winRect.right-
winRect.left, winRect.bottom-
winRect.top,
NULL
, hMenu, hinstance, NULL
);
if
(!
hwnd) return
FALSE;
ShowWindow
(
hwnd, nCmdShow);
UpdateWindow
(
hwnd);
while
(
GetMessage
(&
msg, NULL
, 0
, 0
)) DispatchMessage
(&
msg);
return
msg.wParam;
}
/**
***************************************************************************
*/
void
Play
(
int
tab[NBC_Y][NBC_X], int
y, int
x)
{
tab[y][x]=++
tab[y][x]&
1
;
if
(
y>
0
) tab[y-
1
][x]=++
tab[y-
1
][x]&
1
;
if
(
x>
0
) tab[y][x-
1
]=++
tab[y][x-
1
]&
1
;
if
(
y<
NBC_Y-
1
) tab[y+
1
][x]=++
tab[y+
1
][x]&
1
;
if
(
x<
NBC_X-
1
) tab[y][x+
1
]=++
tab[y][x+
1
]&
1
;
}
/**
***************************************************************************
*/
LRESULT CALLBACK MainWndProc
(
HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static
int
tab[NBC_Y][NBC_X];
switch
(
uMsg)
{
case
WM_COMMAND:
if
(
LOWORD
(
wParam) ==
IDM_QUIT) PostMessage
(
hwnd, WM_CLOSE,0
,0
);
if
(
LOWORD
(
wParam) ==
IDM_NEW)
{
ZeroMemory
(
tab, sizeof
(
tab));
InvalidateRect
(
hwnd, NULL
, FALSE);
}
return
0
;
case
WM_LBUTTONUP:
{
int
x =
GET_X_LPARAM
(
lParam)/
LARG;
int
y =
GET_Y_LPARAM
(
lParam)/
LARG;
if
(
x<
NBC_X &&
y<
NBC_Y) Play
(
tab, y, x);
InvalidateRect
(
hwnd, NULL
, FALSE);
return
0
;
}
case
WM_PAINT:
{
int
i, j;
HPEN hpen;
HDC hdc, hdcDB;
HBITMAP bmDB;
HGDIOBJ hobjold;
PAINTSTRUCT ps;
RECT rect, caserect;
HBRUSH hbcase =
CreateSolidBrush
(
0x00000000
);
hpen =
CreatePen
(
PS_SOLID, 1
, 0x00000000
);
GetClientRect
(
hwnd, &
rect);
hdc =
BeginPaint
(
hwnd, &
ps);
hdcDB =
CreateCompatibleDC
(
hdc);
bmDB =
CreateCompatibleBitmap
(
hdc, rect.right, rect.bottom);
hobjold =
SelectObject
(
hdcDB, bmDB);
SelectObject
(
hdcDB,hpen);
FillRect
(
hdcDB, &
rect, (
HBRUSH)(
COLOR_3DFACE+
1
));
for
(
j=
0
; j<
NBC_Y; j++
)
for
(
i=
0
; i<
NBC_X; i++
)
{
int
value =
tab[j][i];
caserect.left =
i*
LARG+
1
;
caserect.right =
i*
LARG+
LARG;
caserect.top =
j*
LARG+
1
;
caserect.bottom =
j*
LARG+
LARG;
Rectangle
(
hdcDB, i*
LARG, j*
LARG,
i*
LARG+
LARG+
1
, j*
LARG+
LARG+
1
);
if
(
value==
0
) /* Cases noires*/
{
caserect.left++
;
caserect.right--
;
caserect.top++
;
caserect.bottom--
;
FillRect
(
hdcDB, &
caserect, hbcase);
}
}
BitBlt
(
hdc, 0
, 0
, rect.right, rect.bottom, hdcDB, 0
, 0
, SRCCOPY);
DeleteDC
(
hdcDB);
DeleteObject
(
bmDB);
EndPaint
(
hwnd, &
ps);
DeleteObject
(
hbcase);
DeleteObject
(
hpen);
return
0
;
}
case
WM_DESTROY:
PostQuitMessage
(
0
);
return
0
;
default
:
return
DefWindowProc
(
hwnd, uMsg, wParam, lParam);
}
}
À vos compilateurs.