PDA

View Full Version : Beginner's question, please, please, help me



07-08-2000, 11:43 PM
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.

Sjonny
07-09-2000, 01:27 AM
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

07-09-2000, 02:03 AM
Yeah, you are true. It does work with callback function out of the class. Thank you very much.

Galileo430
07-09-2000, 05:34 AM
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.

MikeC
07-10-2000, 01:50 AM
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.)