CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    Subclassing error (127)

    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);
    }




  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Subclassing error (127)

    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


  3. #3
    Guest

    Re: Subclassing error (127)

    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


  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Subclassing error (127)

    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



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured