为什么下面的代码不起作用?

发布于 2024-11-04 11:37:07 字数 1666 浏览 5 评论 0原文

我已经使用以下样式创建了一个静态控件...

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

从我的程序中的某个地方我有以下代码..

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

现在在 dispWndProc 中我有以下代码..

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

谁能告诉我为什么 lParam 不类型转换回 HBITMAP。 ...为什么 img 是 NULL ?

提前感谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-11-11 11:37:07

某些其他代码也可能将 STM_SETIMAGE 发送到您的窗口。计算调用 SendMessage(STM_SETIMAGE) 的次数以及达到 case STM_SETIMAGE 的次数。


另外,HDC imgDC = GetDC((HWND)img); 永远不会起作用。 HBITMAP 不是 HWND

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.

行至春深 2024-11-11 11:37:07

这段代码存在多个问题。

  1. 除了处理 WM_PAINT 之外,您不能在任何地方使用 BeginPaint / EndPaint。在考虑其他问题之前先解决这个问题。
  2. 接下来,尚不清楚您是否正确地对窗口进行了子类化;确保在旧窗口过程上调用 CallWindowProc 。
  3. 保证你所看到的确实是你认为你所看到的是很棘手的。例如,正如本·沃伊特所说,也许你不是发送它的人。也许上面的开关盒块掉下来了。也许您一开始就传入了NULL

从这些事情开始,你就会更接近正轨。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文