Thread: Classes and Win32 API, and another Question

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    72

    Arrow Classes and Win32 API, and another Question

    Hello,
    After programming in Win32 console c++, I feel most ready to start Win32 API. I got down making a blank window, some little text on the screen, but I'm aslo wondering if all C++ is available when using win32 API. Reason I ask is I'm wondering if Classes work as well. For example, the header has my class, and can I still make objects with it on the main source? Does it work like MFC?

    Also, I'm trying to make an object's variable show up on a message box. For example,

    MessageBox(NULL, PC.Age, "Message", MB_OK);

    I get the following Errors:
    error C2065: 'Player' : undeclared identifier
    error C2146: syntax error : missing ';' before identifier 'PC'
    error C2065: 'PC' : undeclared identifier
    error C2228: left of '.Age' must have class/struct/union type
    error C2228: left of '.Age' must have class/struct/union type


    Where do I define objects? In the switch() cases? Any help is appreciated. Thanks.
    Last edited by philvaira; 04-10-2004 at 01:09 PM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>I'm aslo wondering if all C++ is available when using win32 API<<

    Yes, limited only by your compiler's capabilities.

    >>Where do I define objects? In the switch() cases?<<

    Without seeing more code (and use code tags please) it's almost impossible to answer. Still, taking a wild guess that the 'switch' you're referring to is a message handling switch statement within a window procedure then you can declare 'Player' with static scope at the start of the window procedure. For simplicity/testing you may wish to declare the object instance with global scope.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    Code:
    switch(Msg)
    {
    	case WM_MBUTTONDOWN: 
                    Player PC; 
                    PC.Age = 22;
    	MessageBox(NULL, Player.Age, "Message", MB_OK);
    	 break;
    }

    There ya go. It says:
    'Player' : illegal use of this type as an expression

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Enclose the case statement in chicken lips:
    Code:
    case WM_MBUTTONDOWN: 
      { //<--- one of these...
      Player PC; 
      PC.Age = 22;
      MessageBox(NULL, Player.Age, "Message", MB_OK);
      break;
      } //<-- ... and another
    Note that the scope of the declaration (Player PC) will be local to that case statement; if you want to access the object elsewhere then you'll have to declare the object instance with static or global scope as I previously mentioned.
    Last edited by Ken Fitlike; 04-10-2004 at 04:56 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    Oh yeah, I remember the brackets purpose now. And yea, defining an object should be global, me thinks.

    Thanks.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    No matter what I do, it wont work. Here's the script..

    Code:
    #include <windows.h>
    
    
    const char g_szClassName[] = "myClassWindow";
     
    
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {
    
    	PAINTSTRUCT PaintStruct;
    	HDC hDC;
    
    
    	switch(Msg)
    	{
    		case WM_MBUTTONDOWN: 
    						  { //<--- one of these...
    					  	  Player PC;
    						  PC.Age = 22;
    						  MessageBox(NULL, Player.Age, "Message", MB_OK);
    						  break;
    						  } //<-- ... and another
    		case WM_PAINT:   hDC = BeginPaint(hWnd, &PaintStruct);
    					     TextOut(hDC, 10, 10, "Hi", 2);
    					     EndPaint(hWnd, &PaintStruct);
    					     break;
    		
    		case WM_CLOSE:   DestroyWindow(hWnd);
    					     break;
    		
    		case WM_DESTROY: PostQuitMessage(0);
    						 break;
    		default:         return DefWindowProc(hWnd, Msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hWnd;
    	MSG Msg;
    
    	// Registering Class Window
    	wc.cbClsExtra			= 0;
    	wc.cbSize				= sizeof(WNDCLASSEX);
    	wc.cbWndExtra			= 0;
    	wc.hbrBackground		= (HBRUSH) (COLOR_WINDOW+1);
    	wc.hCursor				= LoadCursor(NULL, IDC_ARROW);
    	wc.hIcon				= LoadIcon(NULL, IDI_APPLICATION);
    	wc.hIconSm				= LoadIcon(NULL, IDI_APPLICATION);
    	wc.hInstance			= hInstance;
    	wc.lpfnWndProc			= WndProc;
    	wc.lpszClassName		= g_szClassName;
    	wc.lpszMenuName			= NULL;
    	wc.style				= 0;
    
    	if (!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Registering Class Failed!", "Message", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	// Creating Window
    	hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "World of Dragons", WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
    
    	if (hWnd == NULL)
    	{
    		MessageBox(NULL, "Creating Window Failed!", "Message", MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	// Misc, but not last...
    
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	while (GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    
    	return Msg.wParam;
    }

    Header:

    Code:
    #include <iostream>
    
    class Player
    {
    public:
    	unsigned short int Age;
    };

    And last, our bugs:

    Code:
    C:\Program Files\Microsoft Visual Studio\MyProjects\WIN32Pract2\Main.cpp(5) : error C2146: syntax error : missing ';' before identifier 'PC'
    C:\Program Files\Microsoft Visual Studio\MyProjects\WIN32Pract2\Main.cpp(5) : error C2501: 'Player' : missing storage-class or type specifiers
    C:\Program Files\Microsoft Visual Studio\MyProjects\WIN32Pract2\Main.cpp(5) : fatal error C1004: unexpected end of file found
    Last edited by philvaira; 04-10-2004 at 05:34 PM.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Quote Originally Posted by philvaira
    LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    {

    PAINTSTRUCT PaintStruct;
    HDC hDC;


    switch(Msg)
    {
    case WM_MBUTTONDOWN:
    { //<--- one of these...
    Player PC;
    PC.Age = 22;
    MessageBox(NULL, Player.Age, "Message", MB_OK);
    break;
    } //<-- ... and another
    Try this:

    Code:
    switch(Msg)
     case WM_MBUTTONDOWN:
      {
         Player PC;
         PC.Age = 22;
         MessageBox(NULL, PC.Age, "Message", MB_OK);
      }
    
      break;

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You haven't #included the header file which contains the definition/declaration of Player. Alternatively, just add the contents of 'header' immediately after you #include <windows.h>.

    Also, you can't pass numbers to the MessageBox function; you have to convert them the strings first. For example:
    Code:
    case WM_MBUTTONDOWN: 
      {
      Player PC;
      PC.Age = 22;
      TCHAR chTxt[32];
      wsprintf(chTxt,TEXT("%d"),PC.Age);
      MessageBox(NULL, chTxt, "Message", MB_OK);
      break;
      }
    edit: typos
    Last edited by Ken Fitlike; 04-10-2004 at 06:56 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    Code:
    error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'unsigned short' to 'const char *'
    My spider senses are telling me that MessageBox() only uses chars instead of integers. I tried the following, and it worked for char's.

    Code:
    switch (Msg)
    {
    	 case WM_MBUTTONDOWN:
    	{
    	 char Name[] = "Phil";
    	 MessageBox(NULL, Name, "Message", MB_OK);
                    }

  10. #10
    Registered User
    Join Date
    Apr 2004
    Posts
    72
    There we go.. thanks

  11. #11
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'unsigned short' to 'const char *'<<

    Strange...

    TCHAR is only typedef'd as unsigned short (ie. wchar_t) when UNICODE is defined (and as char when it isn't) which, if true, would mean that MessageBoxW would be called...

    Still, glad that you have it working now.
    Last edited by Ken Fitlike; 04-10-2004 at 07:25 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed