Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Beginner's question, please, please, help me

  1. #1
    Guest

    Beginner's question, please, please, help me

    Hello everyone,
    I have little problem. I'm trying to create very simple class that will be able to create a window and holds messages for it. Code follows :

    MyClass.h
    class MyClass
    {
    LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);
    BOOL CreateWindow();
    ...
    };

    MyClass.cpp
    #include "MyClass.h"
    LRESULT CALLBACK MyClass::WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    switch(....
    .....
    }

    BOOL MyClass::CreateWindow()
    {
    WNDCLASS wc;

    hInstance=GetModuleHandle(NULL);
    wc.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
    wc.lpfnWndProc=(WNDPROC)WndProc;
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hInstance=hInstance;
    wc.hIcon=LoadIcon(NULL, IDI_WINLOGO);
    wc.hCursor=LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground=NULL;
    wc.lpszMenuName=NULL;
    wc.lpszClassName="Test";

    RegisterClass....
    ....
    }

    My problem is on line "wc.lpfnWndProc=(WNDPROC)WndProc;". When I tried to compile it, this error occurs

    "error C2440: '=' : cannot convert from 'long (__stdcall MyClass::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,lo
    ng)' There is no context in which this conversion is possible"

    Do you have any ideas, what's wrong in my code. I'm just a beginner, so I'm sorry for my stupidity. Also sorry for my English, I'm working on it.

    Thank you very much.
    Boda.

  2. #2
    Intern Contributor
    Join Date
    Feb 2000
    Location
    Breda, Netherlands
    Posts
    58

    Re: Beginner's question, please, please, help me

    The problem is you want a windows callback function into a class. Windows and C++ don't want this. It can be done, but your source would be a huge mess and it's not the best thing to do. Move the function out of the class and try again.

    John

  3. #3
    Guest

    Re: Beginner's question, please, please, help me

    Yeah, you are true. It does work with callback function out of the class. Thank you very much.

  4. #4
    Junior Member Newbie
    Join Date
    Jun 2000
    Location
    Hauppauge, NY, USA
    Posts
    23

    Re: Beginner's question, please, please, help me

    This is MFC right?


    If it is I would recommend recodeing it with WINAPI it will be faster and you can take the callback out of that EVIL class.

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    London
    Posts
    503

    Re: Beginner's question, please, please, help me

    I usually have wndProc as a static class member function. This also works fine, and allows the wndProc access to private members of your window class.

    (In case you were wondering, your problem was that normal member functions have an invisible "this" pointer parameter, so the function signature doesn't match the one Windows expects for the callback function. Nonmember functions and static member functions aren't tied to one object and so don't have this extra parameter.)

Posting Permissions

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