Loading and moving Bitmaps in Visual C++
#1 Members - Reputation: 122
Posted 12 October 1999 - 06:24 AM
#2 Members - Reputation: 739
Posted 11 October 1999 - 11:20 AM
For loading:
LoadImage
make sure to flag LR_LOADFROMFILE
You can put a generic bitmap area in your dialog using the resource editor in which to display your bitmap, and the class wizard to get you an instance of that bitmap area's control for putting the right bitmap in the box. If you are going to be using an awful lot of bitmaps you might want to look at CImageList. The control that displays bitmaps is a CStatic, and you can put in the bitmap using the SetBitmap function of that control.
Remember that everything you can display in MFC is a window somewhere up the heirarchy, so you can move the window around by using:
SetWindowPos
Remember that because this is a child of the dialog you have, you're going to have to
move it relative to the rect of the parent.
luck
-fel
#3 Members - Reputation: 122
Posted 12 October 1999 - 06:24 AM
it's my code, so few comments
code:
PDIB CEditImageDlg:ibOpenFile(CString szFile)
{
HFILE fh;
DWORD dwLen, dwBits;
PDIB pdib;
LPVOID p;
OFSTRUCT of;
#if defined(WIN32) | | defined (_WIN32)
#define GetCurrentInstance() GetModuleHandle(NULL)
#else
#define GetCurrentInstance() (HINSTANCE)SELECTOROF((LPVOID)&of)
#endiffh = OpenFile(szFile, &of, OF_READ);
if(fh == -1)
{
HRSRC h;
h = FindResource(GetCurrentInstance(), szFile, RT_BITMAP);#if defined(WIN32) | | defined(_WIN32)
if(h)
return (PDIB)LockResource(LoadResource(GetCurrentInstance(), h));
#else
if(h)
fh = AccessResource(GetCurrentInstance(), h);
#endif
}if(fh == -1)
return NULL;pdib = DibReadBitmapInfo(fh);
if(!pdib)
return NULL;dwBits = pdib->biSizeImage;
dwLen = pdib->biSize + DibPaletteSize(pdib) + dwBits;p = GlobalReAllocPtr(pdib, dwLen, 0);
if(!p)
{
GlobalFreePtr(pdib);
pdib = NULL;
}
else
{
pdib = (PDIB)p;
}
if(pdib)
{
_hread(fh, (LPBYTE)pdib + (UINT)pdib->biSize + DibPaletteSize(pdib), dwBits);
}
_lclose(fh);
return pdib;
}
PDIB CEditImageDlg:ibReadBitmapInfo(HFILE fh)
{
DWORD off;
HANDLE hbi = NULL;
int size;
int i;
int nNumColors;RGBQUAD FAR *pRgb;
BITMAPINFOHEADER bi;
BITMAPCOREHEADER bc;
BITMAPFILEHEADER bf;
PDIB pdib;if(fh == -1)
return NULL;off = _llseek(fh, 0L, SEEK_CUR);
if(sizeof(bf) != _lread(fh, (LPSTR)&bf, sizeof(bf)))
return FALSE;if(bf.bfType != BFT_BITMAP)
{
bf.bfOffBits = 0L;
_llseek(fh, off, SEEK_SET);
}if(sizeof(bi) != _lread(fh, (LPSTR)&bi, sizeof(bi)))
return FALSE;switch(size = (int)bi.biSize)
{
default:
case sizeof(BITMAPINFOHEADER):
break;case sizeof(BITMAPCOREHEADER):
bc = *(BITMAPCOREHEADER*)&bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = (DWORD)bc.bcWidth;
bi.biHeight = (DWORD)bc.bcHeight;
bi.biPlanes = (UINT)bc.bcPlanes;
bi.biBitCount = (UINT)bc.bcBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;_llseek(fh, (LONG)sizeof(BITMAPCOREHEADER)-sizeof(BITMAPINFOHEADER), SEEK_CUR);
break;
}nNumColors = DibNumColors(&bi);
#if 0
if(bi.biSizeImage == 0)
bi.biSizeImage = DibSizeImage(&bi);if(bi.biClrUsed == 0)
bi.biClrUsed = DibNumColors(&bi);
#else
FixBitmapInfo(&bi);
#endifpdib = (PDIB)GlobalAllocPtr(GMEM_MOVEABLE, (LONG)bi.biSize + nNumColors * sizeof(RGBQUAD));
if(!pdib)
return NULL;*pdib = bi;
pRgb = DibColors(pdib);
if(nNumColors)
{
if(size == sizeof(BITMAPCOREHEADER))
{
_lread(fh, (LPVOID)pRgb, nNumColors * sizeof(RGBTRIPLE));for(i = nNumColors - 1; i >=0; i--)
{
RGBQUAD rgb;
rgb.rgbRed = ((RGBTRIPLE FAR*)pRgb)[i].rgbtRed;
rgb.rgbBlue = ((RGBTRIPLE FAR*)pRgb)[i].rgbtBlue;
rgb.rgbGreen = ((RGBTRIPLE FAR*)pRgb)[i].rgbtGreen;
rgb.rgbReserved = (BYTE)0;
}
}
else
_lread(fh, (LPVOID)pRgb, nNumColors * sizeof(RGBQUAD));
}
if(bf.bfOffBits != 0L)
_llseek(fh, off + bf.bfOffBits, SEEK_SET);//m_ColorUsed = nNumColors;
//UpdateData(FALSE);return pdib;
}
for hard coded:
code:
CPaintDC dc(this); // device context for painting
HBITMAP hbitmap = ::LoadBitmap(m_hInstance, MAKEINTRESOURCE(IDB_BITMAP1));HDC hMemDC = ::CreateCompatibleDC(NULL);
SelectObject(hMemDC, hbitmap);
::StretchBlt(dc.m_hDC, 45, 175, 200, 120, hMemDC, 0, 0, 200, 120, SRCCOPY);
:
eleteDC(hMemDC);
:eleteObject(hbitmap);
That's about the MFC manner..........check out the functions I used, and see what you can change. The best way i think is the first, because it's easier to make changes (which I will show you if you want), and to move........and it loads every bmp file....
Hmmmm.....
There are also a lot of other manners. Damn, too many to suit the needs.
------------------
Dance with me......







