Click to See Complete Forum and Search --> : Subclassing error (127)


April 21st, 1999, 10:17 AM
What's wrong with following code? It crashes at run time after one succeful
draw. The error code is 127. Which means the procedure (don't know if it's
the new procedure or the old one) can't be found.
Please help.


// declare
WNDPROC oldproc=NULL;

LRESULT CALLBACK NewWinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM
lParam);


// code in main
........

oldproc = (WNDPROC)GetWindowLong(hWnd, GWL_WNDPROC);
SetWindowLong(hWnd, GWL_WNDPROC, (LONG)NewWinProc);



// subrouting
LRESULT CALLBACK NewWinProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM
lParam)
{
HDC hDC, hMemDC;
HBITMAP hBmp;
BITMAP bmpp;
int width, height;

if (Msg == WM_PAINT)
{
hDC = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hDC);

hBmp = LoadImage(NULL, bmpNam, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); // bmpNam is global

SelectObject(hMemDC, hBmp);
GetObject(hBmp, 64, &bmpp);

width = bmpp.bmWidth;
height= bmpp.bmHeight;

BitBlt(hDC, x0, y0, width, height, hMemDC, 0, 0, SRCCOPY); // x0 & y0 are globals

DeleteDC(hMemDC);
DeleteObject(hBmp);
ReleaseDC(hWnd, hDC);
}

return CallWindowProc(oldproc, hWnd, Msg, wParam, lParam);
}

Paul McKenzie
April 21st, 1999, 11:06 AM
For a WM_PAINT message, I believe you need to call BeginPaint(). This gets you the DC for the HWND. You also need to call EndPaint() before returning (don't do a ReleaseDC() on the HWND DC).

Regards,

Paul McKenzie

April 22nd, 1999, 03:37 PM
Thanks Paul. Error 127 is fixed.

Now I got another problem: This library can only be used on one window.
When I use it on second window with another bmp, the first window also
got painted with the second bmp.

How do I create a different window procedure for the second window?

TIA

Paul McKenzie
April 22nd, 1999, 04:29 PM
You do the same for the second window as you would the first, but there are some things that I see with your code that will not work correctly if you try to do this.

When you subclassed the second window, where do you save the "oldproc" for the second window? You have one variable called oldproc and you are attempting to use it for more than one window.

You need to build a table of hWnds mapped to their oldProc's. When you subclass a window you should check the table to see a) if the window has been subclassed already and b) if not subclassed, get the hwnd and oldproc, pair them together, and store them in an array (or map, or whatever your favorite data structure is) keyed by the hWnd value.

When it's time to call the oldproc, you lookup the oldproc in the table by using the hwnd as a key, and call that oldproc. Remember, you're dealing with *multiple* windows, all with differing "oldprocs"!

When you unsubclass the window, remove the hwnd / oldproc pair from the table.

Hope this helps.

Regards,

Paul McKenzie