Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

WindowProc - need to pass it some data

This topic is 3653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there anyway to pass an additional varible to the WindowProc function? I need to give it a pointer to the window class that created the window. I suppose I could create a map to link the HWND with a pointer to a class but that is hardly an efficent way of doing it...:(
	class Base
	{
		HANDLE Loop;//thread for the windows message pump
		public:
		static LRESULT CALLBACK WindowProc(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam);//this needs a pointer to the base class instance...

		HWND Hwnd;

		Base();
		~Base();
		bool WindowCreate(unsigned width, unsigned Height, const std::string &Caption, bool FullScreen);
		bool WindowDestroy();
...
	};

Share this post


Link to post
Share on other sites
Advertisement
SetWindowLong(hwnd, GWL_USERDATA, YOUR_DATA);

...

Foo f = GetWindowLong(hwnd, GWL_USERDATA));

More information is of course on MSDN.

Share this post


Link to post
Share on other sites
Why is this not working? It doesn't get the pointer and as a result crashes when the WindowProc needs to use it :(

struct WindowLoopData
{
Base* Owner;
std::string Caption;
unsigned Width, Height;
bool Fullscreen;
WindowLoopData(Base* Owner, std::string Caption, unsigned Width, unsigned Height, bool Fullscreen)
{
this->Owner = Owner;
this->Caption = Caption;
this->Width = Width;
this->Height = Height;
this->Fullscreen = Fullscreen;
}
};
unsigned __stdcall WindowLoop(void* Param)
{
WindowLoopData *Data = (WindowLoopData*)Param;
Base* Owner = Data->Owner;

//create a window
Owner->Hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"BaseWc", Data->Caption.c_str(),
WS_OVERLAPPEDWINDOW/*WS_EX_TOPMOST*/,
0, 0, Data->Width, Data->Height,
0, 0, hInstance, Owner);
if(!Owner->Hwnd)
{
lib::ErrorSet("Base::WindowCreate failed - Window creation failed");
return false;
}

ShowWindow(Owner->Hwnd, SW_SHOW);

MSG Msg;
while(GetMessage(&Msg, Owner->Hwnd, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage (&Msg);
}

delete Data;
ThreadExit(Msg.wParam);
return 0;
}




LRESULT CALLBACK Base::WindowProcRouter(HWND Hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if(Msg == WM_NCCREATE)//first Msg = 36 ???
{
Base *Owner = (Base*)((LPCREATESTRUCT)lParam)->lpCreateParams;
SetWindowLong(Hwnd, GWL_USERDATA, (long)Owner);

Owner->Hwnd = Hwnd;
return 0;
}
else
{
Base *Owner = (Base*)(GetWindowLong(Hwnd, GWL_USERDATA));
return Owner->WindowProc(Msg, wParam, lParam);
}
}


Share this post


Link to post
Share on other sites
WM_NCCREATE is not necessarily the first window message that the window receives; it's just the first window message that the window receives that you can extract the create parameters from. You need to handle the case where other window messages come before you've extracted the create parameters from WM_NCCREATE.

Share this post


Link to post
Share on other sites
ok...now Ive got another problem


case WM_NCCREATE:
SetWindowLongPtr(Hwnd, DWLP_USER, (long)((LPCREATESTRUCT)lParam)->lpCreateParams);//Hwnd = 0x001c05b8, base pointer = 0x003376c0
break;




And on the next message

Base *Window = (Base*)GetWindowLongPtr(Hwnd, DWLP_USER);//Hwnd = 0x001c05b8, base pointer = NULL ???




Wheres my stored value disappearing too...

EDIT: never mind..stupid mistake/

Share this post


Link to post
Share on other sites
THat was the stupid mistake :)

Anyway new problem...The windows are created and their message pumps handeled by another thread. When my main thread calls "DestroyWindow(Hwnd);" for a window the window is not destroyed and a WM_DESTROY message is not sent :(

Share this post


Link to post
Share on other sites
HWNDs are thread sensitive. You can only pump messages in the same thread that created the window.

Never separate that. That also applies to a lot of window related functions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!