Advertisement

Loading and moving Bitmaps in Visual C++

Started by October 12, 1999 12:24 PM
1 comment, last by the Ponderer 25 years, 2 months ago
I'm assuming you have the MSVC help files someplace, so here's what you need to look at:

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

~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
In Visual C++ 6 I am trying to make my program able to load bitmaps from my hard drive and move them around by joystick or keyboard movement and display them with different animations. I am trying to do this from an AppWizard Dialog application. If anyone can get me on the right track they will be highly appreciated.
Advertisement
It's obvious that this can be done in many ways. let me show you some of the ways you can use (only source because I have to leave in about 15 minutes)

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)#endif	fh = 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);#endif	pdib = (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).rgbtRed;<BR>				rgb.rgbBlue = ((RGBTRIPLE FAR*)pRgb).rgbtBlue;<BR>				rgb.rgbGreen = ((RGBTRIPLE FAR*)pRgb).rgbtGreen;<BR>				rgb.rgbReserved = (BYTE)0;<BR>			}<BR>		}<BR>		else<BR>			_lread(fh, (LPVOID)pRgb, nNumColors * sizeof(RGBQUAD));<BR>	}<BR>	if(bf.bfOffBits != 0L)<BR>		_llseek(fh, off + bf.bfOffBits, SEEK_SET);<P>	//m_ColorUsed = nNumColors;<BR>	//UpdateData(FALSE);<P>	return pdib;<BR>}<BR></pre><HR></BLOCKQUOTE><P>for hard coded:<P><BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre><BR>		CPaintDC dc(this); // device context for painting<BR>		HBITMAP hbitmap = ::LoadBitmap(m_hInstance, MAKEINTRESOURCE(IDB_BITMAP1));<P>		HDC hMemDC = ::CreateCompatibleDC(NULL);<P>		SelectObject(hMemDC, hbitmap);<P>		::StretchBlt(dc.m_hDC, 45, 175, 200, 120, hMemDC, 0, 0, 200, 120, SRCCOPY);<P>		: <IMG SRC="http://www.gamedev.net/community/forums/ubb/biggrin.gif">eleteDC(hMemDC);<BR>		: <IMG SRC="http://www.gamedev.net/community/forums/ubb/biggrin.gif">eleteObject(hbitmap);<P></pre><HR></BLOCKQUOTE><P>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….<P>Hmmmm…..<P><BR>There are also a lot of other manners. Damn, too many to suit the needs.<P>——————<BR>Dance with me……

This topic is closed to new replies.

Advertisement