在stdafx.h中,在#endif // _AFX_NO_AFXCMN_SUPPORT前加入: #include <afxdisp.h> #include <afxpriv2.h> 在***2Dlg.h中加入: pragma once #include <atlbase.h> CComQIPtr<IPicture>m_spIPicture;//是一个public:变量 在**Dlg::OnPaint() 中加入: CDC *pDC; pDC=GetDC(); // find resource in resource file HINSTANCE hInst = AfxGetResourceHandle(); HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(IDR_JPG), "jpg"); // type if (!hRsrc) //return FALSE; MessageBox("找不到资源"); // load resource into memory DWORD len = SizeofResource(hInst, hRsrc); BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc); if (!lpRsrc) //return FALSE; MessageBox("不能读入内存"); if (m_spIPicture) { m_spIPicture.Release(); } // create memory file and load it CMemFile file(lpRsrc, len);
////////////////// // Load from CFile // CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete); ////////////////// // Load from archive--create stream and load from stream. // CArchiveStream arcstream(&ar); ////////////////// // Load from stream (IStream). This is the one that really does it: call // OleLoadPicture to do the work. //
把JPG图像文件作为二进制资源放到可执行文件中,在程序运行过程中通过JPG绘图界面在Dialog的背景绘制消息中进行绘制。
把jpg放到资源里还不简单...
你应该问的是怎么把jpg解码!
http://www.codeproject.com/bitmap/cximage.asp
②怎么将该jpg资源作为Dialog的背景图显示?以上任一条,只要能详细讲述,且正确者,送分100分不用担心,我会另开贴送分请高手多打几个字,让我能学明白,多谢了。在线等待
BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetWindowRect( &rect );
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC *pdc,memdc;
pdc=GetDC();
memdc.CreateCompatibleDC(pdc);
memdc.SelectObject(&bmp);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memdc, 0, 0, SRCCOPY);
return TRUE;
}
BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetWindowRect( &rect );
CBitmap bmp;
bmp.LoadBitmap(IDB_BITMAP1);
CDC *pdc,memdc;
pdc=GetDC();
memdc.CreateCompatibleDC(pdc);
memdc.SelectObject(&bmp);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memdc, 0, 0, SRCCOPY);
return TRUE;
}
只能使位图显示,不能使jpg显示请高手指点,如何使jpg成为Dialog的背景图
#include <afxdisp.h>
#include <afxpriv2.h>
在***2Dlg.h中加入:
pragma once
#include <atlbase.h>
CComQIPtr<IPicture>m_spIPicture;//是一个public:变量
在**Dlg::OnPaint() 中加入:
CDC *pDC;
pDC=GetDC(); // find resource in resource file
HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hRsrc = ::FindResource(hInst,
MAKEINTRESOURCE(IDR_JPG),
"jpg"); // type
if (!hRsrc)
//return FALSE;
MessageBox("找不到资源"); // load resource into memory
DWORD len = SizeofResource(hInst, hRsrc);
BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
if (!lpRsrc)
//return FALSE;
MessageBox("不能读入内存"); if (m_spIPicture) {
m_spIPicture.Release();
} // create memory file and load it
CMemFile file(lpRsrc, len);
//////////////////
// Load from CFile
//
CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete);
//////////////////
// Load from archive--create stream and load from stream.
//
CArchiveStream arcstream(&ar);
//////////////////
// Load from stream (IStream). This is the one that really does it: call
// OleLoadPicture to do the work.
//
HRESULT hr = OleLoadPicture((IStream*)&arcstream, 0, FALSE,
IID_IPicture, (void**)&m_spIPicture); OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
m_spIPicture->get_Width(&hmWidth);
m_spIPicture->get_Height(&hmHeight);
double fX,fY;
fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
if(FAILED(m_spIPicture->Render(*pDC,0,0,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
AfxMessageBox("渲染图像失败!");
//m_spIPicture->Release(); ASSERT(SUCCEEDED(hr) && m_spIPicture);
ASSERT(SUCCEEDED(hr) && m_spIPicture);
FreeResource(hRsrc);
GlobalFree(lpRsrc);
//return bRet;
CDC *pDC;
pDC=GetDC();
为什么最后没有用ReleaseDC(pDC);实际显示的图片比原图片要小,如何显示原图(按像素)这段代码放在OnEraseBkgnd()中如何?和OnPaint()哪个更好?
我放在OnEraseBkgnd()却没能显示出图片,不知何因?这JPG图片虽然能显示为背景了,但是Dialog上的部分控件却被覆盖了,该如何解决?
我的类似的代码在98下无法显示 800*600*24的BMP.
但同一程序,在win2k下正常.