您可以在静态图片控件上设置DDB吗 [英] Can you set a DDB on Static Picture Control

查看:100
本文介绍了您可以在静态图片控件上设置DDB吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用SS_BITMAP样式创建了图片控件,并在内存DC上创建了位图.那么可以或如何为控件设置DDB的HBITMAP?不适用于:

I have the picture control created with SS_BITMAP style and bitmap created on a memory DC. so can or how can you set the HBITMAP of a DDB for the control? It doesn't work with using:

    HDC hDC = GetDC(hPB); //hPB, Handle to static control.
    HDC hMemDC = ::CreateCompatibleDC(hDC);
    HBITMAP hDDB = ::CreateCompatibleBitmap(hDC, 17, 14);
    SelectObject(hMemDC, hDDB);

    //Draw on DDB     

    SendMessage(hPB, STM_SETIMAGE, IMAGE_BITMAP, LPARAM(hDDB));

    DeleteObject(hDDB);
    DeleteObject(hBrush);
    ReleaseDC(hPB, hDC);

推荐答案

  1. GDI句柄具有进程亲缘关系-您无法通过STM_SETIMAGE绑定到另一个进程中的静态控件.

  1. GDI handles have a process affinity - you cannot STM_SETIMAGE to a static control in another process.

下一步:不能保证STM_SETIMAGE复制位图-大多数情况下,它会保留HBITMAP句柄中传递的内容.在SendMessage后面的行中,删除hDDB句柄,使静态控件具有无效的位图.

Next: STM_SETIMAGE is not guaranteed to make a copy of the bitmap - mostly it retains the passed in HBITMAP handle. In the line following the SendMessage you delete the hDDB handle leaving the static control with an invalid bitmap.

静态控件希望控制传入的位图的寿命-并在销毁位图句柄时尝试Dest​​royObject位图句柄-这意味着您必须:

The static control expects to control the life of the passed in bitmap - and will attempt to DestroyObject the bitmap handle when it is destroyed - this means you must:

  1. 销毁SendMessage调用返回的所有旧HBITMAP-调用STM_SETIMAGE将旧位图的所有权(和销毁责任)转移到调用代码.(1)
  2. 请勿将单个HBITMAP传递给多个控件,因为第一个关闭的控件将破坏它-破坏了其他控件的队伍.

注意:如果您的应用使用通用控件6来获取

Note: If your app uses common controls 6 to get the visual styles controls, the static control never destroys any bitmap passed via STM_SETIMAGE, so the app needs to destroy any returned handles AND passed in handles.

我认为//Draw on DDB代替了已删除的代码?在这种情况下,查看存在的示例代码,我的灵智力量说,问题是因为您没有选择内存DC的位图OUT.您需要使其看起来像这样(重新排列以创建时的相反顺序进行清理):

I think that //Draw on DDB is in place of removed code? In which case, looking at the sample code that IS present, my psychic powers say that the problem is because you are not selecting the bitmap OUT of the memory DC. You need to make it look like this (re-arranged to cleanup in inverse order of creation):

HDC hDC = GetDC(hPB);
HBITMAP hDDB = ::CreateCompatibleBitmap(hDC, 17, 14);
HDC hMemDC = ::CreateCompatibleDC(hDC);
HGDIOBJ hOld = SelectObject(hMemDC, hDDB);
//Draw on DDB here...    
SelectObject(hMemDC,hOld); // this releases the hDDB
DeleteDC(hMemDC);
ReleaseDC(hPB, hDC);

HBITMAP hbmPrev = (HBITMAP)SendMessage(hPB, STM_SETIMAGE, IMAGE_BITMAP, LPARAM(hDDB));
if(hbmPrev && hbmPrev != hDDB)
  DeleteObject(hbmPrev);

这篇关于您可以在静态图片控件上设置DDB吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆