initializing a rectangle

I am trying to write a program that displays a rectangle that moves when the user tells it to the code compiles and the rectangle appears but I get a runtime check failure telling me that the variable r1 is being used without being initialized r1 is the rectangle

here is the declaration of the rectangle
RECT r1, client;

here is where its painted
1
2
3
4
5
6
r1.left = client.left + 400;
	r1.right = (client.left + client.right) /2 + 105;
	r1.top = client.top + 200;
	r1.bottom = (client.top + client.bottom) /2 - 50;

		FillRect (hdc, &r1, b1);

and here is where the error occurs
1
2
3
4
if(x = r1.left);
	{
	r1.left + p; 
	}


What is happening and how do I fix it?
Last edited on
Well try initialising the rects to zero at the point of declaration
RECT r1 = {}, client = {};
If you still have an error, then you will need to post your code as is for a closer look.


Also:
1
2
3
4
if(x = r1.left); //Error?  semi-colon makes this an  empty  statement.
	{
	r1.left + p; //this statement has no effect.
	}
that fixed the error but the rectangle doesnt move its completely unresponsive to mouse and keyboard
If RECT is a class you made yourself, you need to use the parameterized ctor. If you didn't make it yourself, see if there is a parameterized ctor, and if there isn't use the class methods to create the rectangle.

As guestgulkan pointed out, your if statement doesn't do anything, it's essentially ignore. Remove the semicolon. Also, the code in the block of the if statement doesn't do anything either, you probably want an assignment in there as well. Try:
r1.left += p;
I removed the semi-colon, p is an integer and x is the location of the mouse cursor the Idea was to have the rectangle move by adding p to the location of its left side when the mouse approached it from the left this didnt work
Enough of this - post your code.
^^^ Agreed
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
111
112
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	static HBRUSH b1;
	RECT r1 = {}, client = {};
	int x;
	int y;
	int p = rand() % 1000;
	int q = rand() % 1000; 
	int move = 10;

	switch (message)
	{

	case WM_CREATE:
		{
			COLORREF c1 = RGB (140, 0, 140);
			b1 = CreateSolidBrush (c1);

			return 0;
		}
       
	case WM_PAINT:
		hdc = BeginPaint( hwnd, &ps );
		
	
	

               
		GetClientRect (hwnd, &client);

		r1.left = client.left + 390;
		r1.right = (client.left + client.right) /2 + 105;
		r1.top = client.top + 300;
		r1.bottom = (client.top + client.bottom) /2 - 50;

		FillRect (hdc, &r1, b1);
		
	case WM_MOUSEMOVE:
		x = LOWORD (wParam);
		y = HIWORD (wParam);
		
		if(x == r1.left)
		{
			r1.left + p; 
		}

		if(x == r1.right)
		{
			r1.left - p;
		}

		if(y == r1.top)
		{
			r1.top + q;
		}

		if(y == r1.bottom)
		{
			r1.top - q;
		}

	case WM_KEYDOWN:

		switch (wParam)
		{
		case VK_LEFT:
			r1.left - move;
			break;

		case VK_RIGHT:
			r1.left + move;
			break;

		case VK_UP:
			r1.top - move;
			break;

		case VK_DOWN:
			r1.top + move;
			break;

		case VK_ESCAPE:
		               r1.left = client.left + 390;
		               r1.right = (client.left + client.right) /2 + 105;
			r1.top = client.top + 300;
			r1.bottom = (client.top + client.bottom) /2 - 50;
			break;
		}

		return 0;



		EndPaint( hwnd, &ps );
		return 0;

	case WM_SIZE:
		

		return 0;
	case WM_DESTROY:
		

		PostQuitMessage (0);       
		return 0;
	}

	
	return DefWindowProc (hwnd, message, wParam, lParam);
}
You need to brush up on some basic C++ - especially it seems on operators.
For Example:
r1.top + move;
this does not do anything - it creates a value that is the result of adding move to r1.top but the result is not assigned to anything.

Also your WM_PAINT was incorrect, and the list goes on...

Well here is something to get you going -
Notice taht I have done nothing about the mouse movement part and
I have added no checks to stop the rectangle moving outside the window client area.
You can try doing those for yourself.
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	static HBRUSH b1;
    
	int RectWidth = 105;
    int RectHeight = 50;
    
    static RECT r1 = {}, client = {}; //technically the client rect does not need to be static
    static RECT DefPos ={}; //The Default rect position in the window
	
    
    int x;
	int y;
	int p = rand() % 1000;
	int q = rand() % 1000; 
	int move = 10;

	switch (message)
	{

	case WM_CREATE:
		{
			COLORREF c1 = RGB (140, 0, 140);
			b1 = CreateSolidBrush (c1);

            //Initialise the rectangle default position
         GetClientRect (hwnd, &client);
		DefPos.left = client.left + 390;
		DefPos.right = (client.left + client.right) /2 + RectWidth;
		DefPos.top = client.top + 300;
		DefPos.bottom = (client.top + client.bottom) /2 - RectHeight;

        //Copy default position to R1 also;
        r1 = DefPos;

			return 0;
		}
       
	case WM_PAINT:
		hdc = BeginPaint( hwnd, &ps );
               
		//Paint the rectangle
        FillRect (hdc, &r1, b1);

        EndPaint(hwnd,&ps);
        return 0;

		
	case WM_MOUSEMOVE:
		x = LOWORD (wParam);
		y = HIWORD (wParam);
		
		if(x == r1.left)
		{
			r1.left += p; 
		}

		if(x == r1.right)
		{
			r1.left -= p;
		}

		if(y == r1.top)
		{
			r1.top += q;
		}

		if(y == r1.bottom)
		{
			r1.top -= q;
		}

        return 0;


	case WM_KEYDOWN:

		switch (wParam)
		{
		case VK_LEFT:
			r1.left -= move;
            r1.right -= move;
			break;

		case VK_RIGHT:
			r1.left += move;
            r1.right += move;
			break;

		case VK_UP:
			r1.top -= move;
            r1.bottom -= move;
			break;

		case VK_DOWN:
			r1.top += move;
            r1.bottom += move;
			break;

		case VK_ESCAPE:
		      r1 = DefPos;
			break;
		}
        InvalidateRect(hwnd,NULL,TRUE);
        UpdateWindow(hwnd);

		return 0;




	case WM_SIZE:
		

		return 0;
	case WM_DESTROY:
		

		PostQuitMessage (0);       
		return 0;
	}

	
	return DefWindowProc (hwnd, message, wParam, lParam);
}
Last edited on
that works rather well, thank you
Topic archived. No new replies allowed.