开发者

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,


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.


There are multiple issues with this code.

  1. You cannot use BeginPaint / EndPaint anywhere except for handling WM_PAINT. Fix that before even considering other problems.
  2. Next, it's not clear that you're correctly subclassing the window; make sure you call CallWindowProc on the old window proc.
  3. It's tricky to guarantee that what you are seeing is really what you think you are seeing. For example as Ben Voigt says, maybe you are not the one that sent it. Maybe a switch case block above fell through. Maybe you passed in NULL to begin with.

Start with these things, and you will get closer to being on track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜