📄 iplabview.cpp
字号:
// IPLabView.cpp : implementation of the CIPLabView class
//
#include "stdafx.h"
#include "IPLab.h"
#include "IPLabDoc.h"
#include "IPLabView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CIPLabView
IMPLEMENT_DYNCREATE(CIPLabView, CScrollView)
BEGIN_MESSAGE_MAP(CIPLabView, CScrollView)
//{{AFX_MSG_MAP(CIPLabView)
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_EDITTOOL_SELECTRECT, OnEdittoolSelectrect)
ON_COMMAND(ID_EDITTOOL_SELECTANY, OnEdittoolSelectany)
ON_COMMAND(ID_EDITTOOL_PASTE, OnEdittoolPaste)
ON_UPDATE_COMMAND_UI(ID_EDITTOOL_PASTE, OnUpdateEdittoolPaste)
ON_COMMAND(ID_EDITTOOL_CUT, OnEdittoolCut)
ON_UPDATE_COMMAND_UI(ID_EDITTOOL_CUT, OnUpdateEdittoolCut)
ON_COMMAND(ID_EDITTOOL_COPY, OnEdittoolCopy)
ON_UPDATE_COMMAND_UI(ID_EDITTOOL_COPY, OnUpdateEdittoolCopy)
ON_COMMAND(ID_EDITTOOL_ENABLE, OnEdittoolEnable)
ON_UPDATE_COMMAND_UI(ID_EDITTOOL_ENABLE, OnUpdateEdittoolEnable)
ON_COMMAND(ID_IP_INV, OnIpInv)
ON_COMMAND(ID_IP_INV_INREGION, OnIpInvInregion)
ON_COMMAND(ID_IP_INV_INREGION_F, OnIpInvInregionF)
ON_COMMAND(ID_IP_HITTEST, OnIpHittest)
ON_WM_SETCURSOR()
ON_COMMAND(ID_IP_INV_INREGION_O, OnIpInvInregionO)
ON_COMMAND(ID_LEASTSQUARESFITTING, OnLeastsquaresfitting)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIPLabView construction/destruction
CIPLabView::CIPLabView()
{
//初始化ViewAction列表
m_cViewActionEditTool.SetID(IMAGEEDIT);
m_cViewActionEditTool.SetParent(this);
m_listViewActionList.AddHead(&m_cViewActionEditTool);
m_cViewActionImageTool.SetID(IMAGEFITTING);
m_cViewActionImageTool.SetParent(this);
m_listViewActionList.AddHead(&m_cViewActionImageTool);
m_bEditToolEnable = TRUE;
this->m_ToolOption = IMAGEFITTING;
m_cViewActionImageTool.SetEditToolOption(TOOLEASTSQUARESFITTING);
}
CIPLabView::~CIPLabView()
{
}
BOOL CIPLabView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CIPLabView drawing
void CIPLabView::OnDraw(CDC* pDC)
{
CIPLabDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//无闪烁刷屏技术的实现
//http://www.crazy-bit.com/articles/noflash.htm
CSize size = GetTotalSize();
CDC MemDC;
CBitmap MemBitmap;
MemDC.CreateCompatibleDC(NULL);
MemBitmap.CreateCompatibleBitmap(pDC,size.cx, size.cy);
CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
//MemDC.FillSolidRect(0,0,size.cx, size.cy,RGB(255,255,255));
if (pDoc->GetBitmap()) {
pDoc->Draw(&MemDC);
//draw annotate
if (m_bEditToolEnable) {
m_cViewActionEditTool.Draw(&MemDC,GetZoom(),GetZoom(),0,0);
}
m_cViewActionImageTool.Draw(&MemDC);
}
else
{
Graphics g(MemDC.GetSafeHdc());
SolidBrush br(Color(222,222,222));
g.FillRectangle(&br,0,0,size.cx,size.cy);
}
pDC->BitBlt(0,0,size.cx, size.cy,&MemDC,0,0,SRCCOPY);
MemBitmap.DeleteObject();
MemDC.DeleteDC();
}
void CIPLabView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CIPLabDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CSize sizeTotal;
if (pDoc->pPicture)
{
sizeTotal.cx = GetZoom()*pDoc->GetWidth();
sizeTotal.cy = GetZoom()*pDoc->GetHeight();
}
else{
sizeTotal.cx = sizeTotal.cy = 200;
}
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CIPLabView printing
BOOL CIPLabView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CIPLabView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CIPLabView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CIPLabView diagnostics
#ifdef _DEBUG
void CIPLabView::AssertValid() const
{
CScrollView::AssertValid();
}
void CIPLabView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CIPLabDoc* CIPLabView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CIPLabDoc)));
return (CIPLabDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CIPLabView message handlers
void CIPLabView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
{
CMainFrame* pMainWnd=(CMainFrame*)AfxGetMainWnd();
CIPLabDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CString bitmapInfo;
if (pDoc->pPicture) {
bitmapInfo.Format("%d*%d",pDoc->pPicture->GetWidth(),pDoc->pPicture->GetHeight());
}
else
{
CSize size = this->GetTotalSize();
bitmapInfo.Format("%d*%d",size.cx,size.cy);
}
pMainWnd->SetBitmapInfo(bitmapInfo);
CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}
BOOL CIPLabView::OnEraseBkgnd(CDC* pDC)
{
CBrush br(RGB(128,128,128));
FillOutsideRect(pDC, &br);
return TRUE;
return CScrollView::OnEraseBkgnd(pDC);
}
void CIPLabView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
POSITION pos;
pos = m_listViewActionList.GetHeadPosition();
while (pos!=NULL) {
CViewActionBase* pAciton = m_listViewActionList.GetNext(pos);
if (pAciton->GetID()==m_ToolOption) {
pAciton->OnLButtonDown(nFlags, point);
break;
}
}
CScrollView::OnLButtonDown(nFlags, point);
}
void CIPLabView::OnLButtonUp(UINT nFlags, CPoint point)
{
POSITION pos;
pos = m_listViewActionList.GetHeadPosition();
while (pos!=NULL) {
CViewActionBase* pAciton = m_listViewActionList.GetNext(pos);
if (pAciton->GetID()==m_ToolOption) {
pAciton->OnLButtonUp(nFlags, point);
break;
}
}
CScrollView::OnLButtonUp(nFlags, point);
}
void CIPLabView::OnMouseMove(UINT nFlags, CPoint point)
{
POSITION pos;
pos = m_listViewActionList.GetHeadPosition();
while (pos!=NULL) {
CViewActionBase* pAciton = m_listViewActionList.GetNext(pos);
if (pAciton->GetID()==m_ToolOption) {
pAciton->OnMouseMove(nFlags, point);
break;
}
}
CScrollView::OnMouseMove(nFlags, point);
}
void CIPLabView::OnRButtonDown(UINT nFlags, CPoint point)
{
POSITION pos;
pos = m_listViewActionList.GetHeadPosition();
while (pos!=NULL) {
CViewActionBase* pAciton = m_listViewActionList.GetNext(pos);
if (pAciton->GetID()==m_ToolOption) {
pAciton->OnRButtonDown(nFlags, point);
break;
}
}
CScrollView::OnRButtonDown(nFlags, point);
}
BOOL CIPLabView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
POSITION pos;
pos = m_listViewActionList.GetHeadPosition();
while (pos!=NULL) {
CViewActionBase* pAciton = m_listViewActionList.GetNext(pos);
if (pAciton->GetID()==m_ToolOption) {
return pAciton->OnSetCursor( pWnd, nHitTest, message);
}
}
return CScrollView::OnSetCursor(pWnd, nHitTest, message);
}
//////////////////////////////////////////////////////////////////////////
void CIPLabView::ClientToDib(POINT& point)
{
point.x += GetScrollPos(SB_HORZ);
point.y += GetScrollPos(SB_VERT);
point.x=(long)(point.x/this->GetZoom());
point.y=(long)(point.y/this->GetZoom());
}
void CIPLabView::DibToClient(CRect& rect)
{
rect.left=(long)(rect.left*GetZoom());
rect.top=(long)(rect.top*GetZoom());
rect.right=(long)(rect.right*GetZoom());
rect.bottom=(long)(rect.bottom*GetZoom());
rect.left -= GetScrollPos(SB_HORZ);
rect.top -= GetScrollPos(SB_VERT);
rect.right -= GetScrollPos(SB_HORZ);
rect.bottom -= GetScrollPos(SB_VERT);
}
void CIPLabView::DibToClient(POINT& point)
{
point.x=(long)(point.x*GetZoom());
point.y=(long)(point.y*GetZoom());
point.x -= GetScrollPos(SB_HORZ);
point.y -= GetScrollPos(SB_VERT);
}
void CIPLabView::PointInDib(POINT& point)
{
point.x = max(point.x,0);
point.x = min(point.x,this->GetDocument()->GetWidth()-1);
point.y = max(point.y,0);
point.y = min(point.y,this->GetDocument()->GetHeight()-1);
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//edit tool
void CIPLabView::OnEdittoolSelectany()
{
m_ToolOption = (ToolOption)this->m_cViewActionEditTool.GetID();
this->m_cViewActionEditTool.SetEditToolOption(EDITTOOLSELECTANY);
this->m_bEditToolEnable = TRUE;
this->Invalidate();
}
void CIPLabView::OnEdittoolSelectrect()
{
m_ToolOption = (ToolOption)this->m_cViewActionEditTool.GetID();
this->m_cViewActionEditTool.SetEditToolOption(EDITTOOLSELECTRECT);
this->m_bEditToolEnable = TRUE;
this->Invalidate();
}
void CIPLabView::OnEdittoolCopy()
{
this->Invalidate();
}
void CIPLabView::OnUpdateEdittoolCopy(CCmdUI* pCmdUI)
{
pCmdUI->Enable(FALSE);
}
void CIPLabView::OnEdittoolCut()
{
this->Invalidate();
}
void CIPLabView::OnUpdateEdittoolCut(CCmdUI* pCmdUI)
{
pCmdUI->Enable(FALSE);
}
void CIPLabView::OnEdittoolPaste()
{
this->Invalidate();
}
void CIPLabView::OnUpdateEdittoolPaste(CCmdUI* pCmdUI)
{
pCmdUI->Enable(FALSE);
}
void CIPLabView::OnEdittoolEnable()
{
this->m_bEditToolEnable = !this->m_bEditToolEnable;
this->Invalidate();
// this->CancelToolOption();
}
void CIPLabView::OnUpdateEdittoolEnable(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bEditToolEnable) ;
}
//////////////////////////////////////////////////////////////////////////
void CIPLabView::OnIpInv()
{
this->GetDocument()->IPFuncInv();
}
void CIPLabView::OnIpInvInregion()
{
#ifdef _DEBUG
SYSTEMTIME startTime;
::GetSystemTime(&startTime);
#endif //_DEBUG
this->GetDocument()->IPFuncInvInRegion(this->m_cViewActionEditTool.GetWorkingRegion());
#ifdef _DEBUG
SYSTEMTIME endTime;
::GetSystemTime(&endTime);
int timeDiff = GetTimeDiff(startTime,endTime);
CString msgStr;
msgStr.Format("%d ms",timeDiff);
AfxMessageBox(msgStr);
#endif //_DEBUG
}
void CIPLabView::OnIpInvInregionF()
{
#ifdef _DEBUG
SYSTEMTIME startTime;
::GetSystemTime(&startTime);
#endif //_DEBUG
this->GetDocument()->IPFuncInvInRegion_F(this->m_cViewActionEditTool.GetWorkingRegion());
#ifdef _DEBUG
SYSTEMTIME endTime;
::GetSystemTime(&endTime);
int timeDiff = GetTimeDiff(startTime,endTime);
CString msgStr;
msgStr.Format("%d ms",timeDiff);
AfxMessageBox(msgStr);
#endif //_DEBUG
}
void CIPLabView::OnIpInvInregionO()
{
#ifdef _DEBUG
SYSTEMTIME startTime;
::GetSystemTime(&startTime);
#endif //_DEBUG
CRgn rgn;
POINT* pPoints;
int pointNum;
this->m_cViewActionEditTool.GetWorkingRegion(&pPoints,&pointNum);
BOOL bSuccess=rgn.CreatePolygonRgn(pPoints,pointNum,WINDING);
if(bSuccess)
{
this->GetDocument()->IPFuncInvInRegion_O(&rgn);
rgn.DeleteObject();
}
#ifdef _DEBUG
SYSTEMTIME endTime;
::GetSystemTime(&endTime);
int timeDiff = GetTimeDiff(startTime,endTime);
CString msgStr;
msgStr.Format("%d ms",timeDiff);
AfxMessageBox(msgStr);
#endif //_DEBUG
}
void CIPLabView::OnIpHittest()
{
GraphicsPath* pPath = this->m_cViewActionEditTool.GetWorkingRegion();
if (pPath) {
Matrix mat;
GraphicsPath* pTmpPath = pPath->Clone();
mat.Translate(0,0);
mat.Scale(this->GetZoom(), this->GetZoom());
pTmpPath->Transform(&mat);
Region region(pTmpPath);
Graphics g(GetDC()->GetSafeHdc());
Pen outlinePen(Color(150, 0,0,255 ),1.0f);
SolidBrush pathBrush(Color(150,255,0,0));
SolidBrush regionBrush(Color(50,0,255,0));
SolidBrush bgBrush(Color(255,222,222,222));
g.FillRectangle(&bgBrush,0,0,GetDocument()->GetWidth(),GetDocument()->GetHeight());
g.DrawPath(&outlinePen,pTmpPath);
g.FillPath(&pathBrush,pTmpPath);
g.FillRegion(®ionBrush,®ion);
delete pTmpPath;
}
}
void CIPLabView::OnLeastsquaresfitting()
{
m_ToolOption = (ToolOption)this->m_cViewActionImageTool.GetID();
this->m_cViewActionImageTool.SetEditToolOption(TOOLEASTSQUARESFITTING);
// this->m_bEditToolEnable = TRUE;
this->Invalidate();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -