Code:
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define MAX 261
LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS myWin;
MSG msg;
myWin.style = CS_VREDRAW | CS_HREDRAW;
myWin.lpfnWndProc = MainWindowProc;
myWin.cbClsExtra = 0;
myWin.cbWndExtra = 0;
myWin.hInstance = hInstance;
myWin.hIcon = LoadIcon (NULL, IDI_APPLICATION);
myWin.hCursor = LoadCursor (NULL, IDC_ARROW);
myWin.hbrBackground = (HBRUSH) (COLOR_SCROLLBAR + 1);
myWin.lpszMenuName = 0;
myWin.lpszClassName = "My First App.";
if (!RegisterClass (&myWin))
return 0;
HWND hwnd;
hwnd = CreateWindow ( "My First App.",
"SamLife, v1.0",
WS_OVERLAPPEDWINDOW,
20,
20,
MAX + 40,
MAX + 60,
NULL,
NULL,
hInstance,
NULL);
if (!hwnd)
return 0;
SetTimer(hwnd, 1, 1, NULL);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return 0;
}
HDC hdc = NULL;
LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static int gen1 = 0;
static int gen2 = 0;
static int gen3 = 0;
static short xp;
static short yp;
static int timerId = 3000;
static int currentGrid = 0;
int col = 0;
int sum = 0;
int i, j;
int temp_i, temp_j;
static unsigned char bias [8] = { 20, 50, 75, 15, 50, 10, 15, 20 };
static unsigned char grid [2] [MAX] [MAX];
register unsigned char temp;
switch (msg)
{
case WM_CREATE:
srand ((unsigned) time (NULL));
for (i = 0; i < MAX; i++)
for (j = 0; j < MAX; j++)
{
grid [0] [i] [j] = 0;
grid [1] [i] [j] = 0;
}
SetTimer (hwnd, timerId, 1, NULL);
break;
case WM_LBUTTONDOWN:
if (!hdc)
hdc = GetDC (hwnd);
if (hdc)
{
xp = LOWORD(lParam);
yp = HIWORD(lParam);
SetPixel (hdc, xp, yp, RGB (255, 0, 0));
grid [currentGrid] [yp - 20] [xp - 20] = 1;
}
break;
case WM_LBUTTONUP:
if(hdc)
{
ReleaseDC (hwnd, hdc);
hdc = NULL;
}
break;
case WM_MOUSEMOVE:
if(hdc)
{
short xp = LOWORD (lParam);
short yp = HIWORD (lParam);
SetPixel (hdc, xp, yp, RGB (255, 0, 0));
grid [currentGrid] [yp - 20] [xp - 20] = 1;
}
break;
case WM_MOVE:
case WM_SIZE:
case WM_PAINT:
hdc = GetDC (hwnd);
if (!hdc)
break;
for (i = 0; i < MAX; i++)
for (j = 0; j < MAX; j++)
{
if (grid [currentGrid] [i] [j])
SetPixel (hdc, j + 20, i + 20, RGB (255, 255, 255));
else
SetPixel (hdc, j + 20, i + 20, RGB (0, 0, 0));
}
ReleaseDC (hwnd, hdc);
hdc = NULL;
break;
case WM_CLOSE:
DestroyWindow (hwnd);
break;
case WM_DESTROY:
KillTimer (hwnd, timerId);
PostQuitMessage (0);;
break;
case WM_TIMER:
if (!hdc)
hdc = GetDC (hwnd);
if (hdc)
{
SetPixel (hdc, rand () % MAX, rand () % MAX, RGB (255, 0, 0));
ReleaseDC (hwnd, hdc);
hdc = NULL;
}
break;
}
return DefWindowProc (hwnd, msg, wParam, lParam);
}
I've even put in code so that it only takes the HDC if it is free. But I still can't drag and draw unless I comment out the entire WM_TIMER clause.