1、建个了工程, 2、添了个组件(full control),选择Picture属性。 3、发现: CComPtr<IPictureDisp> m_pPicture; 4、搜索MSDN: IPictureDisp The IPictureDisp interface exposes the picture object's properties through Automation. It provides a subset of the functionality available through IPicture methods. (用金山词霸翻译) IPictureDisp 接口通过自动化暴露图片对象的属性。它通过IPicture 的函数提供功能性可用的子集。 5、搜索MSDN: IPicture::SelectPicture Selects a bitmap picture into a given device context, and returns the device context in which the picture was previously selected as well as the picture's GDI handle. This method works in conjunction with IPicture::get_CurDC. HRESULT SelectPicture( HDC hdcIn, //New device context HDC* phdcOut, //Receives a pointer to the previous device context OLE_HANDLE* phbmpOut //Receives a pointer to GDI handle of the picture );6、搜索VCKBASE: http://www.vckbase.com/document/viewdoc/?id=1028 http://www.vckbase.com/document/viewdoc/?id=802 http://www.vckbase.com/document/viewdoc/?id=4467、研究一下先……
2、添了个组件(full control),选择Picture属性。
3、发现: CComPtr<IPictureDisp> m_pPicture;
4、搜索MSDN:
IPictureDisp
The IPictureDisp interface exposes the picture object's properties through Automation. It provides a subset of the functionality available through IPicture methods.
(用金山词霸翻译)
IPictureDisp 接口通过自动化暴露图片对象的属性。它通过IPicture 的函数提供功能性可用的子集。
5、搜索MSDN:
IPicture::SelectPicture
Selects a bitmap picture into a given device context, and returns the device context in which the picture was previously selected as well as the picture's GDI handle. This method works in conjunction with IPicture::get_CurDC. HRESULT SelectPicture(
HDC hdcIn, //New device context
HDC* phdcOut,
//Receives a pointer to the previous device context
OLE_HANDLE* phbmpOut
//Receives a pointer to GDI handle of the picture
);6、搜索VCKBASE:
http://www.vckbase.com/document/viewdoc/?id=1028
http://www.vckbase.com/document/viewdoc/?id=802
http://www.vckbase.com/document/viewdoc/?id=4467、研究一下先……
“
STDAPI OleLoadPicture( IStream * pStream, //指向包含有图像数据的流的指针LONG lSize, //从流中读取的字节数BOOL fRunmode, //图像属性对应的初值REFIID riid, //涉及到的接口标识,描述要返回的接口指针的类型VOID ppvObj // 在rrid中用到的接口指针变量的地址);
”
我去试试……
{
Release(); HINSTANCE hInst = AfxFindResourceHandle(lpszResource, lpszResType);
HRSRC hRsrc = ::FindResource(hInst, lpszResource, lpszResType);
if(hRsrc == NULL) return FALSE;
HGLOBAL hGlobal = LoadResource(hInst, hRsrc);
if(hGlobal == NULL) return FALSE;
DWORD dwSize = SizeofResource(hInst, hRsrc);
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
if(hMem == NULL) return FALSE;
LPVOID pSrc = ::LockResource(hGlobal);
if(pSrc == NULL) {
::GlobalFree(hMem);
return FALSE;
}
LPVOID pDes = ::GlobalLock(hMem);
if(pDes == NULL){
//::GlobalUnlock(hGlobal);
::GlobalFree(hMem);
return FALSE;
}
memcpy(pDes, pSrc, dwSize);
//GlobalUnlock(hGlobal);
GlobalUnlock(hMem);
::FreeResource(hGlobal);
IStream* pStm = NULL;
CreateStreamOnHGlobal(hMem, TRUE, &pStm);
if(!SUCCEEDED(OleLoadPicture(pStm,dwSize,TRUE,IID_IPicture,(LPVOID*)&m_pPic)))
{
pStm -> Release();
::GlobalFree(hMem);
pStm = NULL;
return FALSE;
}
pStm->Release();
::GlobalFree(hMem);
CalcSize();
return TRUE;}BOOL CPicture::LoadPictureFromFile(LPCTSTR lpszFileName)
{
Release(); CFile file;
if(!file.Open(lpszFileName, CFile::modeRead))
return FALSE; DWORD dwSize = file.GetLength();
HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwSize);
if(hMem == NULL) return FALSE;
LPVOID pDes = ::GlobalLock(hMem);
if(pDes == NULL){
::GlobalFree(hMem);
return FALSE;
}
file.ReadHuge(pDes, dwSize);
file.Close(); GlobalUnlock(hMem);
IStream* pStm = NULL;
CreateStreamOnHGlobal(hMem, TRUE, &pStm);
if(!SUCCEEDED(OleLoadPicture(pStm,dwSize,TRUE,IID_IPicture,(LPVOID*)&m_pPic)))
{
pStm -> Release();
::GlobalFree(hMem);
pStm = NULL;
return FALSE;
}
pStm->Release();
::GlobalFree(hMem);
CalcSize();
return TRUE;}
void CPicture::CalcSize()
{
if(m_pPic == NULL) return; m_pPic->get_Width(&_w_him);
m_pPic->get_Height(&_h_him); CDC* pDC = CWnd::GetDesktopWindow()->GetDC();
m_size.cx = _w_him;
m_size.cy = _h_him; pDC->HIMETRICtoDP(&m_size); CWnd::GetDesktopWindow()->ReleaseDC(pDC);}void CPicture::Draw(CDC* pDC, LPCRECT lprcDest, LPCRECT lprcSrc)
{
if(m_pPic)
{
CSize szOrig(lprcSrc->left, lprcSrc->top);
CSize szSrc(lprcSrc->right - lprcSrc->left, lprcSrc->bottom - lprcSrc->top);
pDC->DPtoHIMETRIC(&szOrig);
pDC->DPtoHIMETRIC(&szSrc); m_pPic->Render(*pDC, lprcDest->left,lprcDest->top,lprcDest->right-lprcDest->left,
lprcDest->bottom-lprcDest->top, szOrig.cx, _h_him-szOrig.cy, szSrc.cx,
-szSrc.cy, NULL);
}
}
谢谢上面各位,我试过你们方法,但还不行。我是用ATL开发一个GDI+ ActiveX Control,
加了一个picture 属性: CComPtr<IPictureDisp> m_pPicture
我想在function OnDraw 里初始化它m_pPicture下面是我的代码:
HRESULT OnDraw(ATL_DRAWINFO& di)
{
Graphics graphics(di.hdcDraw);
RECT& rc = *(RECT*)di.prcBounds;
Height = rc.bottom-rc.top;
Width = rc.right-rc.left;Bitmap bmp(Width,Height);
Graphics * pMemoryGraphics=Graphics::FromImage(&bmp);
MyImage * img= new MyImage();
img->DrawGraphics(pMemoryGraphics); // draw the graphics,这函数创建了Bitmap图形bmp
graphics.DrawImage(&bmp,rc.left,rc.top);
// here i created the bitmap "bmp", how to pass it to m_pPicture?
// CComPtr m_pPicture;现在就是要把bmp传给m_pPicture
我试过用下面方法,但不知道怎样将pPicture传给m_pPicture
LPPICTURE pPicture;
PICTDESC pd = {sizeof(PICTDESC),PICTYPE_BITMAP };
HBITMAP hbm = NULL;
bmp.GetHBITMAP(NULL, &hbm);
pd.bmp.hbitmap = hbm;
pd.bmp.hpal = NULL;
OleCreatePictureIndirect(&pd,IID_IPicture,FALSE,(void **)&pPicture);
1、文件将图片打开。
2、读入到IStream中。
3、从流中装载图像。
4、绘图。
上面方法的问题在于:
1、是MFC实现。 ——文件操作需要修改。
2、使用IPicture接口。
(未完,下班后,继续思考)
void CObj::initPic(CComPtr<IPictureDisp> &pPicture,LPCTSTR path)
{
using namespace std; IStream *pStm;
long cb;
fstream file(path, ios_base::in);
file.seekg(0, ios::end);
cb = file.tellg(); HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
file.seekg(0,ios::beg);
file.read((char*)pvData,cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
}
file.close();
OleLoadPicture(pStm,cb,TRUE,IID_IPictureDisp,(LPVOID*)&m_pPicture);
}void CObj::drawPic(ATL_DRAWINFO& di)
{
RECT& rc = *(RECT*)di.prcBounds;
LPPICTURE pPict ;
DWORD dwAttr ;
OLE_XSIZE_HIMETRIC cxSrc;
OLE_YSIZE_HIMETRIC cySrc;
if ((m_pPicture != NULL) &&SUCCEEDED(m_pPicture->QueryInterface(IID_IPicture, (LPVOID*)&pPict)))
{
pPict->get_Attributes(&dwAttr);
if(dwAttr==S_OK)
{
pPict->get_Width(&cxSrc);
pPict->get_Height(&cySrc);
pPict->Render(di.hdcDraw,rc.left, rc.top, rc.right, rc.bottom,0,0,cxSrc,cySrc,&rc);
}
}
}2、然后添加接口函数
STDMETHODIMP CObj::init()
{
// TODO: Add your implementation code here initPic(m_pPicture,"F:\\obj.bmp");
this->FireViewChange(); return S_OK;
}在 HRESULT OnDraw(ATL_DRAWINFO& di) 里添加:
drawPic(di);3、在外部调用该接口函数现在的问题是:
我在构造函数里初始化该变量没有用!!!
这个问题,楼主可重开贴问高人。
{
initPic(m_pPicture,"F:\\obj.bmp");
}void CObj::initPic(IPictureDisp* pPicture,LPCTSTR path)
{
using namespace std; long cb;
fstream file(path, ios_base::in);
file.seekg(0, ios::end);
cb = file.tellg(); CComPtr<IStream> pStm;
HGLOBAL hGlob = GlobalAlloc(GHND, cb);
LPVOID pvData = NULL;
if (hGlob )
{
if ((pvData = GlobalLock(hGlob )) != NULL)
{
file.seekg(0,ios::beg);
file.read((char*)pvData,cb);
GlobalUnlock(hGlob );
CreateStreamOnHGlobal(hGlob , TRUE, &pStm);
}
}
m_pPicture = NULL;
OleLoadPicture(pStm,cb,FALSE,IID_IPictureDisp,(LPVOID*)&m_pPicture);
pStm.Release();
GlobalFree(hGlob );
file.close(); this->m_bRequiresSave = TRUE;
this->FireOnChanged(DISPID_PICTURE);
this->FireViewChange();
}//绘制图片的函数
void CObj::drawPic(ATL_DRAWINFO& di)
{
RECT& rc = *(RECT*)di.prcBounds;
LPPICTURE pPict ;
DWORD dwAttr ;
OLE_XSIZE_HIMETRIC cxSrc;
OLE_YSIZE_HIMETRIC cySrc;
if ((m_pPicture != NULL) &&SUCCEEDED(m_pPicture->QueryInterface(IID_IPicture, (LPVOID*)&pPict)))
{
pPict->get_Attributes(&dwAttr);
if(dwAttr==S_OK)
{
pPict->get_Width(&cxSrc);
pPict->get_Height(&cySrc);
pPict->Render(di.hdcDraw,rc.left, rc.top, rc.right, rc.bottom,0,0,cxSrc,cySrc,&rc);
}
}
} HRESULT OnDraw(ATL_DRAWINFO& di)
{
drawPic(di); return S_OK;
}