Click to See Complete Forum and Search --> : Setting a bitmap as form background
sibi
April 20th, 1999, 12:26 AM
I want to load a bitmap file as background of my form. What i want is that the bitmap will be a external file and by making load call i should be able to load a particular bitmap as background.The number of bitmaps i should load in this manner is so large that i cannot afford the bitmap to be compiled into the program .exe.
NOTE: I am A beginner in VC++
M.Anand
April 20th, 1999, 02:18 AM
Use CBitmap:: LoadBitmap function and then Select the return object into the Device context using SelectObject. Then Create a compatible Memory DC and load the Bitmap on to that.
Finally use BitBlt.
I present a sample code
CPaintDC dc(this); // device context for painting
Note: This dc depends on your program. I have done it in OnPaint( ) function
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC dcmem;
dcmem.CreateCompatibleDC(&dc);
dcmem.SelectObject(&bmp);
dc.BitBlt(0,0,640,480,&dcmem,0,0,SRCCOPY);
With regards
Anand
M.Anand
Development - Lead ,
Satyam Infoway Limited,
Chennai - 600034
India
Jason Teagle
April 20th, 1999, 02:43 AM
Unfortunately the other response relies on the bitmap being part of the resource script = part of the EXE, which is exactly what you didn't want.
The following should help. Note that it only works on Windows '95 (I don't know about '98). Quite why it doesn't work on NT is a mystery, but that's what the help file says (Microsoft: Why?).
---
void CMyWindow::PaintBitmap(CString strFilename)
{
BITMAP sBmp ;
CBitmap bmpPicture, *pBmpOld ;
CClientDC dc(this);
CDC dcTemp ;
HBITMAP hBmp ;
int iWidth, iHeight ;
// Create a temporary memory DC.
dcTemp.CreateCompatibleDC(NULL);*** // Compatible with the screen.
// Load the bitmap.
hBmp = ::LoadImage(NULL,(LPCTSTR)strFilename, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);
bmpPicture.Attach(hBMP);
// Get its dimensions.
bmpPicture->GetObject(sizeof(BITMAP), &sBmp);
iWidth = sBmp.bmWidth ;
iHeight = sBmp.bmHeight ;
// Select the bitmap into the memory DC ready for blitting.
pBmpOld = (CBitmap *)dcTemp.SelectObject(&bmpPicture);
// Blit to the screen DC.
dc.BitBlt(0, 0, iWidth, iHeight, &dcTemp, 0, 0, SRCCOPY);
// Select the bitmap out of the memory DC again.
dcTemp.SelectObject(pBmpOld);
// Delete the loaded bitmap.
bmpPicture.DeleteObject();
// Delete the temporary DC we created.
dcTemp.DeleteDC();
}
---
If you need the code to work under NT (or 16-bit Windows?), e-mail me at jteagle@geocities.com and I will dig out the code you need.
This code should be called from your form's (= dialogue box's?) OnPaint() procedure.
Does this help?
Kanwardeep
April 20th, 1999, 03:09 AM
You can also use active-X (Microsoft forms 2.0 OR picture ) control by just pasting the jpg(it will be of lesser size than bmp) over the widow without writing any code.
M.Anand
April 20th, 1999, 03:37 AM
Hi
Thanks for your correction. I had VB's LoadPicture in my mind ( which takes the full path of the Bitmap) and didn't concentrate much on that LoadBitmap function
Anand
M.Anand
Development - Lead ,
Satyam Infoway Limited,
Chennai - 600034
India
Jason Teagle
April 20th, 1999, 05:09 AM
No problem, switching between VB and VC++ can be confusing sometimes, because your mind gets trained to the one language and you temporarily 'forget' the other stuff.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.