I think ive read just about every article and post on the internet relating to this subject and rewritten this code in about 20 different ways. Aside from the strange results from different ways of doing it (window is drawn, but doesnt initially show up, but windows that cover it leave a print on the desktop in the shape of the windows frame) I can never seem to get the pointer to the window back using using SetWindowLong() or GetWindowLong().
Heres the code im using now, pParentWindow is always 0 no matter what i try so it never jumps into the "MessageHandler()" from this function
Code:
LRESULT CALLBACK W32Window::WinProc(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
//=========================================
// FOR ERROR CHECKING
char error[128] = {0};
LONG_PTR r;
DWORD err;
//=========================================
W32Window* pParentWindow = 0;
int proc = 0;
if(uMessage == WM_CREATE) // Message comes to the window
{
//LPCREATESTRUCT lpcs = (LPCREATESTRUCT)lParam;
//pParentWindow = (W32Window*)lpcs->lpCreateParams;
r = ::SetWindowLong(hWnd, GWL_USERDATA, (LONG)((LPCREATESTRUCT)lParam)->lpCreateParams); // WARNING 1
if(r == 0)
{
err = GetLastError();
sprintf(error, "Error code \" %d \" by SetWindowLong..", err);
MessageBox(NULL, error, "TEST2", MB_OK);
}
}
else
{
pParentWindow = (W32Window*)::GetWindowLong(hWnd, GWL_USERDATA); // GWLP_USERDATA , WARNING 2
if(pParentWindow == 0)
{
err = GetLastError();
if(err != 0 )
{
sprintf(error, "Error code \" %d \" by GetWindowLong..", err);
MessageBox(NULL, error, "TEST3", MB_OK);
}
}
}
if(pParentWindow)
{
pParentWindow->MessageHandler(uMessage, wParam, lParam); // Dropped MessageBox() in here to see if it ever goes into MessageHandler, but no
}
return DefWindowProc(hWnd, uMessage, wParam, lParam);
}
Ive dropped " this " into CreateWindowEx()
some things ive tried are:
- Rewritting it in different ways (the above being one of the shorter versions), trying WM_CREATE instead of WM_NCCREATE, using switch to handle messages
- Using Get/SetWindowLongPtr
- Using Get/SetProp instead
- Setting GWL_USERDATA or GWLP_USERDATA (depending) to 0 (seen that done a few times, but GetLastError just give me invalid index error code 1413?)
right now the window shows but the process doesnt end when i hit exit on the window disappears (know why that is)
and get the warnings
warning C4311: 'type cast' : pointer truncation from 'LPVOID' to 'LONG'
warning C4312: 'type cast' : conversion from 'LONG' to 'W32Window *' of greater size
No matter what ive never been able to get the window back through Set/GetWindowLong(Ptr) what am i missing. Using .NET 2003