MFC---static控件加载图片

时间:2021-07-29 09:17:56
                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。