Strange error with CreateCompatibleDC

Perhaps this is a stupid question, I don’t understand why I can’t get the DC created in the following code:

HBITMAP COcrDlg::LoadClippedBitmap(LPCTSTR pathName,UINT maxWidth,UINT maxHeight)
{
    HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, pathName, IMAGE_BITMAP, 0, 0,
                                  LR_LOADFROMFILE | LR_CREATEDIBSECTION);       
    if (!hBmp)
        return NULL;

    HDC hdc = (HDC)GetDC();
    HDC hdcMem = CreateCompatibleDC(hdc);
    if (!hdcMem)
    {
        DWORD err = GetLastError();
    }  
    ...
    ...
    ...

The hBmp bitmap loads fine, and the hdc value has a valid value. But calling CreateCompatibleDC () returns a NULL pointer. Then GetLastError () returns 0! Can anyone guess what is going on here, please?

PS: There are no memory allocations or GDI routines before this ... therefore I believe that memory leaks should be eliminated.

+5
source share
2 answers

GetDC() HDC. GetDC() CDC.

, , . , MFC -, :

CDC *pDC = GetDC();

// Option 1
CDC memDC;
memDC.CreateCompatibleDC(pDC);

// Option 2
HDC hMemDC = CreateCompatibleDC((HDC)(*pDC));

, 2 , . CDC operator HDC(), HDC, . .

+5

CreateCompatibleDC(). DC . hdc GetDeviceCaps() RASTERCAPS.

, GetDC, , - , C, , , . SoapBox .

+3

All Articles