📄 ii.txt
字号:
在view类添加
CString JpgName;
BOOL DrawImg(CString strFile,
CDC *pDC,
CRect rc,
BOOL bStretch) ;
int State1; (构造函数中初始化为0)
2.view类中实现上面的DrawImg函数。
BOOL ——View::DrawImg(CString strFile, CDC *pDC, CRect rc, BOOL bStretch)
{
IPicture *pPic;
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
if (file.Open(strFile,CFile::modeRead) && file.GetStatus(strFile, fstatus) && ((cb = fstatus.m_size) != 1))
{
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
if ((pvData = GlobalLock(hGlobal)) != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
if(SUCCEEDED(
OleLoadPicture(pStm,fstatus.m_size,TRUE,
IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
double fX,fY;
fX = (double)pDC->GetDeviceCaps(HORZRES)* (double)hmWidth/ ((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)* (double)hmHeight/ ((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
int left = rc.left;
int top = rc.top;
int width = rc.Width();
int height = rc.Height();
if (bStretch)
{
if (width == 0) width = (int)fX;
if (height == 0) height = (int)fY;
}
else
{
width = (int)fX;
height = (int)fY;
}
if(FAILED(pPic->Render(*pDC, left, top, width, height, 0, hmHeight, hmWidth, -hmHeight, NULL)))
{
AfxMessageBox("Failed To Render The picture!");
return FALSE;
}
pPic->Release();
}
else
{
AfxMessageBox("Error Loading Picture From Stream!");
return FALSE;
}
}
}
}
else
{
AfxMessageBox("Can't Open Image File!");
return FALSE;
}
return TRUE;
}
3.添加菜单,ID为ID_OPENJPG,在view类中添加相应的消息响应函数
void ——View::OnOpenjpg()
{
// TODO: Add your command handler code here
if(State1==1)
{
State1=0;
}
CString sFilter;
sFilter = ("图象文件(*.bmp;*.jpg;*.jpeg)|*.bmp;*.jpg;*.jpeg|所有文件(*.*)|*.*||");
CFileDialog dlg(TRUE, NULL, NULL,
OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, sFilter, this);
if(IDOK==dlg.DoModal())
{
JpgName.Format("%s",dlg.GetPathName());
State1=1;
}
Invalidate();
}
4,在onDraw函数中添加代码
if(State1==1)
{
CRect rc;
GetClientRect(&rc);//得到客户区大小
BOOL bStretch=TRUE;//把图片拉伸和窗口一样大
CMyPictureShopView::DrawImg(JpgName,
pDC,
rc,
bStretch) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -