JPG背景



void CXXXDlg::OnPaint()
{
CPaintDC dc(this);
CString strFilePath = _T("jpg_res/bg.jpg");
if (PathFileExists(strFilePath)){
IPicture *m_picture;
OLE_XSIZE_HIMETRIC m_width;
OLE_YSIZE_HIMETRIC m_height;
CFile m_file(strFilePath, CFile::modeRead);
//获取文件长度
DWORD m_filelen = static_cast<DWORD>(m_file.GetLength());
//在堆上分配空间
HGLOBAL m_hglobal = GlobalAlloc(GMEM_MOVEABLE, m_filelen);
LPVOID pvdata = NULL;
//锁定堆空间,获取指向堆空间的指针
pvdata = GlobalLock(m_hglobal);
//将文件数据读区到堆中
m_file.Read(pvdata, m_filelen);
IStream*  m_stream;
GlobalUnlock(m_hglobal);
//在堆中创建流对象
CreateStreamOnHGlobal(m_hglobal, TRUE, &m_stream);
//利用流加载图像
OleLoadPicture(m_stream, m_filelen, TRUE, IID_IPicture, (LPVOID*)&m_picture);
m_stream->Release();
m_picture->get_Width(&m_width);// 宽高,MM_HIMETRIC 模式,单位是0.01毫米
m_picture->get_Height(&m_height);
m_file.Close();
CRect rect;
GetClientRect(rect);
int nW, nH;
nW = (int)(rect.Width());
nH = (int)(rect.Height());//90:预留给底部链接的位置
if (FAILED(m_picture->Render(dc, 0, 0, nW, nH, 0, m_height, m_width, -m_height, &rect))){
GlobalFree(m_hglobal);//!!!
m_picture->Release();//!!!
return;
}
GlobalFree(m_hglobal);//!!!  不释放GDI泄露
m_picture->Release();//!!!
}
}

你可能感兴趣的:(JPG背景)