IPicture* ppic = NULL;
HRESULT hr;
hr = OleLoadPicturePath((CComBSTR)picpath.GetBuffer(),(LPUNKNOWN)NULL,0,0,IID_IPicture,(LPVOID*)&ppic);
if (SUCCEEDED(hr))
{
(CStatic*)(ctrlpwnd)->ModifyStyle(0xF,SS_BITMAP|SS_CENTERIMAGE);
CDC* pdc = ctrlpwnd->GetDC();
CRect rect;
ctrlpwnd->GetWindowRect(&rect);
pwnd->ScreenToClient(&rect);
OLE_XPOS_HIMETRIC xSrc = 0; // 圖片中當前顯示區域的x
OLE_YPOS_HIMETRIC ySrc = 0; // 圖片中當前顯示區域的y
OLE_XSIZE_HIMETRIC cxSrc = rect.Width(); // 圖片中當前顯示區域的寬度
OLE_YSIZE_HIMETRIC cySrc = rect.Height(); // 圖片中當前顯示區域的高度
ppic->get_Width(&cxSrc);
ppic->get_Height(&cySrc);
(ppic->Render(*pdc,0,0,rect.Width(),rect.Height(),0,cySrc,cxSrc,-cySrc,&rect));
ppic->Release();
pwnd->ReleaseDC(pdc);
ppic = NULL;
可以加載 jpg,bmp。
IPicture::Render簡介
HRESULT Render( HDC hdc, //Handle of device context on which to render the image
long x, //Horizontal position of image in hdc
long y, //Vertical position of image in hdc
long cx, //Horizontal dimension of destination rectangle
long cy, //Vertical dimension of destination rectangle
OLE_XPOS_HIMETRIC xSrc, //Horizontal offset in source picture
OLE_YPOS_HIMETRIC ySrc, //Vertical offset in source picture
OLE_XSIZE_HIMETRIC cxSrc, //Amount to copy horizontally in source picture
OLE_YSIZE_HIMETRIC cySrc, //Amount to copy vertically in source picture
LPCRECT prcWBounds //Pointer to position )
x,y,cx,cy分別是指在屏幕上顯示的左上角和右下角;
xsrc,ysrc,cxsrc,cysrc分別是指圖片的大小,所以屏幕上長寬的比例和圖片長寬的比例是一樣的。
有一點令人不解 0,hmHeight,hmWidth,-hmHeight對應於xsrc,ysrc,cxsrc,cysrc,並不是以0,0開頭。(否則圖片的方向會錯誤)
prcWBounds 一般賦值為NULL。
