Advertisement

Loading a Bitmap into a DC

Started by July 19, 2000 03:22 PM
7 comments, last by CoolMike 24 years, 5 months ago
Even though I already know a lot of DirectX, I still can''t quite get DirectDraw to run in a window. So I''ve decided to broaden my skills and learn MFC. My goal is just to make a simple windowed game anyways, so using DirectX isn''t really important. So here is my question - How do I load *.bmp files into a device context so I can draw sprites on my window?
You can visit my web page and download the BitBlt demo I have there. I created this to help a friend learn to use BitBlt. I don''t remember exactly what the program does, but the zip file contains a bitmap and the program uses BitBlt so I''m guessing that it will answere your question.
Advertisement
Here is a simple way of loading a bitmap from a ".rc" file.

HDC DC, MemDC;
HWND hwnd;
HBITMAP hBitmap;
HINSTANCE hInst;

hBitmap = LoadBitmap(hInst, "MY_BITMAP");

DC = GetDC(hwnd);
MemDC = CreateCompatibleDC(DC);
SelectObject(MemDC, hTileBitmap);
BitBlt(DC, 0, 0, 128, 128, MemDC, 0, 0, SRCCOPY);
DeleteDC(MemDC);
ReleaseDC(hwnd, DC);

//Put this in the reasource file

MY_BITMAP BITMAP "something.bmp"



Edited by - The_C_Guy on July 19, 2000 5:13:14 PM
http://www.crosswinds.net/~druidgames/resist.jpg
Use LoadImage() if you don''t want to read the bitmap data on your own!

----------------------------------------------
That's just my 200 bucks' worth!

..-=gLaDiAtOr=-..
Wow, thanks guys. It took me about five minutes to get a bitmap loaded after reading your posts. Now if only I could figure these kinds of things out myself. Now on to bigger and better things, like making buttons. Anybody know anything about those?
I take it you mean the standard window buttons. Call CraeteWindow and specify "BUTTON" in the class name.
    hButton = CreateWindow("BUTTON",                       "Click Me",                       WS_CHILD|BS_PUSHBUTTON|BS_TEXT,                       20,20,100,40                       hWnd,//parent window                       NULL,                       hInst,                       NULL);    


That should do the trick




========================================================
If something sounds stupid but works, it's not stupid
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Advertisement
That helps, NuffSaid, but do I have to draw the button or anything? The program runs fine but doesn''t display a button.
try adding WS_VISIBLE in the flags.

Hope that helps
While we''re on the topic of blitting and device contexts, can somebody tell me how to load a bitmap onto a DirectDraw surface without using the CreateSurfaceFromFile function? This is because I''d like to use an image library (like OpenIL) to load compressed images. I use Visual Basic.

Thanks for your help.

GDNet+. It's only $5 a month. You know you want it.

This topic is closed to new replies.

Advertisement