Here in this program when I change
bar[19].y - 20
to
bar[19].y + 20
the rectangle now changes to white sheet moving upwards instead of rectangle moving upwards.What is the bug in the code please let me know?Thank you.
What I have tried:
#ifndef UNICODE
#define UNICODE
#endif
#include<windows.h>
#include<string.h>
int px = 100, py = 100,i = 0;
POINT m;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void check();
struct box { int x; int y; int width; }bar[20];
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"Game";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
HWND hwnd = CreateWindowEx(
0,
CLASS_NAME,
L"Catch Me If You Can",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void check()
{
GetCursorPos(&m);
if (m.x <10 && m.y <10) { px = 250; py = 250; }
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
HBRUSH cr = CreateSolidBrush(RGB(250, 125, 50));
HBRUSH wr = CreateSolidBrush(RGB(255,255,255));
HBRUSH bkfill = CreateSolidBrush(RGB(230, 230, 230));
HANDLE hold;
HPEN pen = CreatePen(PS_NULL, 0, 1);
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
SendMessage(hwnd, WM_CREATE, NULL, NULL);
FillRect(hdc, &ps.rcPaint, cr);
SelectObject(hdc, pen);
for (i = 0; i < 20; i++)
{
bar[i].y = 100 + i * 20;
bar[i].width = 2 * (i + 1) * 150 / 20;
bar[i].x = 250-bar[i].width/2;
}
SelectObject(hdc, wr);
Rectangle(hdc, bar[1].x, bar[1].y, bar[1].x + bar[1].width, bar[1].y - 20);
while (bar[19].y != 30)
{
SelectObject(hdc, cr);
Rectangle(hdc, bar[19].x, bar[19].y--, bar[19].x + bar[19].width, bar[19].y - 20);
SelectObject(hdc, wr);
Rectangle(hdc, bar[19].x, bar[19].y, bar[19].x + bar[19].width, bar[19].y - 20);
Sleep(4);
}
EndPaint(hwnd, &ps);
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}