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
I have a new problem. I'm trying to make it so I can move the circle around using the arrow keys, but for some reason my code won't move it. wParam doesn't seem to be returning anything.
/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";
bool startFrame=0;
int keyB;
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
640, /* The programs width */
480, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
I think you have to put keypress handling inside its own event (WM_KEYDOWN or something, it's probably on MSDN somewhere)
"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
How do you remember all this stuff?? Without TGF/MMF I'd be lost. C++ seems like it takes the memory of a computer. Maybe I'm missing the easy way to remember this crap. I've tried learning these other languages, I never can do it. Anyways, I'm done rambling now. lol
thinking is like pong, it's easy, but you miss sometimes.
In borland there is tickboxes when you create a new project and use VPL (or something like that) must be ticked for window apps to work. I dont know much about windows C++, i'm best at c hardware programming and know next to nothing about the above .
Liquix kitty -> If you learn programming from bottom up, it's a learning procedure. You take one thing at a time. Then stuff is usually easily remembered. That's why tutorials are great.