Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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)
{
	// Register the window class.
	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);

	// Create the window.

	HWND hwnd = CreateWindowEx(
		0,                              // Optional window styles.
		CLASS_NAME,                     // Window class
		L"Catch Me If You Can",    // Window text
		WS_OVERLAPPEDWINDOW,            // Window style

										// Size and position
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

		NULL,       // Parent window    
		NULL,       // Menu
		hInstance,  // Instance handle
		NULL        // Additional application data
	);

	if (hwnd == NULL)
	{
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);

	// Run the message loop.
	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_CREATE:
		
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_PAINT:
	{
		//InvalidateRect(hwnd, 0, TRUE);
		//px = 100, py = 100,i=0;
		
		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;         //or we could use  bar[i].x = 350-(i+1)*150/20;
		}
		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);
		}
		/*for (i = 0; i < 20; i++)
			Rectangle(hdc, bar[i].x, bar[i].y, bar[i].x + bar[i].width, bar[i].y + 20);*/
		EndPaint(hwnd, &ps);
	}
   
	}
	
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Posted
Updated 12-Apr-17 5:01am

1 solution

With that change you give the rectangle a different signed heigth and so the bottom line is drawn on the other side.

If you want to move the rectangle you must change the y-coordinate of the origin and not the heigth.

Play a bit the the Rectangle function to better visualize the changes.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900