Thread: Classes and Win32 API, and another Question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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;

  7. #7
    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.

  8. #8
    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);
                    }

Popular pages Recent additions subscribe to a feed