ATL中的IPictureDisp和IPicture的互相转换以及IPicture的绘图

在ATL的项目中的控件类中的OnDraw函数中进行绘图操作,以下代码在OnDraw函数中实现:

RECT& rc = *(RECT*)di.prcBounds;
CComPtr m_picdisp = NULL;//IPictureDisp对象指针
_bstr_t m_bstrImgUrl = "C:\\桌面\\1.bmp";//bmp图片格式的路径
HRESULT hr = NOERROR;
PICTDESC pictDesc;
pictDesc.picType = PICTYPE_BITMAP;
pictDesc.bmp.hbitmap = (HBITMAP)::LoadImage(NULL, m_bstrImgUrl, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);//加载bmp格式的图片
pictDesc.bmp.hpal = nullptr;
pictDesc.cbSizeofstruct = sizeof(PICTDESC);
if(!SUCCEEDED(hr = OleCreatePictureIndirect(&pictDesc, IID_IPictureDisp, TRUE, (LPVOID*)&m_picdisp)))//将图片对象转换为IPictureDisp对象
{
	return S_FALSE;
}
CComPtr pIPic = NULL;
DWORD dwAttr;
if (SUCCEEDED(hr = m_picdisp->QueryInterface(IID_IPicture, (LPVOID*)&pIPic)))//将IPictureDisp对象转换为IPicture对象
{
	pIPic->get_Attributes(&dwAttr);
	if (dwAttr == S_OK)
	{
		OLE_XSIZE_HIMETRIC hmWidth;
		OLE_XSIZE_HIMETRIC hmHeight;
		pIPic->get_Width(&hmWidth);//获取图像的宽度
		pIPic->get_Height(&hmHeight);//获取图像的高度
        //利用IPicture对象进行绘图
		pIPic->Render(di.hdcDraw,
			rc.left,
			rc.top,
			rc.right - rc.left,
			rc.bottom - rc.top,
			0,
			hmHeight,
			hmWidth,
			-hmHeight,
			&rc);
	}
}
return S_OK;

 

你可能感兴趣的:(ATL中的IPictureDisp和IPicture的互相转换以及IPicture的绘图)