1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
//Variablen
//---------
static BOOL fState[FIELD_COUNT_X][FIELD_COUNT_Y];
static int cxBlock, cyBlock;
HBITMAP hBitmap;
BITMAP bitmap ;
HDC hdc, hdcBmp;
int x, y;
PAINTSTRUCT ps;
RECT rect;
// Nachrichten switchen
//-------------------
switch(message)
{
// Fenster wird erstellt
//----------------------
case WM_CREATE:
{
hBitmap = (HBITMAP)LoadImageW(NULL,L"bmp_top.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(hBitmap == NULL)
{
MessageBoxW(hwnd,TEXT("Bild konnte nicht geladen werden"),TEXT("Error"),0);
return -1;
}
hdcBmp = CreateCompatibleDC(hdc);
SelectObject(hdcBmp, hBitmap);
return 0;
}
// Fenstergröße wird verändert
//----------------------------
case WM_SIZE:
{
cxBlock = LOWORD(lParam) / FIELD_COUNT_X;
cyBlock = HIWORD(lParam) / FIELD_COUNT_Y;
return 0;
}
// Linke Maustaste gedrückt
//-------------------------
case WM_LBUTTONDOWN:
{
x = LOWORD(lParam) / cxBlock;
y = HIWORD(lParam) / cyBlock;
if(x < FIELD_COUNT_X && y < FIELD_COUNT_Y)
{
fState[x][y] ^= 1;
rect.left = x * cxBlock;
rect.top = y * cyBlock;
rect.right = (x + 1) * cxBlock;
rect.bottom = (y + 1) * cyBlock;
InvalidateRect(hwnd, &rect, FALSE);
}
else
{
MessageBeep(0);
}
return 0;
}
// System oder andere Anwendung will zeichnen
//-------------------------------------------
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
for(x = 0 ; x < FIELD_COUNT_X ; x++)
for(y = 0 ; y < FIELD_COUNT_Y ; y++)
{
Rectangle(hdc, x * cxBlock, y * cyBlock,
(x + 1) * cxBlock, (y +1) * cyBlock);
BitBlt(hdc, x * cxBlock, y * cyBlock, bitmap.bmWidth, bitmap.bmHeight,
hdcBmp, 0, 0, SRCCOPY) ;
if(fState[x][y])
{
MoveToEx(hdc, x * cxBlock, y * cyBlock, NULL);
LineTo (hdc,(x + 1) * cxBlock,(y + 1) * cyBlock);
MoveToEx(hdc, x * cxBlock,(y + 1) * cyBlock, NULL);
LineTo (hdc,(x + 1) * cxBlock, y * cyBlock);
}
}
EndPaint(hwnd, &ps);
return 0;
}
// Anfrage zum Beenden
//--------------------
case WM_DESTROY:
{
DeleteDC (hdcBmp);
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
}
// Den Rest der Standardwindowprozedur überlassen
//-----------------------------------------------
return DefWindowProc(hwnd, message, wParam, lParam);
}
|