CreateCompatibleBitmap

CreateCompatibleBitmap

本文介绍了如何让CreateCompatibleBitmap与OnDraw(CDC * pDC)一起使用 - 一个MFC C ++项目visual studio 2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Studio 12 C ++问题:



我在下面的OnDraw(CDC * pDC)代码中尝试在后台编写显示然后稍后用bitblt在前台显示它...但在运行期间创建兼容的DC cdcMemory(isCDCcreated返回true)。然而,hbmMemory返回null(CreateCompatibleBitmap失败),我不知道为什么......我感谢任何建议或后续行动。



问题:CreateCompatibleBitmap失败



使用bmpLoad演示代码,CreateCompatibleBitmap失败



I have a Visual Studio 12 C++ issue:

I have within OnDraw(CDC* pDC) code below to try to write the display in the background and then show it in the foreground later with bitblt ... but during run-time the compatible DC cdcMemory is created (isCDCcreated returns true). However hbmMemory returns null (CreateCompatibleBitmap fails) and I do not know why.... I appreciate any suggestions or follow-up.

ISSUE: CreateCompatibleBitmap fails

With bmpLoad demonstration code, CreateCompatibleBitmap fails

    // Following: http://stackoverflow.com/questions/17416152/transfer-an-image-from-cdc-to-cbitmap
    // and https://msdn.microsoft.com/en-us/library/kwxzck32.aspx
    // Create an in-memory CDC compatible with the display DC we're using to paint
    CDC cdcMemory;  //A background memory version of the CDC to draw on to make the final update appear faster
    BOOL isCDCcreated = cdcMemory.CreateCompatibleDC(pDC);

...

    // Create a compatible bitmap from the previously created compatible CDC cdcMemory
    // See https://msdn.microsoft.com/en-us/library/25kc2k5s.aspx for HBITMAP conversion from Cbitmap
    // See explanation at http://forums.codeguru.com/showthread.php?314588-a-quick-question-how-to-use-CDC-operator-HDC
    // See additinal important explanation at http://www.codeproject.com/Articles/224754/Guide-to-Win32-Memory-DC
    HDC hdcfromcdc_pDC = (HDC) pDC; // Get the HDC for the existing window's PDC to create the compatible bitmap
    // Now create the compatible bitmap in background memory to draw on.
    HBITMAP hbmMemory = CreateCompatibleBitmap(hdcfromcdc_pDC, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
    if (hbmMemory == NULL) // Error message if cannot create compatible HBITMAP
        {
            MessageBox(L"CreateCompatibleBitmap has failed", L"Failed", MB_OK);
            goto restoreandexit;  // go to end to restore and exit
        }



hbmMemory返回Null并出现错误消息。为什么会这样,我怎么能让CreateCompatibleBitmap工作?


hbmMemory returns Null and the error message comes up. Why is this, and how can I get CreateCompatibleBitmap to work?

推荐答案

HDC hdcfromcdc_pDC = (HDC) pDC; // Get the HDC for the existing window's PDC to create the compatible bitmap





您尝试将指向CDC的指针投射到HDC!



试试这个:





You try to cast pointer to CDC to HDC!

Try this:

HDC hdcfromcdc_pDC = (HDC) *pDC; // Get the HDC for the existing window's PDC to create the compatible bitmap


auto hdc = pDC->m_hDC;





并让编译器推断出hdc的类型,如果您尝试以奇怪的方式使用该对象,则随后发出警告。



and let the compiler deduce the type of hdc AND subsequently warn you if you try and use the object in a weird way.



这篇关于如何让CreateCompatibleBitmap与OnDraw(CDC * pDC)一起使用 - 一个MFC C ++项目visual studio 2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:35