VC中怎样把jpg的图片加载到对话框中作为背景解决办法
VC中怎样把jpg的图片加载到对话框中作为背景
我想把JPG格式的一整张图片作为对话框的背景,这和格式引入不了啊
------解决方案--------------------
http://www.pconline.com.cn/pcedu/empolder/gj/vc/0607/820674.html第9课第9节
------解决方案--------------------
我想把JPG格式的一整张图片作为对话框的背景,这和格式引入不了啊
------解决方案--------------------
http://www.pconline.com.cn/pcedu/empolder/gj/vc/0607/820674.html第9课第9节
------解决方案--------------------
- C/C++ code
bool CreateSurfaceFromImg( DXSURFACE** ppSur, const WCHAR* strBMP, DWORD dwImgW,/* = 0*/ DWORD dwImgH/* = 0*/ ) { WCHAR wstrFileName[MAX_PATH]; HANDLE hFile=::CreateFileW(strBMP, GENERIC_READ ,0,0,OPEN_EXISTING, 0, 0); if (INVALID_HANDLE_VALUE==hFile) { wsprintfW(wstrFileName,L"Can not open the file %s",strBMP); MessageBoxW(NULL,wstrFileName,L"error",0L); return false; } DWORD dwSize = ::GetFileSize(hFile,NULL); if (0 == dwSize ) { wsprintfW(wstrFileName,L"File %s size invalid",strBMP); MessageBoxW(NULL,wstrFileName,L"error",0L); return false; } //在堆中分配一块可移动的内存(利于内存回收) HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE , dwSize); if(!hGlobal) { wsprintfW(wstrFileName,L"File %s Error allocating memory",strBMP); MessageBoxW(NULL,wstrFileName,L"error",0L); return false; } //锁定内存将指针指向内存的首字节 char* pData =(char*)(::GlobalLock(hGlobal)); if(!pData) { wsprintfW(wstrFileName,L"File %s Error locking memory",strBMP); MessageBoxW(NULL,wstrFileName,L"error",0L); GlobalFree(hGlobal); return false; } //将文件数据读入内存块 DWORD dwRead;//这个参数设为NULL的话编译通过,运行出错. if(false == ::ReadFile(hFile,pData,dwSize,&dwRead,NULL)) { wsprintfW(wstrFileName,L"Read File %s Error",strBMP); MessageBoxW(NULL,wstrFileName,L"error",0L); ::GlobalFree(hGlobal); ::GlobalUnlock(hGlobal); ::CloseHandle(hFile); return false; } ::GlobalUnlock(hGlobal); ::CloseHandle(hFile); IPicture* pPicture = NULL; IStream* pStream = NULL; // 不能删除内存,由pStream接管内存 if(::CreateStreamOnHGlobal(hGlobal, false, &pStream) != S_OK) { MessageBoxW(NULL,L"CreateStreamOnHGlobal",L"error",0L); return false; } if (::OleLoadPicture(pStream,dwSize, false,IID_IPicture,(LPVOID*)(&pPicture)) != S_OK) { pStream->Release(); MessageBoxW(NULL,L"OleLoadPicture",L"error",0L); return false; } //释放内存块 ::GlobalFree(hGlobal); pStream->Release(); // 储存图片的尺寸 OLE_XSIZE_HIMETRIC hmW; OLE_YSIZE_HIMETRIC hmH; pPicture->get_Width(&hmW); pPicture->get_Height(&hmH); //在提供默认参数时计算出图片的实际尺寸 if (0==dwImgW && 0==dwImgH) { HDC hWinDC=::GetDC(NULL); dwImgW = ::MulDiv(hmW, ::GetDeviceCaps(hWinDC, LOGPIXELSX), 2540); dwImgH = ::MulDiv(hmH, ::GetDeviceCaps(hWinDC, LOGPIXELSY), 2540); ::ReleaseDC(NULL, hWinDC); } if(S_OK != CreateSurface(ppSur,dwImgW,dwImgH)) { return false; } HDC hDC; (*ppSur)->pdds->GetDC( &hDC ); pPicture->Render(hDC,0,0,dwImgW,dwImgH ,0,hmH,hmW,-hmH,NULL); pPicture->Release(); (*ppSur)->pdds->ReleaseDC( hDC ); ::DeleteDC( hDC ); return true; } //******************************************************************// HRESULT BltFast( int nPX,int nPY,DXSURFACE* pSur,const RECT* prcSour) { RECT rc; if (NULL == prcSour) SetRect(&rc,0,0,pSur->nWidth,pSur->nHeight); else CopyRect(&rc,prcSour); if ( nPX > g_nBackBufferW ||nPY > g_nBackBufferH ||nPX + (rc.right - rc.left) < 1 ||nPY + (rc.bottom - rc.top) < 1 ) return S_OK; //裁剪矩形 if (nPX < 0 ) { rc.left -= nPX; nPX = 0 ; } if (nPY < 0 ) { rc.top -= nPY; nPY = 0 ; } if (nPX+(rc.right-rc.left) > g_nBackBufferW) { rc.right -= nPX+(rc.right-rc.left)-g_nBackBufferW; } if (nPY+(rc.bottom-rc.top) > g_nBackBufferH) { rc.bottom -= nPY+(rc.bottom-rc.top)-g_nBackBufferH; } if (pSur->bColorKey) return g_pBackBuffer->BltFast(nPX,nPY,pSur->pdds, &rc,DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT); else return g_pBackBuffer->BltFast(nPX,nPY,pSur->pdds, &rc,DDBLTFAST_WAIT); }