• Popular Now

• 11
• 27
• 9
• 20
• 31

Archived

This topic is now archived and is closed to further replies.

MFC: Drawing on offscreen Bitmap

This topic is 5108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am trying to draw on an offscreen bitmap using MFC. After I am done drawing on the bitmap I insert it into an imagelist. However, when I use the bitmap from the image list I only get to see a black square. What am I doing wrong here? This is my code:
    CBitmap      *OldBmp = NULL;
CBitmap       bmp;
CClientDC    *pDC = NULL;
CDC           DC;
CBrush        Brush;
CBrush       *OldBrush = NULL;

// Get DC

if (::IsWindow(GetSafeHwnd()))
pDC = new CClientDC(this);
else
pDC = new CClientDC(AfxGetMainWnd());

if (!pDC)
return;

if (!DC.CreateCompatibleDC(pDC))
{
TRACE("Failed to create Compatible DC\n");
return;
}

if (!bmp.CreateCompatibleBitmap(&DC, 8, 8))
{
TRACE("Failed to create Compatible Bitmap\n");
return;
}

OldBmp   = DC.SelectObject(&bmp);
OldBrush = DC.SelectObject(&Brush);

CRect Rect(0, 0, 8, 8);
DC.SetBkColor(RGB(255,0,0));
DC.FillSolidRect(&Rect, RGB(255, 0, 0));

DC.SelectObject(OldBmp);
DC.SelectObject(OldBrush);

// Dump into ImageList

m_ImageList.Create(8, 8, ILC_COLOR, 0, 50);

// Release DC

bmp.DeleteObject();
DC.DeleteDC();
Brush.DeleteObject();
delete pDC;
pDC = NULL;

// Attach imagelist to legenda

m_lstLegenda.SetImageList(&m_ImageList, LVSIL_SMALL);

Toolmaker
-Earth is 98% full. Please delete anybody you can.

Share on other sites
I could be wrong, but I don''t think (COLORREF)0 is a valid COLORREF. Try RGB(0, 0, 0) (or 0xFF000000 if you really want to be hardcore ) instead? Some GDI functions seem to be rather anal about this.

Share on other sites
I got it working! I downloaded Microsoft''s CMemDC example from http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q151/8/56.asp&NoWebContent=1 and used that as a base.

However, I have a new problem. I need to create a bunch of colors(I think the values of 255, 223, 191, 159, 127, 95, 63, 31 would do) using the following pattern:

(Value, 0, 0)
(0, Value, 0)
(0, 0, Value)
(Value, Value, 0)
(Value, 0, Value)
(0, Value, Value)

Where Value ranges from the above list. I can''t find a way to implement this in an algo, any ideas?

Toolmaker

-Earth is 98% full. Please delete anybody you can.