I'm writing a report on C++ in school as we speak/write, and i'm using Dev-c++ ( www.bloodshed.net ). It's free, easy to use and i haven't experienced any problems with code not working...
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam)
{
//static variables used to keep track of the balls position
static int dX = 5, dY = 5; //stores direction
static int x = 0, y = 0, oldX = 0, oldY = 0;//stores position
//device context and brush used for drawing
HDC hDC;
HBRUSH brush;
//find out which message is being sent
switch(nMsg)
{
case WM_CREATE:
//create the timer (0.02 seconds)
SetTimer(hWnd, 1, 20, NULL);
break;
case WM_TIMER: //when the timer goes off (only one)
//get the dc for drawing
hDC = GetDC(hWnd);
//use pure white
brush = (HBRUSH)SelectObject(hDC, GetStockObject(WHITE_BRUSH));
//fill a RECT object with the appropriate values
RECT temp;
temp.left = oldX;
temp.top = oldY;
temp.right = oldX + 30;
temp.bottom = oldY + 30;
//cover the old ellipse
FillRect(hDC, &temp, brush);
//get ready to draw the new ellipse
brush = (HBRUSH)SelectObject(hDC, GetStockObject(GRAY_BRUSH));
//draw it
Ellipse(hDC, x, y, 30 + x, 30 + y);
//update the values
oldX = x;
oldY = y;
//prep the new coordinates for next time
x += dX;
y += dY;
//get the window size and store it in rect
RECT rect;
GetClientRect(hWnd, &rect);
//if the circle is going off the edge then reverse its direction
if(x + 30 > rect.right || x < 0)
{
dX = -dX;
}
if(y + 30 > rect.bottom || y < 0)
{
dY = -dY;
}
//put the old brush back
SelectObject(hDC, brush);
//release the dc
ReleaseDC(hWnd, hDC);
break;
case WM_DESTROY:
//destroy the timer
KillTimer(hWnd, 1);
//end the program
PostQuitMessage(0);
break;
default:
//let Windows handle every other message
return(DefWindowProc(hWnd, nMsg, wParam, lParam));
}
return 0;
}
_
Btw, the code should work because it's from a C++ programming book made in 2001.
the last parameter of CreateWindowEx is LPVOID (void*) so you can't pass value 1 without typcasting it. Try changing it to 0 or NULL instead.
maybe you made a typo while you were copying from the book?
i'm pretty sure borland works fine with windows. maybe you forgot to link some libraries. what sort of errors did it give you?
also, If you can find a copy, get Visual C++. Probably Microsoft's best piece of work
Edited by the Author.
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam,
LPARAM lParam)
{
//static variables used to keep track of the balls position
static int dX = 5, dY = 5; //stores direction
static int x = 0, y = 0, oldX = 0, oldY = 0;//stores position
//device context and brush used for drawing
HDC hDC;
HBRUSH brush;
//find out which message is being sent
switch(nMsg)
{
case WM_CREATE:
//create the timer (0.02 seconds)
SetTimer(hWnd, 1, 20, NULL);
break;
case WM_TIMER: //when the timer goes off (only one)
//get the dc for drawing
hDC = GetDC(hWnd);
//use pure white
brush = (HBRUSH)SelectObject(hDC, GetStockObject(WHITE_BRUSH));
//fill a RECT object with the appropriate values
RECT temp;
temp.left = oldX;
temp.top = oldY;
temp.right = oldX + 30;
temp.bottom = oldY + 30;
//cover the old ellipse
FillRect(hDC, &temp, brush);
//get ready to draw the new ellipse
brush = (HBRUSH)SelectObject(hDC, GetStockObject(GRAY_BRUSH));
//draw it
Ellipse(hDC, x, y, 30 + x, 30 + y);
//update the values
oldX = x;
oldY = y;
//prep the new coordinates for next time
x += dX;
y += dY;
//get the window size and store it in rect
RECT rect;
GetClientRect(hWnd, &rect);
//if the circle is going off the edge then reverse its direction
if(x + 30 > rect.right || x < 0)
{
dX = -dX;
}
if(y + 30 > rect.bottom || y < 0)
{
dY = -dY;
}
//put the old brush back
SelectObject(hDC, brush);
//release the dc
ReleaseDC(hWnd, hDC);
break;
case WM_DESTROY:
//destroy the timer
KillTimer(hWnd, 1);
//end the program
PostQuitMessage(0);
break;
default:
//let Windows handle every other message
return(DefWindowProc(hWnd, nMsg, wParam, lParam));
}
The only header file used is windows.h. I tried doing another Windows program in it( a simple messagebox) which uses windows.h, but it worked fine then. So, the links to the libraries should be right.
I got different errors to that, I think mine's outdated... I'll look once I've got the newest version
edit: never mind, it was just being arsey that I didn't set it to a win32 project. these error messages are unbelievably useless
Ok, simple enough. In dev-cpp, NULL is typcasted to void*, Meaning whenever you use it, the compiler thinks you're trying to pass a pointer to an int parameter (in this case, parameter #1). Try 0 instead. This'd explain why MSVC has no troubles (MSVC doesn't typecast NULL)
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G
i just tried compiling the last code you (snerlin) wrote, and it compiles without a problem and the ball bounces around... only got 1 little error, something about null or whatever.
Create your own brush using CreateBrush(). I think it just takes a COLORREF (use the RGB(r,g,b) macro to make a colour value for that) but it wouldn't hurt to have a quick look on MSDN if it doesn't work for you.
Are you trying to make games with the windows api? you'd probably be best looking for a directx wrapper like Allegro or SDL
"Say you're hanging from a huge cliff at the top of mt. everest and a guy comes along and says he'll save you, and proceeds to throw religious pamphlets at you while simultaniously giving a sermon." - Dustin G