📄 cimageview.cpp
字号:
// CImageView.cpp : implementation of the CCImageView class
//
#include "stdafx.h"
#include "CImage.h"
#include "CImageDoc.h"
#include "CImageView.h"
#include "Translation.h"
#include "RotateAngle.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCImageView
IMPLEMENT_DYNCREATE(CCImageView, CView)
BEGIN_MESSAGE_MAP(CCImageView, CView)
//{{AFX_MSG_MAP(CCImageView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_COMMAND(ID_ROTATE_ANGLE, OnRotateAngle)
ON_COMMAND(ID_TRANSLATION, OnTranslation)
ON_COMMAND(ID_VMIRROR, OnVMirror)
ON_COMMAND(ID_HMIRROR, OnHmirror)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CCImageView construction/destruction
CCImageView::CCImageView()
{
// TODO: add construction code here
rect.bottom = rect.top = 0;
rect.left = rect.right = 0;
}
CCImageView::~CCImageView()
{
}
BOOL CCImageView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCImageView drawing
void CCImageView::OnDraw(CDC* pDC)
{
CCImageDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CClientDC dc(this);
Jpeg.OuttoDC(&dc);
Img.Draw2DC(&dc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCImageView printing
BOOL CCImageView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCImageView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCImageView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCImageView diagnostics
#ifdef _DEBUG
void CCImageView::AssertValid() const
{
CView::AssertValid();
}
void CCImageView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCImageDoc* CCImageView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCImageDoc)));
return (CCImageDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCImageView message handlers
void CCImageView::OnFileOpen()
{
// TODO: Add your command handler code here
CClientDC dc(this);
char filt[220];
strcpy(filt,"Image Files(*.bmp)|*.bmp;|Image Files(*.jpg)|*.jpg;|");
CFileDialog fd(true,"bmp", NULL, OFN_FILEMUSTEXIST,filt,this);
if(fd.DoModal()!=IDOK) return;
CString lpFileName=fd.GetPathName();
lpFileName.MakeUpper();
char drive[260];
char dir[256];
char fname[256];
char ext[256];
_splitpath(lpFileName, drive, dir, fname, ext);
if(!stricmp(ext, ".jpg"))
{
Jpeg.Load(lpFileName);
Jpeg.OuttoDC(&dc);
}
else if (!stricmp(ext, ".bmp"))
{
Img.LoadBmp(lpFileName);
Img.Draw2DC(&dc);
}
Invalidate();
}
void CCImageView::OnEditPaste()
{
// TODO: Add your command handler code here
if(Img.IsBufferValid()==false) return;
CClientDC dc(this);
BYTE* pRect;
rect.bottom = 300;
rect.right = 300;
Img.GetImgRect( rect, pRect);
DWORD dwWidth, dwHeight;//被选中范围的图像的属性
dwWidth = rect.right - rect.left;
dwHeight = rect.bottom - rect.top;
if(!pRect) return;
BITMAPINFO bmi;
DWORD BytePerPixel = 4;
DWORD dwSize = 100*100*BytePerPixel;//计算图像数据的大小
//填充位图信息头
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = dwWidth;
bmi.bmiHeader.biHeight = -dwHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = BytePerPixel*8;
bmi.bmiHeader .biClrImportant = 0;
bmi.bmiHeader .biSizeImage = dwSize;
bmi.bmiHeader.biXPelsPerMeter = dwWidth;
bmi.bmiHeader.biYPelsPerMeter = dwHeight;
bmi.bmiHeader.biCompression = 0;
bmi.bmiHeader .biClrUsed = 0;
StretchDIBits(dc.GetSafeHdc(), dwWidth+10, 0, dwWidth, dwHeight,
0, 0, dwWidth, dwHeight, (UINT *)pRect, &bmi,
DIB_RGB_COLORS,SRCCOPY);
}
void CCImageView::OnRotateAngle()
{
// TODO: Add your command handler code here
CRotateAngle RotAgl;
if(!RotAgl.DoModal())
return;
double fRotateAngle;
fRotateAngle = RotAgl.m_fRotateAngle;
Img.RotateAngle(fRotateAngle);
Invalidate();
}
void CCImageView::OnTranslation()
{
// TODO: Add your command handler code here
if(Img.IsBufferValid() == false) return;
CTranslation Trst;
if(!Trst.DoModal())
return;
DWORD dwXOffset, dwYOffset;
dwXOffset = Trst.m_dwXOffset;
dwYOffset = Trst.m_dwYOffset;
Img.Translation( dwXOffset, dwYOffset);
Invalidate();
}
//垂直镜像
void CCImageView::OnVMirror()
{
// TODO: Add your command handler code here
bool bDirection = false;
Img.Mirror(bDirection);
Invalidate();
}
void CCImageView::OnHmirror()
{
// TODO: Add your command handler code here
bool bDirection = true;
Img.Mirror(bDirection);
Invalidate();
}
void CCImageView::OnFileSave()
{
// TODO: Add your command handler code here
CString strFileName;
static char szFilter[] = "BMP文件(*.bmp)|*.bmp||";
CFileDialog dlg(false,"bmp",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if(dlg.DoModal() !=IDOK) return;
strFileName = dlg.GetPathName();
Img.SaveBmp(strFileName);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -