Inserting an image into a view
-
Hi: which is the easiest way to insert a little image (maybe a BMP 30x30 pixels) in specific coordinates (x, y) into a view? Thanks.
-
Hi: which is the easiest way to insert a little image (maybe a BMP 30x30 pixels) in specific coordinates (x, y) into a view? Thanks.
In the OnDraw handler of the view class, first create a HBITMAP struct. Then use LoadImage to load the bitmap image from a file into the struct. Then create a CBitmap object and attach it to the HBITMAP. Then use standard BitBlt techniques to blit the bitmap into the view. Here's a code sample:
void CMyView::OnDraw(CDC* pDC)
{
HBITMAP hbmpMyBitmap;
hbmpMyBitmap = (HBITMAP) LoadImage( NULL, "C:\\Windows\\MyBitmap.bmp",
IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE );CBitmap objBitmap;
objBitmap.Attach( hbmpMyBitmap );CDC tempDC;
tempDC.CreateCompatibleDC( pDC );
CBitmap* pOldBitmap = (CBitmap*) tempDC.SelectObject( &objBitmap );pDC->BitBlt(x, y, width, height, &tempDC, 0, 0, SRCCOPY);
tempDC.SelectObject( pOldBitmap );
return;
}Something like this. Remember to replace file path, destination coordinates and bitmap width and height with the correct values. Hope it helps, Antti Keskinen ---------------------------------------------- "If we wrote a report stating we saw a jet fighter with a howitzer, who's going to believe us ?" -- R.A.F. pilot quote on seeing a Me 262 armed with a 50mm Mauser cannon.
-
In the OnDraw handler of the view class, first create a HBITMAP struct. Then use LoadImage to load the bitmap image from a file into the struct. Then create a CBitmap object and attach it to the HBITMAP. Then use standard BitBlt techniques to blit the bitmap into the view. Here's a code sample:
void CMyView::OnDraw(CDC* pDC)
{
HBITMAP hbmpMyBitmap;
hbmpMyBitmap = (HBITMAP) LoadImage( NULL, "C:\\Windows\\MyBitmap.bmp",
IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE );CBitmap objBitmap;
objBitmap.Attach( hbmpMyBitmap );CDC tempDC;
tempDC.CreateCompatibleDC( pDC );
CBitmap* pOldBitmap = (CBitmap*) tempDC.SelectObject( &objBitmap );pDC->BitBlt(x, y, width, height, &tempDC, 0, 0, SRCCOPY);
tempDC.SelectObject( pOldBitmap );
return;
}Something like this. Remember to replace file path, destination coordinates and bitmap width and height with the correct values. Hope it helps, Antti Keskinen ---------------------------------------------- "If we wrote a report stating we saw a jet fighter with a howitzer, who's going to believe us ?" -- R.A.F. pilot quote on seeing a Me 262 armed with a 50mm Mauser cannon.
Thank you.