開発者およびコミュニティの皆様へ
私は現在、GUI を使用した Win32 API を使用したスネーク ゲームに取り組んでいます。
WM_PAINT メッセージ内の WndProc() 関数で Snake をペイントしてから、Painting Context を作成して Rectangles を描画しています。
しかし問題は、Snake が動いていて、描画された Rectangles が消えないことです。そこで、WndProc() で関数 InvalidateRect() を呼び出して、ウィンドウを更新しました。それは機能していますが、ヘビで42ステップ後、ウィンドウが白くなり、ボタン(最小化、最大化など)も表示されることがあります
次に、CreateTimerQueueTimer() を使用して、InvalidRect() 呼び出しを TimerRoutine に入れます。問題は解決したように見えましたが、数分後に再び発生しました。コードは次のとおりです。
VOID CALLBACK TimerRoutine(HWND lpParam, BOOLEAN TimerOrWaitFired)
{
BoardEditing();
InvalidateRect(lpParam, NULL, TRUE);
}
...メインで:
CreateTimerQueueTimer(&hTimer, hTimerQueue, (WAITORTIMERCALLBACK)TimerRoutine, hWnd, 0, 100, 0);
... WndProc() では:
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
if (GameOver == 1) {
TextOut(hdc, 100, 100, gameover, _tcslen(gameover));
}
else {
HBRUSH brushrot = CreateSolidBrush(RGB(255, 0, 0));
HBRUSH brushgruen = CreateSolidBrush(RGB(0, 255, 0));
HBRUSH brushschwarz = CreateSolidBrush(RGB(0, 0, 0));
for (int y = 0; y < Board_Y; y++) {
for (int x = 0; x < Board_X; x++) {
RECT rect = { x * 30,y * 30,x * 30 + 29,y * 30 + 29 };
if (Board[y][x] == 1) {
FillRect(hdc, &rect, brushrot);
}
else if (Board[y][x] == 2) {
FillRect(hdc, &rect, brushgruen);
}
}
}
}
EndPaint(hWnd, &ps);
break;