ShowBitmapView.cpp
上传用户:turtfrt
上传日期:2022-06-23
资源大小:36k
文件大小:6k
- // ShowBitmapView.cpp : implementation of the CShowBitmapView class
- //
- #include "stdafx.h"
- #include "ShowBitmap.h"
- #include "ShowBitmapDoc.h"
- #include "ShowBitmapView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView
- IMPLEMENT_DYNCREATE(CShowBitmapView, CView)
- BEGIN_MESSAGE_MAP(CShowBitmapView, CView)
- //{{AFX_MSG_MAP(CShowBitmapView)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView construction/destruction
- // CShowBitmapView 构造/析构
- CShowBitmapView::CShowBitmapView()
- {
- // TODO: add construction code here
- state=0;
- }
- CShowBitmapView::~CShowBitmapView()
- {
-
- }
- BOOL CShowBitmapView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView drawing
- void CShowBitmapView::OnDraw(CDC* pDC)
- {
- CShowBitmapDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- if (!pDoc)
- return;
- if (state==1)
- {
- if (0 == extname.Compare(_T("bmp")))
- {
- CDC dcImage;
- if (!dcImage.CreateCompatibleDC(pDC))
- {
- return;
- }
- BITMAP bm;
- m_bitmap.GetBitmap(&bm);
- dcImage.SelectObject(&m_bitmap);
- pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&dcImage,0,0,SRCCOPY);
- }
- else
- {
- if (0 == extname.Compare(_T("jpg")))
- {
- ShowJpgGif(pDC,BmpName,1,1);
- }
- }
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView printing
- BOOL CShowBitmapView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CShowBitmapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CShowBitmapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView diagnostics
- #ifdef _DEBUG
- void CShowBitmapView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CShowBitmapView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CShowBitmapDoc* CShowBitmapView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CShowBitmapDoc)));
- return (CShowBitmapDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CShowBitmapView message handlers
- BOOL CShowBitmapView::ShowJpgGif(CDC* pDC,CString strPath, int x, int y)
- {
- IStream *pStm;
- CFileStatus fstatus;
- CFile file;
- LONG cb;
- //打开文件并检测文件的有效性
- if (file.Open(strPath,CFile::modeRead)&&
- file.GetStatus(strPath,fstatus)&&
- ((cb = fstatus.m_size) != -1))
- {
- HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
- LPVOID pvData = NULL;
- if (hGlobal != NULL)
- {
- pvData = GlobalLock(hGlobal);
- if (pvData != NULL)
- {
- //file.ReadHuge(pvData, cb); //6.0中可能是用这个函数
- file.Read(pvData, cb); //VC2005.NET中用这个函数
- GlobalUnlock(hGlobal);
- CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
- }
- }
- }
- else
- {
- return false;
- } //打开文件结束
- //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
- //要显示动画GIF请使用ACTIVE控件。
- IPicture *pPic;
- //load image from file stream
- 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;
- //get image height and width
- 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);
- //use render function display image
- if(FAILED(pPic->Render(*pDC,x,y,(DWORD)fX,(DWORD)fY,0,hmHeight,hmWidth,-hmHeight,NULL)))
- {
- pPic->Release();
- return false;
- }
- pPic->Release();
- }
- else
- {
- return false;
- }
- return true;
- }
- void CShowBitmapView::ShowBitmap(CString BmpName)
- {
- if (state==0)
- {
- HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,BmpName,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_DEFAULTSIZE|LR_LOADFROMFILE);
- m_bitmap.Detach();
- m_bitmap.Attach(hBitmap);
- state=1;
- Invalidate();
- }
- }
- void CShowBitmapView::OnFileOpen()
- {
- // TODO: Add your command handler code here
- //CFileDialog dlg(TRUE,_T("BMP"),_T("*.BMP"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP;*.jpg)|*.BMP;*.jpg|")); //这行代码可以打开BMP格式的图片
- CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("位图文件(*.BMP)|*.BMP|jpg文件(*.jpg)|*.jpg||")); //这行代码可以打开BMP和JPG格式的图片
- //gif格式图片的打开在这就不列出来了,留给各位自己尝试吧,对于gif格式图片的显示图片函数和jpg格式图片是同一个函数。
- if (IDOK==dlg.DoModal())
- {
- state=0;
- BmpName.Format(_T("%s"),dlg.GetPathName());
- extname = dlg.GetFileExt(); //返回选定文件的扩展文件名
- extname.MakeLower();
- if (0==extname.Compare(_T("bmp")))
- {
- ShowBitmap(BmpName);
- }
- else
- {
- state=1;
- }
- Invalidate();
- }
-
- }