CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2013
    Posts
    19

    Exclamation Error in registering windowsclass

    The following code it taken from msdn library but it is failing to compile.
    the following code has a header where all the variables used here are stored in header App.h.
    please help me with this, i cannot really go further without this problem resolved.

    The following lines are giving trouble:

    Code:
    DialogBox(pApp->getInstance(), MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, pApp->About);
    error: 'App::About': function call missing argument list; use '&App::About' to create a pointer to member

    Code:
    wcex.lpfnWndProc	= &App::WndProc;
    error: '=' : cannot convert from 'LRESULT (__stdcall App::* )(HWND,UINT,WPARAM,LPARAM)' to 'WNDPROC'
    There is no context in which this conversion is possible
    (Removing &did not help)


    Here is the complete code:


    Code:
    #include "stdafx.h"
    #include "App.h"
    
    
    App::App(void)
    {
    }
    
    
    App::~App(void)
    {
    }
    
    
    //Initializing variables
    BOOL App::Init(HINSTANCE hInstance, int cmd)
    {
       
    
       hInst = hInstance; // Store instance handle in our global variable
    	
       // Initialize global strings
    	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    	LoadString(hInstance, IDC_FIRSTPAINT, szWindowClass, MAX_LOADSTRING);
    	MyRegisterClass(hInstance);
    
    	// Perform application initialization:
    	if (!InitInstance (cmd))
    	{
    		return FALSE;
    	}
    	return true;
    }
    
    //   FUNCTION: InitInstance(HINSTANCE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL App::InitInstance(int nCmdShow)
    {
    	HWND hWnd;
       hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    	   CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInst, this);
    
       if (!hWnd)
       {
          return FALSE;
       }
    
       ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);
    
       return TRUE;
    }
    
    
    //Function to run the message loop for the class
    
    int App::RunMessageLoop()
    {
    	MSG msg;
    	HACCEL hAccelTable;
    
    	
    	hAccelTable = LoadAccelerators(hInst, MAKEINTRESOURCE(IDC_FIRSTPAINT));
    
    	// Main message loop:
    	while (GetMessage(&msg, NULL, 0, 0))
    	{
    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    
    	return (int) msg.wParam;
    }
    
    
    //
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    ATOM App::MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    
    	wcex.style			= CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc	= &App::WndProc;
    	wcex.cbClsExtra		= 0;
    	wcex.cbWndExtra		= 0;
    	wcex.hInstance		= hInst;
    	wcex.hIcon			= LoadIcon(hInst, MAKEINTRESOURCE(IDI_FIRSTPAINT));
    	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_FIRSTPAINT);
    	wcex.lpszClassName	= szWindowClass;
    	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    
    	return RegisterClassEx(&wcex);
    }
    
    
    //
    //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND	- process the application menu
    //  WM_PAINT	- Paint the main window
    //  WM_DESTROY	- post a quit message and return
    //
    //
    LRESULT CALLBACK App::WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	App* pApp;
    
    
    
        if (message == WM_CREATE)
        {
            LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
            pApp = (App *)pcs->lpCreateParams;
            ::SetWindowLongPtrW(hWnd,GWLP_USERDATA,PtrToUlong(pApp));
    
            return TRUE;
        }
    
        else
    
        {
            pApp = reinterpret_cast<App *>(static_cast<LONG_PTR>(::GetWindowLongPtrW(hWnd,GWLP_USERDATA)));
    
    	 if (!pApp)
               return DefWindowProc(hWnd, message, wParam, lParam);
    
    
    	int wmId, wmEvent;
    	PAINTSTRUCT ps;
    	HDC hdc;
    
    	switch (message)
    	{
    	case WM_COMMAND:
    		wmId    = LOWORD(wParam);
    		wmEvent = HIWORD(wParam);
    		// Parse the menu selections:
    		switch (wmId)
    		{
    		case IDM_ABOUT:
    			DialogBox(pApp->getInstance(), MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, pApp->About);
    			break;
    		case IDM_EXIT:
    			DestroyWindow(hWnd);
    			break;
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    		}
    		break;
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Add any drawing code here...
    		EndPaint(hWnd, &ps);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	default:
    		return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    }
    
    
    // Message handler for about box.
    INT_PTR CALLBACK App::About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	UNREFERENCED_PARAMETER(lParam);
    	switch (message)
    	{
    	case WM_INITDIALOG:
    		return (INT_PTR)TRUE;
    
    	case WM_COMMAND:
    		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    		{
    			EndDialog(hDlg, LOWORD(wParam));
    			return (INT_PTR)TRUE;
    		}
    		break;
    	}
    	return (INT_PTR)FALSE;

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error in registering windowsclass

    Quote Originally Posted by pavankn18 View Post
    The following code it taken from msdn library
    Are you sure you copied the code correctly? In your other posts, you seem to take working code, and then turn it into non-working code.
    but it is failing to compile. the following code has a header where all the variables used here are stored in header App.h. please help me with this, i cannot really go further without this problem resolved.
    Please post "App.h".

    That error probably happened since a WNDPROC cannot be a non-static member function. It must be a non-class or static class function. You are trying to assign a non-static member function pointer to a type that knows nothing about non-static class member functions.

    In C++, a non-static member function and a global function are two totally different typess, even though they would seem to be the same thing.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 28th, 2013 at 05:57 PM.

  3. #3
    Join Date
    Jan 2013
    Posts
    19

    Re: Error in registering windowsclass

    Hey thanks, that was the problem,

    i dont copy the code as it is, instead i will study once and write it so that i can traceback what went wrong, in this problem i was looking only at app.cpp, so could not resolve it.
    thanks you your help

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Error in registering windowsclass

    Quote Originally Posted by pavankn18 View Post
    i dont copy the code as it is,
    Then don't say that you got it from MSDN, as that makes MSDN look bad for no reason.
    instead i will study once and write it so that i can traceback what went wrong,
    If your goal is to learn Windows programming, then you take working code and run that first. Then you take the working code and you add to it, but not change the code so that you now break fundamental rules of C++.

    It would seem you need to learn C++, not just Windows programming. The examples you are using assume you know C++ already -- if you go mess up the examples by writing faulty C++ code, then you're not going to get very far.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2013
    Posts
    19

    Re: Error in registering windowsclass

    Ok sorry i understand,

    Actually i studied c++, but soon i started to learn vc++ i am encountering many problems which i did not face while learning c++ in console application, that time i used codeblocks and was writing entire program in single file but i know that is not a good idea,

    and i can see too many macros in vc++ and so many functions, i am often using "goto definition", but few times it wont help much, for example LoadString function, i am infact finding it difficult to learn at this point.

    I was aware that only static functions can be called without using class object, yet i was not thinking about type of functions in app class, i know i am not an expert in c++, but i really want to learn windows programming at this point.

    Thank you, i am really getting help much needed at this point.
    Last edited by pavankn18; January 29th, 2013 at 10:30 AM.

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

Featured