/*///
// 1:Load bitmap到内存中,并用句柄唯一标示.
// 2:用该句柄和一个CBitmap对象乡关联.
// 3:正常使用CBitmap对象.最多的操作是使用BitBlt();
// 4:用完后要释放CBitmap对象和句柄对应的资源.Detach()和DeleteObject();
// 其中,bmp的结构信息(如长和宽等)要把bmp转成BITMAP结构后才能获得.GetObject()和GetBitmap
*///
//有点象菜单的使用.先导入到一个对象(或结构)中,然后提取,在弄到程序中
HBITMAP m_hBitmap;
m_hBitmap = NULL;
char* lpszPathName="test.bmp";//文件名,必须为bmp--只能在当前目录下.实验了,不是的.比如D://类名和COM类名.bmp就可以.出现//的原因是转意字符的应用.在字符串中,’/’是转意字符,要能读取’/’,前面加个’/’就好了.前一个是转意字符,后一个是盘符的标志.
BITMAP m_bmif;//位图结构,用于存放bmp文件的各种数据信息,如下:
/*
typedef struct tagBITMAP { // bm
LONG bmType; //Specifies the bitmap type. This member must be zero.
LONG bmWidth;
LONG bmHeight;
LONG bmWidthBytes;//Specifies the number of bytes in each scan line.
//This value must be divisible by 2,
//because the system assumes that the bit values of a bitmap form an array that is word aligned.
WORD bmPlanes; //Specifies the count of color planes.
WORD bmBitsPixel; //Specifies the number of bits required to indicate the color of a pixel
LPVOID bmBits; //Pointer to the location of the bit values for the bitmap.
// The bmBits member must be a long pointer to an array of character (1-byte) values.
} BITMAP;
*/
CSize m_ViewSize;
/*
typedef struct tagSIZE { // siz
LONG cx;
LONG cy;
} SIZE;*/
//导入位图到内存,返回一个bmp的句柄来唯一标示--LoadImage只能导入位图,光标和图标
m_hBitmap=(HBITMAP)LoadImage(NULL,lpszPathName, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION/*到入位图无关的image*/);
//最后应该使用DeleteObject()来释放资源
/*
HANDLE LoadImage(
HINSTANCE hinst, // handle of the instance containing the image.
//To load an OEM image, set this parameter to zero.
LPCTSTR lpszName, // name or identifier of image名字或ID
UINT uType, // type of image类型
int cxDesired, // desired width
int cyDesired, // desired height
UINT fuLoad // load flags
);
Loads the image from the file specified by the lpszName parameter. If this flag is not specified, lpszName is the name of the resource.
*/
if(m_hBitmap)
{
//把用内存中的信息填充BITMAP结构,为的是获得bmp的长和高而已
::GetObject (m_hBitmap, sizeof(BITMAP), &m_bmif);
m_ViewSize.cx = m_bmif.bmWidth;
m_ViewSize.cy = m_bmif.bmHeight;
// UpdateView();
}else
{
m_ViewSize.cx = 100;
m_ViewSize.cy = 100;
}
if(m_hBitmap)
{
CBitmap bmp;
bmp.Attach(m_hBitmap);//把m_hBitmap选到CBitmap对象中,供后面使用
// Get the size of the bitmap.获取bmp的结构信息到BITMAP结构对象中,下面的BitBlt函数要用.
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);
CDC* pDC=GetDC();//换成GetWindowDC()效果很不错.
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);//创建兼容的上下文设备DC
// Select the bitmap into the in-memory DC.和CPen以及CBrush的原理一样,要先保存旧的,用于当前的CDI对象使用后恢复
CBitmap* pOldBitmap = dcMemory.SelectObject(&bmp);
//Show the bitmap
pDC->BitBlt(0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,
0, 0, SRCCOPY);
//还原CDI对象
dcMemory.SelectObject(&pOldBitmap);
bmp.Detach();
}
DeleteObject(m_hBitmap);//释放资源,良好的习惯.