[C++] Win32 PolylineTo() issue?

I'm using PolylineTo inside a for loop to draw a border. However, at the midpoint, the lines seem to jump up one line, but it stays the same height across, so it's more like it jumps up one and increases in height by one. The first line seems unchanged though. Below is the full WM_PAINT code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
case WM_PAINT:{
	HDC hdc;
	HGDIOBJ old;
	PAINTSTRUCT ps;
			
	RECT rect; GetClientRect(hwnd, &rect);
	int width=rect.right, color=280; //We add the first 40 later
			
	BeginPaint(hwnd, &ps);
	hdc=GetDC(hwnd);

	ofstream out("x.txt");
			
	for(int i=1; i<=4; i++){
		POINT pt[2]={0,i,rect.right,i};
		old=SelectObject(hdc, CreatePen(PS_SOLID, 1, RGB(color-=40, color, color)));
		out<< i << ": " << pt[0].x << " " << pt[0].y << " " << pt[1].x << " " << pt[1].y << endl;
		PolylineTo(hdc, pt, 2);
		SelectObject(hdc, old);
	}

	EndPaint(hwnd, &ps);
	break;
}


The output of that ofstream is:


1: 0 1 694 1
2: 0 2 694 2
3: 0 3 694 3
4: 0 4 694 4



Also, if you were to multiply the i's by 2 or something, you would find that it will still climb to the line above.

XP Pro, SP3, MSVC++ 2008 Express.

Any help would be appreciated.
Last edited on
A couple of things:

1. You don't use GetDC() to get the DC during WM_PAINT. You get it as the result of calling BeginPaint(). See http://msdn.microsoft.com/en-us/library/dd183362(VS.85).aspx.
2. You don't set your initial position with MoveToEx(). See http://msdn.microsoft.com/en-us/library/dd145069(VS.85).aspx. You should always explicitly set it before using functions that use this, like PolylineTo().
3. If you are trying to draw a box, use Rectangle(). See http://msdn.microsoft.com/en-us/library/dd162898(VS.85).aspx.
4. If you insist on using PolylineTo() to draw a rectangle, you must set the starting point using MoveToEx(), and then you should specify 4 points in total to calls to PolylineTo(). Since your FOR loop goes from 1 to 4, you are providing twice as much points as needed. Finally, x.txt should contain:

1: 694 1 694 YYY
2: 0 YYY 0 1


where YYY would be the desired height of the rectangle.
1. That doesn't really matter, does it? Nonetheless, I'd made that change already.
2. That's the solution, MoveToEx is what I was missing.
3. Nope, not drawing a rectangle, just happens to be in that shape. I'm actually making a gradient, I know there's an official way, but I havn't gotten that far yet. :)

Thanks for the help, MoveToEx() is exactly what I needed.
Topic archived. No new replies allowed.