📄 magicscissorsview.cpp
字号:
// MagicScissorsView.cpp : implementation of the CMagicScissorsView class
//
#include "stdafx.h"
#include "MagicScissors.h"
#include "MagicScissorsDoc.h"
#include "MagicScissorsView.h"
#include "Image/Image.h"
#include "Image/Video.h"
#include "EdgeList.h"
#include "EdgePoint.h"
#include "ObjRgn.h"
#include "PolygonList.h"
#include "Polygon.h"
#include "FrameInfo.h"
#include "FrameFindDlg.h"
#include "MainFrm.h"
#include "DelRegionsDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMagicScissorsView
#define IDT_VIDEO_PLAY 1010
#define IDM_CHANGE_SLIDER WM_USER+3
IMPLEMENT_DYNCREATE(CMagicScissorsView, CScrollView)
BEGIN_MESSAGE_MAP(CMagicScissorsView, CScrollView)
//{{AFX_MSG_MAP(CMagicScissorsView)
ON_COMMAND(IDM_PLAY, OnPlay)
ON_COMMAND(IDM_STOP, OnStop)
ON_COMMAND(IDM_PAUSE, OnPause)
ON_COMMAND(IDM_NEXT, OnNext)
ON_COMMAND(IDM_PREV, OnPrev)
ON_WM_TIMER()
ON_COMMAND(IDM_SEGMENT, OnSegment)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_COMMAND(IDM_MODIFY, OnModify)
ON_WM_LBUTTONUP()
ON_UPDATE_COMMAND_UI(IDM_SEGMENT, OnUpdateSegment)
ON_UPDATE_COMMAND_UI(IDM_MODIFY, OnUpdateModify)
ON_WM_RBUTTONDOWN()
ON_COMMAND(IDM_DELETE_REGION, OnDeleteRegion)
ON_COMMAND(IDM_SMOOTH, OnSmooth)
ON_WM_KEYDOWN()
ON_WM_CONTEXTMENU()
ON_COMMAND(IDM_COPYPREV, OnCopyprev)
ON_COMMAND(IDM_FINDEDGE, OnFindedge)
ON_COMMAND(IDM_PLAY_ONLY, OnPlayOnly)
ON_UPDATE_COMMAND_UI(IDM_PLAY_ONLY, OnUpdatePlayOnly)
ON_COMMAND(IDM_PLAY_TRACE, OnPlayTrace)
ON_UPDATE_COMMAND_UI(IDM_PLAY_TRACE, OnUpdatePlayTrace)
ON_COMMAND(IDC_ZOOM_IN, OnZoomIn)
ON_UPDATE_COMMAND_UI(IDC_ZOOM_IN, OnUpdateZoomIn)
ON_COMMAND(IDC_ZOOM_OUT, OnZoomOut)
ON_UPDATE_COMMAND_UI(IDC_ZOOM_OUT, OnUpdateZoomOut)
ON_COMMAND(IDM_FIND_FRAME, OnFindFrame)
ON_COMMAND(IDM_TO_FIRST, OnToFirst)
ON_COMMAND(IDM_TO_LAST, OnToLast)
ON_UPDATE_COMMAND_UI(IDM_PLAY, OnUpdatePlay)
ON_UPDATE_COMMAND_UI(IDM_PREV, OnUpdatePrev)
ON_UPDATE_COMMAND_UI(IDM_NEXT, OnUpdateNext)
ON_WM_KEYUP()
ON_COMMAND(IDM_DELETE_REGIONS, OnDeleteRegions)
ON_COMMAND(ID_MAKE_ACTIVECONTOUR, OnMakeActivecontour)
ON_COMMAND(IDM_ACTIVECONTOUR_EVOLVE, OnActivecontourEvolve)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMagicScissorsView construction/destruction
CMagicScissorsView::CMagicScissorsView()
{
// TODO: add construction code here
m_bPlay = FALSE;
m_ptStart = m_ptEnd = CPoint(0,0);
m_nMode = 0;
m_bEdgeStart = FALSE;
m_nPlayMode = 0;
m_nZoom = 1;
m_bScroll = FALSE;
}
CMagicScissorsView::~CMagicScissorsView()
{
}
BOOL CMagicScissorsView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMagicScissorsView drawing
void CMagicScissorsView::OnDraw(CDC* pDC)
{
CMagicScissorsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
CDC MemDC;
CBitmap bitmap;
MemDC.CreateCompatibleDC(pDC);
bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
CBitmap* pBitmap = MemDC.SelectObject(&bitmap);
CPen pen;
pen.CreatePen(PS_DOT,1,RGB(0,0,200));
CPen* pPen;
if( pDoc->m_pImage )
pDoc->m_pImage->Paint( &MemDC );
if( pDoc->m_pCurFrame )
pDoc->m_pCurFrame->Paint( &MemDC, RGB(0,255,0) );
if( pDoc->m_pPolygonList )
pDoc->m_pPolygonList->Paint( &MemDC );
if( m_nMode == 1 )
{
if( pDoc->m_pEdgeList )
pDoc->m_pEdgeList->Paint(&MemDC, RGB(0,255,0));
pPen = MemDC.SelectObject(&pen);
MemDC.MoveTo(pDoc->m_ptConner[0]);
MemDC.LineTo(pDoc->m_ptConner[1]);
MemDC.LineTo(pDoc->m_ptConner[3]);
MemDC.LineTo(pDoc->m_ptConner[2]);
MemDC.LineTo(pDoc->m_ptConner[0]);
MemDC.SelectObject(pPen);
if( pDoc->m_pTempList )
pDoc->m_pTempList->Paint(&MemDC, RGB(255,0,0));
}
if( m_nMode == 2 )
{
if( pDoc->m_pModifyList )
pDoc->m_pModifyList->Paint(&MemDC, RGB(255,0,0));
}
if( m_nMode == 4 )
{
if( pDoc->m_pEdgeList )
pDoc->m_pEdgeList->Paint(&MemDC, RGB(0,255,0));
if( !pDoc->m_pEdgeList )
{
CBrush* pBrush = (CBrush*)MemDC.SelectStockObject(NULL_BRUSH);
CPen pen2;
pen2.CreatePen(PS_SOLID, 1, RGB(0,200,0));
pPen = MemDC.SelectObject(&pen2);
MemDC.Rectangle(m_ptStart.x,m_ptStart.y,m_ptEnd.x,m_ptEnd.y);
MemDC.SelectObject(pBrush);
MemDC.SelectObject(pPen);
}
}
if( m_nZoom == 1 )
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &MemDC, 0, 0, SRCCOPY );
else
pDC->StretchBlt( 0, 0, rect.Width()*m_nZoom, rect.Height()*m_nZoom, &MemDC, 0, 0, rect.Width(), rect.Height(), SRCCOPY );
MemDC.SelectObject(pBitmap);
}
void CMagicScissorsView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CMagicScissorsDoc* pDoc = GetDocument();
CSize sizeTotal;
// TODO: calculate the total size of this view
if( pDoc->m_pImage )
sizeTotal = pDoc->m_ImgSize;
else
sizeTotal.cx = sizeTotal.cy = 100;
//sizeTotal = pDoc->m_ImgSize;
SetScrollSizes(MM_TEXT, sizeTotal);
ResizeParentToFit(FALSE);
ResizeParentToFit(TRUE);
pDoc->m_pMain = this;
if( pDoc->m_pImage )
((CMainFrame*)AfxGetMainWnd())->m_wndVideoControlBar.Init(0,pDoc->m_nTotalFrame-1, this);
}
/////////////////////////////////////////////////////////////////////////////
// CMagicScissorsView diagnostics
#ifdef _DEBUG
void CMagicScissorsView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMagicScissorsView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMagicScissorsDoc* CMagicScissorsView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMagicScissorsDoc)));
return (CMagicScissorsDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMagicScissorsView message handlers
void CMagicScissorsView::OnPlay()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
if( !m_bPlay )
{
int fps;
if( pDoc->m_pVideo )
fps = 1000.f/pDoc->m_pVideo->GetFrameRate();
else
fps = 100;
SetTimer( IDT_VIDEO_PLAY, fps, NULL );
m_bPlay = TRUE;
}
}
void CMagicScissorsView::OnStop()
{
// TODO: Add your command handler code here
if( m_bPlay )
{
KillTimer( IDT_VIDEO_PLAY );
m_bPlay = FALSE;
}
CMagicScissorsDoc* pDoc = GetDocument();
pDoc->m_nCurFrame = 0;
pDoc->GetNextFrame(FALSE);
Invalidate(FALSE);
((CMainFrame*)AfxGetMainWnd())->m_wndVideoControlBar.SetPos(0);
}
void CMagicScissorsView::OnPause()
{
// TODO: Add your command handler code here
if( m_bPlay )
{
KillTimer(IDT_VIDEO_PLAY);
m_bPlay = FALSE;
}
}
void CMagicScissorsView::OnNext()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
//if( pDoc->m_pVideo )
{
if( pDoc->m_nCurFrame > 0 )
{
BeginWaitCursor();
pDoc->GetNextFrame(FALSE);
Invalidate(FALSE);
if( pDoc->m_pSecond )
pDoc->m_pSecond->Invalidate(FALSE);
((CMainFrame*)AfxGetMainWnd())->m_wndVideoControlBar.SetPos(pDoc->m_nCurFrame-1);
EndWaitCursor();
}
}
m_nMode = 0;
}
void CMagicScissorsView::OnPrev()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
//if( pDoc->m_pVideo )
{
if( pDoc->m_nCurFrame > 0 )
{
BeginWaitCursor();
pDoc->GetPrevFrame();
Invalidate(FALSE);
if( pDoc->m_pSecond )
pDoc->m_pSecond->Invalidate(FALSE);
((CMainFrame*)AfxGetMainWnd())->m_wndVideoControlBar.SetPos(pDoc->m_nCurFrame-1);
EndWaitCursor();
}
}
m_nMode = 0;
}
void CMagicScissorsView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CMagicScissorsDoc* pDoc = GetDocument();
if( nIDEvent = IDT_VIDEO_PLAY )
{
if( m_bPlay )
{
KillTimer(IDT_VIDEO_PLAY);
if( m_nPlayMode == 0 )
{
int fps;
if( pDoc->m_pVideo )
fps = 1000.f/pDoc->m_pVideo->GetFrameRate();
else
fps = 100;
SetTimer( IDT_VIDEO_PLAY, fps, NULL );
pDoc->GetNextFrame(FALSE);
//pDoc->GetNextFrame();
}
else
{
pDoc->GetNextFrame();
pDoc->TraceEdge();
SetTimer( IDT_VIDEO_PLAY, 10, NULL );
}
Invalidate(FALSE);
((CMainFrame*)AfxGetMainWnd())->m_wndVideoControlBar.SetPos(pDoc->m_nCurFrame-1);
if( pDoc->m_pSecond )
pDoc->m_pSecond->Invalidate(FALSE);
if( pDoc->m_nCurFrame == pDoc->m_nTotalFrame )
{
KillTimer(IDT_VIDEO_PLAY );
m_bPlay = FALSE;
}
}
}
CScrollView::OnTimer(nIDEvent);
}
void CMagicScissorsView::OnSegment()
{
// TODO: Add your command handler code here
CMagicScissorsDoc* pDoc = GetDocument();
if( !pDoc->m_bEdge )
pDoc->DetectEdge();
m_nMode = 1;
}
void CMagicScissorsView::OnModify()
{
// TODO: Add your command handler code here
if( m_nMode == 2 )
m_nMode = 0;
else
m_nMode = 2;
}
void CMagicScissorsView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMagicScissorsDoc* pDoc = GetDocument();
CPoint pt = point;
CPoint sc = GetScrollPosition();
point += sc;
point.x /= m_nZoom;
point.y /= m_nZoom;
CSize size = pDoc->m_ImgSize;
if( point.x >= 0 && point.x < size.cx && point.y >= 0 && point.y < size.cy )
{
if( m_bScroll )
{
m_ptStart = point;
}
else if( m_nMode == 0 )
{
m_ptStart = point;
if( pDoc->SelectObject(point) )
m_nMode = 3;
Invalidate(FALSE);
}
else if( m_nMode == 1 )
{
m_ptStart = point;
pDoc->AddEdge();
if( !m_bEdgeStart ) m_bEdgeStart = TRUE;
}
else if( m_nMode == 2 )
{
m_ptStart = point;
pDoc->AddModify( point );
if( !m_bEdgeStart ) m_bEdgeStart = TRUE;
}
else if( m_nMode == 4 )
{
m_ptStart = point;
m_bEdgeStart = TRUE;
}
}
point = pt;
CScrollView::OnLButtonDown(nFlags, point);
}
void CMagicScissorsView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMagicScissorsDoc* pDoc = GetDocument();
CPoint pt = point;
CPoint sc = GetScrollPosition();
point += sc;
point.x /= m_nZoom;
point.y /= m_nZoom;
CSize size = pDoc->m_ImgSize;
if( point.x >= 0 && point.x < size.cx && point.y >= 0 && point.y < size.cy )
{
if( m_bScroll && nFlags&MK_LBUTTON )
{
int dx = -m_ptStart.x + point.x;
int dy = -m_ptStart.y + point.y;
dx *= m_nZoom;
dy *= m_nZoom;
CPoint sc = GetScrollPosition();
sc.x += dx;
sc.y += dy;
ScrollToPosition(sc);
m_ptStart = point;
}
else if( m_nMode == 1 && m_bEdgeStart )
{
m_ptEnd = point;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -