need some help with a basic win32 api program

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 6232 Views - Last Post: 06 September 2008 - 02:57 PM Rate Topic: -----

#1 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

need some help with a basic win32 api program

Post icon  Posted 05 September 2008 - 01:17 PM

Hey guys, Im having a little trouble with my first foundations assignment. It involves making a window, having a equalateral triangle, square, and pentagon be painted on the screen, and when you press the up arrow, down arrow, etc. the pentagon itself will move in the direction of the arrow, when you press page up/down, the triangle will grow/shrink, and when you press the spacebar, all three shapes will change color.

I havent had any classes in this except intro to C, so I am completly lost as to what to do.

I have FINALLY gotten the shapes on the screen, that was hard enough. Now Im focusing on making the pentagon move up/down/left/right with the arrow keys. I know I need to have WM_KEYDOWN, and there will be if statements, for if up is pressed, down, etc. but I dont know how to increment the array and have it repaint it with the updated array of points.(which is what needs to be done right?)

Please help me, Im totally lost and my teacher doesnt respond to emails :angry:

This is my callback function that i have so far:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hDC;
	PAINTSTRUCT Ps;
	HPEN hPen;
	HPEN hPen2;
	HPEN hPen3;
	HFONT hFont;
	int i;
	
	static POINT PointArray[]={{375,450},{275,270},{175,450}};
	static POINT PointArray2[]={{900,339},{805,270},{710,339},{746,451},{864,451}};
	
	switch(msg)
	{
		case WM_PAINT:
			hDC=BeginPaint(hwnd,&Ps);
			hPen=CreatePen(PS_SOLID,10,RGB(20,20,200));
			
			SelectObject(hDC,hPen);
			Rectangle(hDC,455,270,635,450);
			DeleteObject(hPen);

			hPen2=CreatePen(PS_SOLID,10,RGB(200,20,20));
			SelectObject(hDC,hPen2);
			Polygon(hDC, PointArray, 3);
			DeleteObject(hPen2);
			

			hPen3=CreatePen(PS_SOLID,10,RGB(20,200,20));
			SelectObject(hDC,hPen3);
			Polygon(hDC, PointArray2, 5);
			DeleteObject(hPen3);

		case WM_KEYDOWN:
			if(wParam==VK_UP)
												{
												 }
			
			InvalidateRect(hwnd,NULL,true);
		break;

		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: need some help with a basic win32 api program

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 02:25 PM

I think you have the right idea, though there is an easier way to change the position of the shapes besides directly manipulating the array (which is simple for movement, but is more difficult for the growing/shrinking). There are functions that manipulate the current transformation matrix. Here would be the basic idea of using them:

// Set the graphics mode to advanced.  This needs to be called at the beginning
SetGraphicsMode(hDC, GM_ADVANCED);

// Save the current transformation state so that it can be reused later.
XFORM current;
GetWorldTransform(hDC, &current);

// Draw the pentagon.  Offset the origin.  To move the pentagon down, yoffset would be a negative number.
OffsetWindowOrgEx(hDC, xoffset, yoffset, NULL);

// Draw the pentagon using your current method.  The previous call will change the position without actually having
// to change the array.
........ // Your code here


// Restore the transformation state
SetWorldTransform(hDC, &current);

// Do another transform to draw the triangle  (If you want add 10% to the size each time, use 10 for xden and yden).
ScaleWindowExtEx(hDC, xnum, xden, ynum, yden, NULL);

// Do whatever to draw triangle

// Restore the transformation state
SetWorldTransform(hDC, &current);




This is the gist of it, anyway. This way, you will only need to manipulate the scales, offsets, or whatever.
Was This Post Helpful? 0
  • +
  • -

#3 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 02:48 PM

thank you very much for responding! however, I have never seen any of what you wrote there, he definitly never mentioned anything like that in class.

so when i set the graphocs mode, that happens where im declaring variables? and what is a transformation state?

now, when im offsetting the pentagons origin, is OffsetWindowOrgEx(hDC, xoffset, yoffset, NULL); supposed to be inside the WM_KEYDOWN and inside an if/else statemtn somehow? same with the triangle.
Was This Post Helpful? 0
  • +
  • -

#4 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 03:21 PM

The way the transformation mechanism works is that the current transformation is applied to all given points on all operations. If you've ever used a CAD program (such as AutoCAD), it's very similar to UCS (user coordinate system). But the idea is that I can manipulate the coordinate system so that my output is drawn with respect to the new coordinate system.

Here's a concrete example. The default coordinate system for Windows specifies that the origin (0,0) is at the upper left corner of the window. Coordinate transformation allows me to move that origin anywhere in the window. I could move it to the center, or to the bottom left corner. Everything I draw will be relative to that origin.

I can also rotate the origin, so that X extends upward, and Y extends left, if I rotate the origin 90 degrees. Again, this will affect everything I draw.

I can also scale the coordinate plane. So if I have a window that is 100 x 100, and I want to increase the size of something by 2x, I can change the coordinate scale so that it's interpreted as 50 x 50, where each unit means double.

And actually, with the scaling, I was incorrect. You should also change the offset of the origin before scaling, which that offset should be to the center of the shape you're drawing.

A transformation state is just the current effective transformation that is in place. If I move the origin to the center of the window, that is a part of the current transformation state. After I'm done drawing, I'd like to revert those changes to the coordinate system, which I can do by saving the transformation state before manipulating it, and restoring it from the stored version afterwords.

Quote

now, when im offsetting the pentagons origin, is OffsetWindowOrgEx(hDC, xoffset, yoffset, NULL); supposed to be inside the WM_KEYDOWN and inside an if/else statemtn somehow? same with the triangle.


You should do all of this when handling WM_PAINT. In WM_KEYDOWN, you should be manipulating some global or static variables that make reference to offset or scale factor (depending on the key), and the drawing routine in WM_PAINT should make reference to those variables. InvalidateRect(...) will cause the window to be repainted in the near future (a WM_PAINT message will get sent to your window proc when no other messages are in the queue.)
Was This Post Helpful? 0
  • +
  • -

#5 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:10 PM

when i put the SetGraphicsMode(hDC, GM_ADVANCED); into my code, i get a warning when compiling but when i try to debug it, i get an error message and im not able to open the window, how come that happens?

and also, i understand the application of being able to change the scale, and how it would make things larger/smaller. but why would i want to change where the origin is on the screen? if i do everything according to the origin being at the upper left corner, would that make everything a lot harder to do?

im sorry, but i know only the very very basic about programming. ive only had Intro to C so this is all very new for me.

This post has been edited by notchcrazy: 05 September 2008 - 06:17 PM

Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:10 PM

View Postnotchcrazy, on 5 Sep, 2008 - 09:10 PM, said:

when i put the SetGraphicsMode(hDC, GM_ADVANCED); into my code, i get a warning when compiling but when i try to debug it, i get an error message and im not able to open the window, how come that happens?


What is the warning?
Was This Post Helpful? 0
  • +
  • -

#7 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:19 PM

its a debug error, it says:

run-time check failure #- The variable 'hDC'is being used without being initialized
Was This Post Helpful? 0
  • +
  • -

#8 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:21 PM

Is your hDC=BeginPaint(hwnd, &ps) before your SetGraphicsMode? It needs to be, because you need the value of hDC before you can use that call.
Was This Post Helpful? 0
  • +
  • -

#9 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:25 PM

o ok, i was putting it where i declared the variables. whoops!

now i have another question, my teacher said we would need a lot of static variables to store the information about the shapes. how would i be using them here?
Was This Post Helpful? 0
  • +
  • -

#10 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:34 PM

Well, you would need static variables to retain the state of the shapes. For example, you need to retain the offset of pentagon and the scale factor of the shape. So you could declare those in your function. If the pentagon only needs to go up and down, you only need one integer variable to retain this. Same thing goes with the scale.

When you get a message indicating a key press, and need to modify the size or position of the shapes, you would modify these variables accordingly (adding/subtracting from the offset, and the same thing with the scale), and then call InvalidateRect as you are. Your WM_PAINT handler would reference these variables when drawing the shape (specifically, in the ScaleWindowExtEx and OffsetWindowOrgEx, as well as other transformation functions that you might end up using).

You could initialize the offset to zero, and add 10 to it when the up key is pressed, and subtract 10 when the down key is pressed. For scale, you could initialize that to 16 (assuming that you hardcode the xden and yden to 16), and add and subtract one to that for each up and down key press. Scaling is implemented as a fraction (xnum / xden), and your scale variable would simply represent xnum, with xden always being 16.

By the way, many of these functions can be found on MSDN, and most searches on Google for these function names will take you right there, which will give you the details as to what you're supposed to pass to it, and what it will return, as well as other effects.

This post has been edited by perfectly.insane: 05 September 2008 - 06:37 PM

Was This Post Helpful? 0
  • +
  • -

#11 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:40 PM

so the point array's i have for the polygon functions do not need to be static, since the functions you've given me will not actually change their values, it will just change the way they are interpreted?
Was This Post Helpful? 0
  • +
  • -

#12 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:42 PM

View Postnotchcrazy, on 5 Sep, 2008 - 09:40 PM, said:

so the point array's i have for the polygon functions do not need to be static, since the functions you've given me will not actually change their values, it will just change the way they are interpreted?


That is correct. However, there is no need to change the way it is, and it will be more efficient the way you have it now. You might want to make them const though.
Was This Post Helpful? 0
  • +
  • -

#13 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 06:49 PM

ok, so i think im actually starting to understand this a little!

so what ill be doing is initializing some int values for the offset and scale functions, which will be inside the WM_PAINT case, they will also be static because i will be altering them, so as long as no button is pressed, they will stay them same. then i will go to case WM_KEYDOWN and have if(wParam==VK_UP) then i will change the value of the staic int variable that i had like y=y+10, then call the invalidaterect so it then draws the sahpe again but with a different offset value? same with the scale?

if all that is right, then the only thing i dont get is how i specify that the offset or the scale will only be for one shape and not affect the others.
Was This Post Helpful? 0
  • +
  • -

#14 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 07:13 PM

Yes, you are correct about the usage of the variables.

To specify an individual offset for each shape, use multiple variables. Since you're calling SetWorldTransform in between each shape (with the original transform state acquired with GetWorldTransform), each transform will only affect the shape drawn, up until the next SetWorldTransform. The transforms affect what your drawing code does, so you can use different transforms for each shape to achieve the desired effect.
Was This Post Helpful? 0
  • +
  • -

#15 notchcrazy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-September 08

Re: need some help with a basic win32 api program

Posted 05 September 2008 - 07:46 PM

well i thought i was getting it...

im just really confused when the functions are supposed to be called and how they are exactly updated.

im trying right now to focus on the up arrow. so i declared a static int variable x before the switch statement. then, inside the switch statement and inside the case WM_PAINT, i set graphics mode, declare XFORM current, and call GetWorldTransform(hDC, &current). now there are three seperate chunks below that, one to draw the square, one for the triangle, and one for the pentagon. should there be 3 seperate SetWorldTransform(hDC, &current) for each of those? and if so, should the XFORM be different for each of them? (well only for the triangle and pentagon). if that is right, then i would have in the VK_UP condition current.eDy = (FLOAT) 75.0; to change the value of the vertical translation component? but if thats to change the position then what is the offset for?

im just really lost, comming from only a intro to C class to this i just really feel like it doesnt make sense
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2