xcdemon05
xcdemon05

Reputation: 1422

convert BITMAP to HBITMAP

If I have a BITMAP named bitmap, how to I convert this into an HBITMAP?

So far I have tried:

HBITMAP hbm = (HBITMAP)&bitmap; //doesn't work

HANDLE hand = &bitmap;
HBITMAP hbm = hand; //doesn't work

CImage cim;
cim.Attach(&bitmap);
HBITMAP hbm = cim.Detach(); //definitely doesn't work

CBitmap cbm;
cbm.Attach(bitmap);
CImage cim;
cim.Attach(cbm.Detach());
HBITMAP hbm = cbm.Detach(); //desperate I know, horribly failed

There has got to be some easy, one line way to do it but I just can't find it. Can I get some help?

Upvotes: 5

Views: 7382

Answers (1)

David Heffernan
David Heffernan

Reputation: 612784

Call CreateBitmapIndirect passing your BITMAP. Hey presto, an HBITMAP is returned.

For what it's worth, a good way to look for the answer yourself is to go to the MSDN topic describing BITMAP. Scroll down to the bottom of the page and look at the See Also section. The most important functions that use the type will be listed there, and indeed CreateBitmapIndirect is there. That's always a useful way of finding out what can be done with any particular Win32 type.

Upvotes: 9

Related Questions