// testView.cpp : implementation of the CTestView class // #include "stdafx.h" #include "test.h" #include "testDoc.h" #include "testView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CTestView IMPLEMENT_DYNCREATE(CTestView, CView) BEGIN_MESSAGE_MAP(CTestView, CView) //{{AFX_MSG_MAP(CTestView) ON_COMMAND(ID_BIG, OnBig) ON_COMMAND(ID_SMALL, OnSmall) 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() ///////////////////////////////////////////////////////////////////////////// // CTestView construction/destruction CTestView::CTestView() { // TODO: add construction code here filename = ""; m_picture = NULL; size = 1; } CTestView::~CTestView() { } BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CTestView drawing void CTestView::OnDraw(CDC* pDC) { CTestDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if(m_picture) { long width; long height; m_picture->get_Width(&width); //得到图片的宽 m_picture->get_Height(&height); //得到图片的高 int nwidth=MulDiv(width,GetDeviceCaps(pDC->GetSafeHdc(),LOGPIXELSX),2540); int nheight=MulDiv(height,GetDeviceCaps(pDC->GetSafeHdc(),LOGPIXELSY),2540); CRect rc; GetClientRect(&rc); //得到窗口大小 m_picture->Render(pDC->GetSafeHdc(),(rc.Width()-nwidth/size)/2,(rc.Height()-nheight/size)/2, nwidth/size,nheight/size,0,height,width,-height,&rc); } } ///////////////////////////////////////////////////////////////////////////// // CTestView printing BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CTestView diagnostics #ifdef _DEBUG void CTestView::AssertValid() const { CView::AssertValid(); } void CTestView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CTestDoc* CTestView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc))); return (CTestDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CTestView message handlers void CTestView::LoadPicture() { HANDLE hfile=CreateFile(filename,GENERIC_READ,0,NULL,OPEN_EXISTING,0, NULL); DWORD dfilesize=GetFileSize(hfile,NULL); LPVOID p=NULL; HGLOBAL h=GlobalAlloc(GMEM_MOVEABLE,dfilesize); p=GlobalLock(h); DWORD dw=0; BOOL bread=ReadFile(hfile,p,dfilesize,&dw,NULL); GlobalUnlock(h); CloseHandle(hfile); LPSTREAM pstm=NULL; HRESULT hr=CreateStreamOnHGlobal(h,TRUE,&pstm); if(m_picture) m_picture->Release(); hr=::OleLoadPicture(pstm,dfilesize,FALSE,IID_IPicture,(LPVOID *)&m_picture); pstm->Release(); } void CTestView::OnBig() { // TODO: Add your command handler code here size=size*0.909; //图片变大 Invalidate(); } void CTestView::OnSmall() { // TODO: Add your command handler code here size=size*1.1; //图片缩小 Invalidate(); } void CTestView::OnFileOpen() { // TODO: Add your command handler code here static char sfile[]="JPEG(*.JPEG;*.JPE;*.JIF;*.JPG)|*.JPEG;*.JPE;*.JIF;*.JPG|BMP Windows位图(*.BMP;*.DIB;*.RLE)|*.BMP;*.DIB;*.RLE|PNG(*.PNG)|*.PNG||"; CFileDialog dlg(TRUE,"jpg"," ",OFN_ALLOWMULTISELECT,sfile); if(IDOK==dlg.DoModal()) //文件打开对话框 { filename=dlg.GetPathName(); LoadPicture(); } Invalidate(); }