Search code examples
c++winapivisual-c++win32-process

Why the following code is not working?


I have Created a static control using following styles...

picBoxDisp = CreateWindow("STATIC", "image box",
         WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
         50, 50, 250, 300,
         hwnd , (HMENU)10000, NULL, NULL);  

SetWindowLongPtr(picBoxDisp,GWLP_WNDPROC,(LONG) dispWndProc);

from someplace in my program I have the following code..

SendMessage(picBoxDisp,STM_SETIMAGE, (WPARAM) IMAGE_BITMAP,(LPARAM) hBitmap);

now inside the dispWndProc I have the following code..

LRESULT CALLBACK dispWndProc(HWND hwnd,UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;
static PAINTSTRUCT paintSt;
static RECT aRect;
switch(msg)
{
    case WM_PAINT:
    {
        hdc = BeginPaint(hwnd,&paintSt);
        GetClientRect(hwnd,&aRect);                     
        // the code for painting 
        EndPaint(hwnd,&paintSt);
    }
    break;
    case STM_SETIMAGE:
    {

        //painting code;
        HBITMAP img = (HBITMAP)lParam;
        BITMAP bmp;
        GetObject(img,sizeof(bmp),&bmp);
        HDC imgDC = GetDC((HWND)img);
        HDC memDC = CreateCompatibleDC(imgDC);
        SelectObject(memDC,img);
        if((img==NULL))// ||(imgDC==NULL)||(memDC==NULL))
        {

                     MessageBox(NULL,"img is NULL","Bad Programming!!! Error",MB_OK);

        }

        else

        {
        StretchBlt(hdc,0,0,aRect.right,aRect.bottom,
        memDC,0,0,bmp.bmWidth,bmp.bmHeight,
        SRCCOPY);
        }

    }
        break;  
    default:
        return DefWindowProc(hwnd,msg,wParam,lParam);

}

return 0;
}

can anyone tell why the lParam doesnt typecast back to HBITMAP.... why img is NULL ?

thanks in advance,


Solution

  • It's possible that some other code is also sending STM_SETIMAGE to your window. Count the number of times you call SendMessage(STM_SETIMAGE) and the number of times you reach case STM_SETIMAGE.


    Also, HDC imgDC = GetDC((HWND)img); is never going to work. An HBITMAP is not an HWND.