Results 1 to 6 of 6

Thread: What is wrong with this...

  1. #1

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Hi!
    I'm new to C++ programming, and trying out the following code, but, becuase it is C++, it doesn't work! (being a little sarcastic???)
    Here it is:

    Code:
    // INCLUDES ////////////////////
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <windowsx.h>
    #include <stdio.h>
    #include <math.h>
    // DEFINES ////////////////////
    // defines for windows
    #define WINDOWS_CLASS_NAME "WINCLASS1"
    // GLOBALS ///////////////////
    HWND main_windows_handle = NULL; // save the window handle
    // FUNCTIONS ////////////////
    LRESULT CALLBACK WindowProc(HWND hwnd,
    									UINT msg, WPARAM wparam, LPARAM lparam)
    {
    // this is the main message handler of the system
    PAINTSTRUCT ps; // used in WM_AINT
    HDC hdc;
    // find out what the message is
    switch(msg)
    	{
    	case WM_CREATE:
    				{
    				// do initialization stuff here
    				return(0);
    				} break;
    	case WM_PAINT:
    				{
    				// simply validate the window
    				hdc = BeginPaint(hwnd,&ps);
    				EndPaint(hwnd,ps);
    				return(0);
    				} break;
    	case WM_DESTROYY:
    				{
    				// kill the application
    				PostQuitMessage(0);
    				return(0);
    				} break;
    	default: break;
    	} // end switch
    // process any messages that you didn't take care of
    return (DefWindowProc(hwnd,msg,wparam,lparam));
    } // end WinProc
    // WINMAIN ///////////////////
    int WINAPI WinMain(HINSTANCE hinstance,
    						HINSTABCE previnstance,
    						LPSTR lpcmdline,
    						int ncmdshow)
    {
    WNDCLASS winclass // this will hold the class you create
    HWND hwnd; // generic window handle
    MSG  msg; // generic message
    // first, fill in the window class structure
    winclass.style = CS_DBLCLKS | CS_OWNDC |
    					  CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WindowProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground = GetStockObject(BLACK_BRUSH);
    winclass.lpszMenuName = NULL;
    winclass.lpszClassName = WINDOW_CLASS_NAME;
    // register the following class
    if (!RegisterClass(&winclass))
    	return(0);
    // create window
    if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,
    		"Hello Carsten!", // Title
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Flags
    		0,0, // x,y
    		320,200, // width, height
    		NULL, // Handle to parent
    		NULL, // Handle to menu
    		hInstance, // instance
    		NULL))) // creation parameters
    return(0);
    // save the window handle in a global
    main_window_handle = hwnd;
    //enter main event loop
    while(1)
    		{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    		// test whether this is a quit
    			if /msg.message == WM_QUIT) break;
    			//translate any accelerator keys
    			TranslateMessage(&msg);
    			// send the message to the window proc
    			DispatchMessage(&msg);
    			} // end if
    		// main game processing here
    } // end while
    return(msg.wParam);
    } // end WinMain
    I get about 10 errors, I don't feel like writing here....
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Here's the corrected code, with changes commented:
    Code:
    // INCLUDES ////////////////////
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <windowsx.h>
    #include <stdio.h>
    #include <math.h>
    // DEFINES ////////////////////
    // defines for windows
    #define WINDOW_CLASS_NAME "WINCLASS1"       // <------ WINDOW not WINDOWS
    // GLOBALS ///////////////////
    HWND main_window_handle = NULL; // save the window handle       // <------ window not windows
    // FUNCTIONS ////////////////
    LRESULT CALLBACK WindowProc(HWND hwnd,
    									UINT msg, WPARAM wparam, LPARAM lparam)
    {
    // this is the main message handler of the system
    PAINTSTRUCT ps; // used in WM_AINT
    HDC hdc;
    // find out what the message is
    switch(msg)
    	{
    	case WM_CREATE:
    				{
    				// do initialization stuff here
    				return(0);
    				} break;
    	case WM_PAINT:
    				{
    				// simply validate the window
    				hdc = BeginPaint(hwnd,&ps);
    				EndPaint(hwnd,&ps);       // <------ needed a pointer
    				return(0);
    				} break;
    	case WM_DESTROY:                      // <------ spelling mistake
    				{
    				// kill the application
    				PostQuitMessage(0);
    				return(0);
    				} break;
    	default: break;
    	} // end switch
    // process any messages that you didn't take care of
    return (DefWindowProc(hwnd,msg,wparam,lparam));
    } // end WinProc
    // WINMAIN ///////////////////
    int WINAPI WinMain(HINSTANCE hinstance,
    						HINSTANCE previnstance,      // <------ spelling mistake
    						LPSTR lpcmdline,
    						int ncmdshow)
    {
    WNDCLASS winclass; // this will hold the class you create        // <------ missing semicolon
    HWND hwnd; // generic window handle
    MSG  msg; // generic message
    // first, fill in the window class structure
    winclass.style = CS_DBLCLKS | CS_OWNDC |
    					  CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc = WindowProc;
    winclass.cbClsExtra = 0;
    winclass.cbWndExtra = 0;
    winclass.hInstance = hinstance;
    winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);     // <------ void* needs an explicit cast  
    winclass.lpszMenuName = NULL;
    winclass.lpszClassName = WINDOW_CLASS_NAME;
    // register the following class
    if (!RegisterClass(&winclass))
    	return(0);
    // create window
    if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME,
    		"Hello Carsten!", // Title
    		WS_OVERLAPPEDWINDOW | WS_VISIBLE, // Flags
    		0,0, // x,y
    		320,200, // width, height
    		NULL, // Handle to parent
    		NULL, // Handle to menu
    		hinstance, // instance                           // <------ hinstance not hInstance
    		NULL))) // creation parameters
    return(0);
    // save the window handle in a global
    main_window_handle = hwnd;
    //enter main event loop
    while(1)
    		{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
    		{
    		// test whether this is a quit
    			if (msg.message == WM_QUIT) break;      // <------ typo
    			//translate any accelerator keys
    			TranslateMessage(&msg);
    			// send the message to the window proc
    			DispatchMessage(&msg);
    			} // end if
    		// main game processing here
    } // end while
    return(msg.wParam);
    } // end WinMain
    Harry.

    "From one thing, know ten thousand things."

  3. #3

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Thank you very much Harry! That did the job!
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    If you're new to C++, I wouldn't recommend you start off with Windows programs. Stick to console stuff until you know all the basics.
    Harry.

    "From one thing, know ten thousand things."

  5. #5

    Thread Starter
    Frenzied Member CyberCarsten's Avatar
    Join Date
    Sep 1999
    Location
    Aalborg Ø, Denmark
    Posts
    1,544
    Hi Harry!
    I have been making console programming for about a few months, reading billions of beginners tutorials, so I thought it was time to move up the ladder!
    I know Windows programming is hard in C++(nothing like old VB!! ) but I have to learn it!!!
    razor
    Software Engineer Student, Aalborg University, Denmark
    http://www.cs.auc.dk

    My email at AUC: will get a new email soon
    My website: http://www.razorsoftware.net


    Windows XP Pro/ Gentoo Linux (Laptop)
    Windows XP Pro (Home PC)

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay sounds like a good idea to try this out then
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width