ATL控件Picture属性赋值方法

类别:VC语言 点击:0 评论:0 推荐:

//接口函数
STDMETHODIMP CObj::init()
{
 // TODO: Add your implementation code here
 initPic(m_pPicture,"F:\\obj.bmp");

 return S_OK;
}

//初始化m_pPicture的函数
void CObj::initPic(CComPtr<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)
 {
  RECT& rc = *(RECT*)di.prcBounds;
  Rectangle(di.hdcDraw, rc.left, rc.top, rc.right, rc.bottom);

  SetTextAlign(di.hdcDraw, TA_CENTER|TA_BASELINE);
  LPCTSTR pszText = _T("ATL 3.0 : Obj");
  TextOut(di.hdcDraw,
   (rc.left + rc.right) / 2,
   (rc.top + rc.bottom) / 2,
   pszText,
   lstrlen(pszText));

  drawPic(di); //通过这句绘制

  return S_OK;
 }

本文地址:http://com.8s8s.com/it/it37.htm