定义成员变量
IPicture *m_picture;
OLE_XSIZE_HIMETRIC m_width;
OLE_YSIZE_HIMETRIC m_height;
BOOL m_IsShow;
CString csImagePath;
void CForwardManage2::GetPic(CString PicName)
{
CFile m_file(PicName,CFile::modeRead );
//获取文件长度
DWORD m_filelen = m_file.GetLength();
//在堆上分配空间;
HGLOBAL m_hglobal = GlobalAlloc(GMEM_MOVEABLE,m_filelen);
LPVOID pvdata = NULL;
//锁定堆空间,获取指向堆空间的指针;
pvdata = GlobalLock(m_hglobal);
//将文件数据读区到堆中;
m_file.Read(pvdata,m_filelen);
IStream* m_stream;
GlobalUnlock(m_hglobal);
//在堆中创建流对象;
CreateStreamOnHGlobal(m_hglobal,TRUE,&m_stream);
//利用流加载图像;
OleLoadPicture(m_stream,m_filelen,TRUE,IID_IPicture,(LPVOID*)&m_picture);
m_picture->get_Width(&m_width);
m_picture->get_Height(&m_height);
CDC* dc = GetDC();
m_IsShow = TRUE;
CWnd *pWnd=NULL;
pWnd=GetDlgItem(IDC_STATIC_PIC);
if(pWnd)
{
CRect rect;
pWnd->GetClientRect(rect);
ClientToScreen(rect);
CDC * dc= pWnd->GetDC();
//CDC * dc= GetDC();
CSize sz(rect.Width(), rect.Height());
dc->HIMETRICtoDP( &sz ); // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位
LONG width=rect.Width();
m_picture->Render(dc->GetSafeHdc(),0,0,rect.Width(),rect.Height(),0,m_height,m_width,-m_height,NULL);
}
//SetScrollRange(SB_VERT,0,(int)(m_height/26.45)-rect.Height());
//SetScrollRange(SB_HORZ,0,(int)(m_width/26.45)-rect.Width());
//m_picture->Render(dc->GetSafeHdc(),1,50,(int)(m_width/26.45),(int)(m_height/26.45),0,m_height,m_width,-m_height,NULL);
m_file.Close();
}
onPaint中添加代码
void CForwardManage2::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialogEx::OnPaint() for painting messages
if (m_IsShow==TRUE)
{
CRect rect;
CWnd *pWnd=NULL;
pWnd=GetDlgItem(IDC_STATIC_PIC);
if(pWnd)
{
pWnd->GetClientRect(rect);
ClientToScreen(rect);
//dc->SetWindowOrg(0,pos);
//m_picture->Render(*dc,1-hpos,50-vpos,(int)(m_width/26.45),(int)(m_height/26.45)
// ,0,m_height,m_width,-m_height,NULL);
CDC * dc= pWnd->GetDC();
//CDC * dc= GetDC();
CSize sz(rect.Width(), rect.Height());
//dc->HIMETRICtoDP( &sz ); // 转换 MM_HIMETRIC 模式单位为 MM_TEXT 像素单位 ,sz是图像要显示的大小
LONG width=rect.Width();
m_picture->Render(dc->GetSafeHdc(),0,0,sz.cx,sz.cy,0,m_height,m_width,-m_height,NULL);
}
// m_picture->Render(dc->GetSafeHdc(),0,0,rect.Width(),rect.Height(),0,m_height,m_width,-m_height,NULL);
}
}