⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpggifview.cpp

📁 vc++程序设计与技巧中第8章“图形图像编程”部分的VC++原代码
💻 CPP
字号:
// JPGGifView.cpp : implementation of the CJPGGifView class
//

#include "stdafx.h"
#include "JPGGif.h"

#include "JPGGifDoc.h"
#include "JPGGifView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView

IMPLEMENT_DYNCREATE(CJPGGifView, CView)

BEGIN_MESSAGE_MAP(CJPGGifView, CView)
	//{{AFX_MSG_MAP(CJPGGifView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView construction/destruction

CJPGGifView::CJPGGifView()
{
	// TODO: add construction code here

}

CJPGGifView::~CJPGGifView()
{
}

BOOL CJPGGifView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView drawing

void CJPGGifView::OnDraw(CDC* pDC)
{
	CJPGGifDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	IPicture *pPic; 
	IStream *pStm; 

	CFileStatus fstatus; 
	CFile file; 
	LONG cb; 

	if (file.Open("C:\\flore.jpg",CFile::modeRead)
		&& file.GetStatus("C:\\flore.jpg", 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);
					
					if(FAILED(pPic->Render(*pDC,100,50,(DWORD)fX,(DWORD)fY,
						0,hmHeight,hmWidth,-hmHeight,NULL))) 
						AfxMessageBox("Failed To Render The picture!");
					pDC->TextOut(120,175,"这是一个JPG图像");
					pPic->Release(); 
				} 
				else
				{
					AfxMessageBox("Error Loading Picture From Stream!"); 
				}
			} 
		} 
	} 
	else 
	{
		AfxMessageBox("Can't Open Image File!");
	}

}

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView printing

BOOL CJPGGifView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CJPGGifView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CJPGGifView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView diagnostics

#ifdef _DEBUG
void CJPGGifView::AssertValid() const
{
	CView::AssertValid();
}

void CJPGGifView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CJPGGifDoc* CJPGGifView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJPGGifDoc)));
	return (CJPGGifDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CJPGGifView message handlers

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -