Jump to content
  • Advertisement
Sign in to follow this  
Fixxer

Win API: Bitmaps

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to create a bitmap in photoshop and use it for my application as the background. How can I load a bitmap and display it using the windows API. An explanation or a link to one would be great. I already tried looking through MSDN but I couldnt find it.

Share this post


Link to post
Share on other sites
Advertisement
Essentially, the process will be that you load the bitmap (LoadBitmap), create a memory-DC (GetDC on the window, then CreateCompatibleDC), select the bitmap into the memory-DC (SelectObject), then copy from the memory-dc to the window-dc (BitBlt or StretchBlt).

Share this post


Link to post
Share on other sites
How can I manually add a bitmap to a rc file because Im not using the IDE's resource editor and if i do it will screw up all the other resources.

Share this post


Link to post
Share on other sites
yea right after i posted i went DUH! lol so ive got that, and ive gotten to the last function call. Im a bit confused on what the destination dc and what the source dc are.

What I have so far:

HANDLE hmainbg = LoadBitmap(hmaininstance,"test.bmp");
HDC hmaindc = GetDC(hmainwnd);
CreatCompatibleDC(hmaindc);
SelectObject(hmaindc,hmainbg);



Confusing Prototype:

BOOL BitBlt(
HDC hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
HDC hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
DWORD dwRop // raster operation code
);

Share this post


Link to post
Share on other sites
The source DC is the DC that contains the bitmap and the destination DC is the DC that you are going to paint to. Here is an example:


// Add this to your dialog's window procedure - it will be called when the
// dialog needs to be repainted
case WM_PAINT: {
PAINTSTRUCT ps;

// hmaindc will be the DC of your window
HDC hmaindc = BegainPaint(hwnd, &ps);

// hmemorydc will hold hmainbg so that we can paint it to the window
HDC hmemorydc = CreateCompatibleDC(hmaindc);

// Of course we have to load the bitmap itself
HBITMAP hmainbg = LoadBitmap(hmaininstance, "test.bmp");

// When we select hmainbg into hmemorydc we must hold the original
// bitmap (which is returned) so that we can return the DC to its original
// state before we delete it
HBITMAP holdbmp = (HBITMAP)SelectObject(hmemorydc, hmainbg);

// Before we draw the bitmap, let's get some information about it
// This includes its width and height
BITMAP bmpinfo;
GetObject(hmainbg, sizeof(bmpinfo), &bmpinfo);

// Now we can draw hmainbg to the window using BitBlt()
BitBlt(hmaindc, 0, 0, bmpinfo.bmWidth, bmpinfo.bmHeight,
hmemorydc, 0, 0, SRCCOPY);

// Now the painting is done so we begin to clean up
// First selelect the original bitmap into the memory DC
SelectObject(hmemorydc, holdbmp);

// Now delete the bitmap
DeleteObject(hmainbg);

// And then the memory DC
DeleteDC(hmemorydc);

// And finally, end the painting process
EndPaint(hwnd, &ps);

break;
}

Share this post


Link to post
Share on other sites
If you're loading the bitmap from a resource file, HANDLE hmainbg = LoadBitmap(hmaininstance,"test.bmp"); is incorrect. You only specify the file name if you're loading from a file. Otherwise, you have to use some macro (MAKEINTORESOURCE or MAKEINTRESOURCE or something like that)

Share this post


Link to post
Share on other sites
This is what I have so far, no success.


HBITMAP hmainbg = LoadBitmap(hmaininstance,"test.bmp");
HDC hmaindc = GetDC(hmainwnd);
HDC hmemdc = CreateCompatibleDC(hmaindc);
HBITMAP holdbmp = (HBITMAP)SelectObject(hmemdc,hmainbg);
BitBlt(hmemdc,0,0,325,300,hmaindc,0,0,SRCCOPY);
SelectObject(hmemdc, holdbmp);
DeleteObject(hmainbg);
DeleteDC(hmemdc);



Share this post


Link to post
Share on other sites
i have no clue if this will work..but here it goes

load the bitmap into the resource file
or have it available to be opened as a file

in the callback for messages put this in there

switch(msg){

...code in there already....

case WM_ERASEBKGND:
HBITMAP bmp=LoadBitmap(hinst,MAKEINTRESORCE(resource id for bitmap));
HDC hDCbg=(HDC)wParam;
RECT rc;
GetClientRect(hwnd,&rc);
HDC hdctemp=CreateCompatibleDC(hDCbg);
SelectObject(hdctemp,bmp);
BitBlt(hDCbg,rc.top,rc.left,rc.bottom-rc.top,rc.right-rc.left,hdctemp,0,0,SRCPAINT);

DeleteObject(hdctemp);
DeleteObject(bmp);
return 0;


that should work...some of the functions may be mixed up, but that is supposed to reapaint the background with a bitmap

to use a file and not a resource use
HBITMAP bmp=LoadBitmap(hinst,"c:/filename.bmp");

Share this post


Link to post
Share on other sites
by the way..anybody know how to use the wave api to record?...
off topic i know

the WM_ERASEBKGROUND message is made for drawing the background
wparam is the hdc, beats haveing to fool with WM_PAINT and possibly replacing the entire window with your bitmap, after all you said background
WM_PAINT is to draw the entire window ( even the box, menu, everything)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!