It would be simpler if you drew the tic-tac-toe board on the main window, instead of using nine child windows:
To make the code easier to maintain, break the code into functions.
Code:
#define SQUARE_BLANK 0
#define SQUARE_X 1
#define SQUARE_O 2
void DrawSquare(HDC hdc, int x, int y, int square){
switch(square){
case SQUARE_BLANK:
// draw a blank square
break;
case SQUARE_X:
// draw an X
break;
case SQUARE_O:
// draw an O
break;
}
}
// in the window procedure
// mouse click handler
case WM_LBUTTONDOWN:{
POINT pt;
int x,y;
GetCursorPos(&pt);
x=GetClickedColumn(hwnd,&pt); // app-defined function
y=GetClickedRow(hwnd,&pt); // app-defined function
squares[x][y]=CurrentSquareType;
// app-defined function - returns X, O, or BLANK
CurrentSquareType=GetNextSquare(CurrentSquareType);
break;
}
// drawing handler
case WM_PAINT:{
HDC hdc;
PAINTSTRUCT ps;
int i,j;
hdc=BeginPaint(hwnd,&ps);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
DrawSquare(hdc,i,j,squares[i][j]);
}
}
EndPaint(hwnd,&ps);
break;
}
Something like this to get you started. You shouldn't send or post a WM_CREATE message to the window after its creation.
WM_COMMAND messages are usually processed through notifications by menus and standard Windows controls. Since this program is to be kept as simple as possible, no WM_COMMAND processing is necessary, unless you want to add a menu.
To exit the program, process the WM_CLOSE message by calling PostQuitMessage:
Code:
case WM_CLOSE:
PostQuitMessage(0);
break;