Sign in to follow this  
ff8

i need help with classes

Recommended Posts

hi all ! i need your help plz with class see the code first win32.h:
class squall_win32{
public:
	///handle blank window///
		MSG Msg;
	///handle blank window///
	squall_win32();	
	 LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM );
	void sql_win32(const char *,HINSTANCE , HINSTANCE ,
	LPSTR , int);
	~squall_win32();

private:
	///handle blank window///
		WNDCLASSEX wc;
		HWND hwnd;

	///handle blank window///
};

win32.cpp:
#ifndef _SQUALL_H_
#define _SQUALL_H_
#include <windows.h>
#include "win32.h"
#endif


LRESULT  CALLBACK squall_win32:: WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
	switch(msg)
	{
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
} 

void squall_win32::sql_win32(const char *WinTit,HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow){

	const char g_szClassName[] = "myWindowClass";
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Window Registration Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return ;
	}
		hwnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		g_szClassName,
		WinTit,
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error!",
			MB_ICONEXCLAMATION | MB_OK);
		return ;
	}
	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);
}
squall_win32::~squall_win32(){

}

i face this problem why :S??? --------------------Configuration: simple_window - Win32 Debug-------------------- Compiling... win32.cpp C:\Windows\Desktop\WIN32api\simple_window\win32.cpp(29) : error C2440: '=' : cannot convert from 'long (__stdcall squall_win32::*)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long )' There is no context in which this conversion is possible Error executing cl.exe. simple_window.exe - 1 error(s), 0 warning(s) bye !

Share this post


Link to post
Share on other sites
IIRC, the callback can't be a member function. Instead, the caller has to be passed through the fourth paramater, or something. Common question for Win32 people.

Share this post


Link to post
Share on other sites
Make the WndProc static. The reason it's not compiling from what I can see, is that the this pointer, is being implicitly passed to the WndProc method call. Obviously, WndProc doesn't have a this parameter, and rightly so. So, make the method static.

-hellz

Share this post


Link to post
Share on other sites
yeah if you make a class to wrap window stuff with a WndProc in it, the WndProc has to be static. what you can do to over come the drawbacks of having the WndProc as a static member, is when you use CreateWindowEx or CreateWindow to create the window you pass the "this" pointer as the lpParam parameter.
then in the WndProc you handle WM_CREATE and lift the lpCreateParams member of the CREATESTRUCT and attach it to the window with SetWindowLong(hWnd,GWL_USERDATA,lpCreateParams);
then everytime you need to get it back use GetWindowLong, and you got a pointer to the window wrapper class.

Share this post


Link to post
Share on other sites
mmmmm i feel i understand it but not very ...
can any one edit my code for the correct one plz :):):)

Share this post


Link to post
Share on other sites
ff8,

Hopefully this explanation will make things a bit clearer. Take the following code:

test.h

#ifndef TEST_H
#define TEST_H

#include <iostream>

class CTest
{
private:
int x, y;
public:
CTest(int x, int y);
static void Foo();
};

#endif





test.cpp

#include "test.h"

CTest::CTest(int x, int y)
{
this->x = x;
this->y = y;
}

void CTest::Foo() {
std::cout << "Foo() called." << std::endl;
}





main.cpp

#include "test.h"

int main()
{
CTest test(5, 3);

CTest::Foo();

return 0;
}





Now, take a look at the CTest constructor and you'll see that I used the this pointer to initialise the class' member variables, x and y. However, as you can see, there is no parameter defined for the this pointer in the constructor's parameter list:


CTest(int x, int y);





However, with all member functions, the this pointer is implicitly passed to the functions. I think it's a const reference to the current class, but I don't know the actual syntax for the parameter.

However, taking the constructor call in main.cpp as an example:


CTest test(5, 3);





Is effectively turned into something like:


CTest test(this, 5, 3);





Again, I can't remember how exactly the compiler translates that, but I'm sure someone will clear me up on that one.

Now, this doesn't apply to static methods, as they can be used independently of an object. For example:


CTest::Foo();




This calls the CTest::Foo() function, without having an instance of the class. For that reason, a this pointer doesn't need to be passed to static functions, being as the this pointer references an instance of a class.

So to change your original code:


class squall_win32{
public:
///handle blank window///
MSG Msg;
///handle blank window///
squall_win32();
static LRESULT CALLBACK WndProc(HWND , UINT , WPARAM , LPARAM ); // Notice I've added the static keyword.
void sql_win32(const char *,HINSTANCE , HINSTANCE ,
LPSTR , int);
~squall_win32();

private:
///handle blank window///
WNDCLASSEX wc;
HWND hwnd;

///handle blank window///
};





I hope that helps to clear things up for you,

-hellz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this