📄 viewactionedittool.cpp
字号:
// ViewActionEditTool.cpp: implementation of the CViewActionEditTool class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ViewActionEditTool.h"
#include "FastHitTest.h"
#include "IPLabView.h"
typedef CIPLabView* CLASSVIEWHANDLE;
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CViewActionEditTool::CViewActionEditTool()
{
m_nEditToolOption = EDITTOOLNOTHING;
m_bDraging = FALSE;
m_WorkingRegion = NULL;
}
CViewActionEditTool::~CViewActionEditTool()
{
if (m_WorkingRegion != NULL) {
delete m_WorkingRegion;
}
}
void CViewActionEditTool::OnLButtonDown(UINT nFlags, CPoint point)
{
CancelSelection();
switch(m_nEditToolOption) {
case EDITTOOLSELECTRECT:
SelectRectLBDown(nFlags,point);
break;
case EDITTOOLSELECTANY:
AnySelectLBDown(nFlags,point);
break;
default:
;
}
return;
}
void CViewActionEditTool::OnLButtonUp(UINT nFlags, CPoint point)
{
switch(m_nEditToolOption) {
case EDITTOOLSELECTRECT:
SelectRectLBUp(nFlags,point);
break;
case EDITTOOLSELECTANY:
AnySelectLBUp(nFlags,point);
break;
default:
;
}
return;
}
void CViewActionEditTool::OnLButtonDblClk(UINT nFlags, CPoint point)
{
}
void CViewActionEditTool::OnRButtonUp(UINT nFlags, CPoint point)
{
}
void CViewActionEditTool::OnRButtonDown(UINT nFlags, CPoint point)
{
}
void CViewActionEditTool::OnRButtonDblClk(UINT nFlags, CPoint point)
{
}
void CViewActionEditTool::OnMouseMove(UINT nFlags, CPoint point)
{
switch(m_nEditToolOption) {
case EDITTOOLSELECTRECT:
SelectRectMouseMove(nFlags,point);
break;
case EDITTOOLSELECTANY:
AnySelectMouseMove(nFlags,point);
break;
default:
;
}
return;
}
BOOL CViewActionEditTool::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
return TRUE;
}
void CViewActionEditTool::SetEditToolOption(EditToolOption option)
{
m_nEditToolOption = option;
}
EditToolOption CViewActionEditTool::GetEditToolOption()
{
return m_nEditToolOption;
}
BOOL CViewActionEditTool::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
switch(m_nEditToolOption) {
case EDITTOOLSELECTRECT:
case EDITTOOLSELECTANY:
::SetCursor ( ::LoadCursor (NULL, IDC_CROSS) ) ;
break;
default:
::SetCursor (::LoadCursor (NULL, IDC_ARROW)) ;
}
return TRUE;
}
//////////////////////////////////////////////////////////////////////////
void CViewActionEditTool::CancelSelection()
{
if (m_WorkingRegion != NULL) {
delete m_WorkingRegion;
m_WorkingRegion = NULL;
}
m_SelNum = 0;
this->GetParent()->Invalidate();
}
GraphicsPath* CViewActionEditTool::GetWorkingRegion()
{
return m_WorkingRegion;
}
void CViewActionEditTool::GetWorkingRegion(POINT** ppPoints,int* pPpointNum)
{
*ppPoints = this->m_Sel;
*pPpointNum = this->m_SelNum;
}
void CViewActionEditTool::SetWorkingRegion(GraphicsPath* pWorkingPath)
{
if (m_WorkingRegion!=NULL) {
delete m_WorkingRegion;
m_WorkingRegion=NULL;
}
if (pWorkingPath == NULL) {
return;
}
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
Graphics g(pView->GetDocument()->GetBitmap());
Region region(pWorkingPath);
Region imageRegion(Rect(0,0,pView->GetDocument()->GetWidth(),pView->GetDocument()->GetHeight()));
region.Intersect(&imageRegion);
if (region.IsEmpty(&g)) {
return;
}
m_WorkingRegion = pWorkingPath->Clone();
}
BOOL CViewActionEditTool::IsWorkingRegionEmpty()
{
return m_WorkingRegion==NULL;
}
//////////////////////////////////////////////////////////////////////////
void CViewActionEditTool::SelectRectLBDown(UINT nFlags, CPoint point)
{
m_bDraging=TRUE;
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
pView->SetCapture();
if (this->m_WorkingRegion!=NULL) {
delete m_WorkingRegion;
m_WorkingRegion=NULL;
pView->Invalidate();
}
pView->ClientToDib(point);
pView->PointInDib(point);
m_Sel[0] = point;
m_Sel[1] = point;
return;
}
void CViewActionEditTool::SelectRectMouseMove(UINT nFlags, CPoint point)
{
if(m_bDraging)
{
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
pView->ClientToDib(point);
pView->PointInDib(point);
m_Sel[2] = m_Sel[1];
m_Sel[1] = point;
DrawTmpRect();
}
}
void CViewActionEditTool::SelectRectLBUp(UINT nFlags, CPoint point)
{
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
if (m_bDraging)
{
Rect tRect;
GraphicsPath tWorkingPath(FillModeWinding);
tRect.X = min(m_Sel[0].x,m_Sel[1].x);
tRect.Width = abs( m_Sel[0].x - m_Sel[1].x );
tRect.Y = min(m_Sel[0].y,m_Sel[1].y);
tRect.Height = abs( m_Sel[0].y - m_Sel[1].y );
tWorkingPath.AddRectangle(tRect);
SetWorkingRegion(&tWorkingPath);
m_bDraging=FALSE;
::ReleaseCapture();
pView->Invalidate();
}
}
void CViewActionEditTool::Draw(CDC* pDC,float scaleX,float scaleY, int offX,int offY)
{
//draw region
int lineWidth = 1;
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
if (m_WorkingRegion) {
Graphics g(pDC->GetSafeHdc());
Color outlilneColor(220, 10, 10, 60);
Matrix matrix;
matrix.Scale(scaleX, scaleY);
matrix.Translate(offX,offY,MatrixOrderAppend);
GraphicsPath tRegion(FillModeWinding);
tRegion.AddPath(m_WorkingRegion,true);
tRegion.Transform(&matrix);
Pen outlinePen1(outlilneColor,lineWidth);
outlinePen1.SetDashStyle(DashStyleDash);
outlinePen1.SetEndCap(LineCapFlat);
outlinePen1.SetLineJoin(LineJoinRound );
g.DrawPath(&outlinePen1,&tRegion);
}
}
/*
void CViewActionEditTool::OnEditToolCopy()
{
if (m_WorkingRegion==NULL) {
return;
}
Rect rect;
m_WorkingRegion->GetBounds(&rect);
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
Bitmap* pPicture = pView->GetDocument()->GetBitmap();
Graphics tThis(pPicture);
rect.Intersect(Rect(0,0,pPicture->GetWidth(),pPicture->GetHeight()));
Rect rectCut(0,0,rect.Width,rect.Height);
Bitmap* pCut = new Bitmap(rect.Width,rect.Height,PixelFormat32bppARGB );
FastHitTest tHitTestBuffer(m_WorkingRegion);
//
BitmapData bmData_Origin;
BitmapData bmData_Cut;
pPicture->LockBits(&rect,
ImageLockModeRead ,
PixelFormat32bppARGB,
&bmData_Origin);
pCut->LockBits(&rectCut,
ImageLockModeWrite,
PixelFormat32bppARGB,
&bmData_Cut);
//
UINT* pixels0 = (UINT*)bmData_Origin.Scan0;
UINT* pixelsCut = (UINT*)bmData_Cut.Scan0;
int off0 = bmData_Origin.Stride - rect.Width*4;
int offCut = bmData_Cut.Stride - rect.Width*4;
int i,j;
for(j=0;j<rect.Height;j++)
{
for(i=0;i<rect.Width;i++)
{
if (tHitTestBuffer.IsVisible(i+rect.X,j+rect.Y)) {
*pixelsCut = *pixels0;
//*pixels0 = 0xffffffff;
// pixels0[j * bmData_Origin.Stride / 4 + i] = 0xffffffff;
}
else
{
*pixelsCut = 0xffff00ff;
}
pixels0++;
pixelsCut++;
}
pixels0 = (UINT* )((int)pixels0+off0);
pixelsCut = (UINT* )((int)pixelsCut+offCut);
}
//
pPicture->UnlockBits(&bmData_Origin);
pCut->UnlockBits(&bmData_Cut);
this->m_Clipboard.Attach(pCut,rect.X,rect.Y);
SetWorkingRegion(NULL);
return;
}
*/
void CViewActionEditTool::AnySelectLBDown(UINT nFlags, CPoint point)
{
GetParent()->SetCapture();
m_bDraging=TRUE;
m_Sel[0] = point;
m_SelNum = 1;
}
void CViewActionEditTool::AnySelectMouseMove(UINT nFlags, CPoint point)
{
if(m_bDraging)
{
if (point==m_Sel[m_SelNum-1]) {
return;
}
if (m_SelNum<MAX_SEL_POINTS) {
m_Sel[m_SelNum] = point;
m_SelNum++;
DrawSelectAnyRegion();
}
}
}
void CViewActionEditTool::AnySelectLBUp(UINT nFlags, CPoint point)
{
if (m_bDraging)
{
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
GraphicsPath tWorkingPath(FillModeWinding);
int i;
Point* pPoints = new Point[m_SelNum];
for(i=0;i<m_SelNum;i++)
{
pView->ClientToDib(m_Sel[i]);
pPoints[i].X = m_Sel[i].x;
pPoints[i].Y = m_Sel[i].y;
}
tWorkingPath.AddPolygon(pPoints,m_SelNum);
delete pPoints;
SetWorkingRegion(&tWorkingPath);
//m_SelNum = 0;
m_bDraging=FALSE;
::ReleaseCapture();
this->GetParent()->Invalidate();
}
}
void CViewActionEditTool::DrawTmpRect()
{
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
CPen penLine(PS_SOLID,2,RGB(255,0,0));
CClientDC dc(pView);
CPen* pOldPen=dc.SelectObject(&penLine);
int nOldRop=dc.SetROP2(R2_NOTXORPEN);
CRect tStarRect;
CRect tEndRect;
SIZE tBorder;
// Graphics g(pView->GetDC()->GetSafeHdc());
// Pen outlinePen1(Color(200, 0, 0, 0),1.0f);
// outlinePen1.SetDashStyle(DashStyleDash);
tBorder.cx = 1;
tBorder.cy = 1;
tStarRect.left = min(m_Sel[0].x,m_Sel[1].x);
tStarRect.right = max(m_Sel[0].x,m_Sel[1].x);
tStarRect.top = min(m_Sel[0].y,m_Sel[1].y);
tStarRect.bottom = max(m_Sel[0].y,m_Sel[1].y);
tEndRect.left = min(m_Sel[0].x,m_Sel[2].x);
tEndRect.right = max(m_Sel[0].x,m_Sel[2].x);
tEndRect.top = min(m_Sel[0].y,m_Sel[2].y);
tEndRect.bottom = max(m_Sel[0].y,m_Sel[2].y);
pView->DibToClient(tStarRect);
pView->DibToClient(tEndRect);
dc.DrawDragRect(tStarRect,tBorder,tEndRect,tBorder);
//g.DrawRectangle(&outlinePen1,tStarRect.left,tStarRect.top,tStarRect.Width(),tStarRect.Height());
//g.DrawRectangle(&outlinePen1,tEndRect.left,tEndRect.top,tEndRect.Width(),tEndRect.Height());
dc.SelectObject(pOldPen);
dc.SetROP2(nOldRop);
}
void CViewActionEditTool::DrawSelectAnyRegion()
{
CLASSVIEWHANDLE pView = (CLASSVIEWHANDLE)this->GetParent();
Graphics g(pView->GetDC()->GetSafeHdc());
Pen outlinePen1(Color(200, 0, 0, 0),1.0f);
outlinePen1.SetDashStyle(DashStyleDash);
g.DrawLine(&outlinePen1,Point(m_Sel[m_SelNum-2].x,m_Sel[m_SelNum-2].y),Point(m_Sel[m_SelNum-1].x,m_Sel[m_SelNum-1].y));;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -