⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagecamview.cpp

📁 《数字图象工程案例》原码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
	// TODO: Add your command handler code here
	
}

void CimageCAMView::OnUpdateRotateother(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}
BOOL CimageCAMView::MergeText()
{
	if (! ::IsWindow(m_EditText.m_hWnd))
		return FALSE;
	
	CString s;
	m_EditText.GetWindowText(s);
	
	CRect rc;
	m_EditText.GetWindowRect(&rc);
	ScreenToClient(&rc);
	CRect rcClear = rc;
	rcClear.InflateRect(3,3);
	m_EditText.DestroyWindow();
	InvalidateRect(&rcClear);
	
	// Merge Text into DIB
	if (! s.IsEmpty())
	{
		CClientDC dc(this);
		
		CDC * pDibDC = m_pDib->BeginPaint(&dc);
		int nOldBkMode = pDibDC->SetBkMode(TRANSPARENT);
		COLORREF crOldTextColor = pDibDC->SetTextColor(m_crPenColor);
		CFont *pOldFont = pDibDC->SelectObject(m_pFont);
		
		// needed rectangle
		ClientToDib(rc);
		if (rc.bottom > m_pDib->GetHeight())
			rc.bottom = m_pDib->GetHeight();
		if (rc.right > m_pDib->GetWidth())
			rc.right = m_pDib->GetWidth();
		
		pDibDC->DrawText(s, &rc, m_nTextAlign);
		pDibDC->SetBkMode(nOldBkMode);
		pDibDC->SetTextColor(crOldTextColor);
		pDibDC->SelectObject(pOldFont);
		m_pDib->EndPaint();
	}
	
	return TRUE;
}


void CimageCAMView::MergeFloatDib()
{
	if (m_pFloatWnd)
	{
		CRect rc;
		m_pFloatWnd->GetWindowRect(&rc);
		CPoint point = rc.TopLeft();
		ScreenToClient(&point);
		ClientToDib(point);
		m_pDib->MergeDib(m_pFloatWnd->m_hDibFloat, point);
		
		// document changed
		CDocument* pDoc = GetDocument();
		pDoc->SetModifiedFlag(TRUE);
	}
	DeleteFloatWnd();
}

void CimageCAMView::DeleteFloatDib()
{
	// if selected rect is exist, cut it
	CutSelectedRect();
	// if float DIB window exist, delete it
	DeleteFloatWnd();
}

void CimageCAMView::CutFloatDib()
{
	// copy to clipboard first
	CopyToClipboard();
	// then delete
	DeleteFloatDib();
}

void CimageCAMView::CopyToClipboard()
{
	// Clean clipboard of contents, and copy the DIB/DDB/PAL.
	// Actual copy will occured in WM_RENDERALLFORMATS/WM_RENDERFORMAT
	if (OpenClipboard())
	{
		EmptyClipboard();
		
		HDIB hDib;
		HBITMAP hBitmap;
		HPALETTE hPalette;
		// if there is float dib, copy it
		if (m_pFloatWnd)
		{
			hDib = CopyHandle(m_pFloatWnd->m_hDibFloat);
			hBitmap = DIBToBitmap(m_pFloatWnd->m_hDibFloat, (HPALETTE)m_pDib->m_pPalette->GetSafeHandle());
			hPalette = CopyPalette((HPALETTE)m_pDib->m_pPalette->GetSafeHandle());
		}
		else	// otherwise, copy the entire DIB
		{
			hDib = CopyHandle(m_pDib->m_hDib);
			hBitmap = DIBToBitmap(m_pDib->m_hDib, (HPALETTE)m_pDib->m_pPalette->GetSafeHandle ());
			hPalette = CopyPalette((HPALETTE)m_pDib->m_pPalette->GetSafeHandle());
		}
		
		// set them to clipboard
		SetClipboardData(CF_DIB, hDib);
		SetClipboardData(CF_BITMAP, hBitmap);
		SetClipboardData(CF_PALETTE, hPalette);
		
		CloseClipboard();
	}
}
void CimageCAMView::ClientToDib(CPoint& point)
{
	point.x += GetScrollPos(SB_HORZ);
	point.y += GetScrollPos(SB_VERT);
}

void CimageCAMView::ClientToDib(CRect& rect)
{
	rect.left += GetScrollPos(SB_HORZ);
	rect.top += GetScrollPos(SB_VERT);
	rect.right += GetScrollPos(SB_HORZ);
	rect.bottom += GetScrollPos(SB_VERT);
}

void CimageCAMView::CutSelectedRect()
{
	if (! m_rcClip.IsRectEmpty())
	{
		// adjust position with scroll position
		CRect rcInDib(m_rcClip);
		ClientToDib(rcInDib);
		
		// cut select rectangle in m_pDib
		m_pDib->CutRect(rcInDib);
		
		// empty current rectangle
		m_rcClip.SetRectEmpty();
		
		// document changed
		CDocument* pDoc = GetDocument();
		pDoc->SetModifiedFlag(TRUE);
	}
}
void CimageCAMView::DibToClient(CPoint& point)
{
	point.x -= GetScrollPos(SB_HORZ);
	point.y -= GetScrollPos(SB_VERT);
}

void CimageCAMView::DibToClient(CRect& rect)
{
	rect.left -= GetScrollPos(SB_HORZ);
	rect.top -= GetScrollPos(SB_VERT);
	rect.right -= GetScrollPos(SB_HORZ);
	rect.bottom -= GetScrollPos(SB_VERT);
}
void CimageCAMView::DeleteFloatWnd()
{
	if (m_pFloatWnd)
	{
		delete m_pFloatWnd;
		m_pFloatWnd = NULL;
	}
}

void CimageCAMView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CimageCAMDoc* pDoc = GetDocument();
	m_pDib=pDoc->m_pDib;
	if (PointInDib(point,m_pDib->ratio))
	{
		// Merge and delete old float DIB (if exist)
		MergeFloatDib();
		
		// if ther is text, just merge it into DIB
		if (! MergeText())	// else, do paint
		{
			
			if (m_nDrawType == DT_FREELINE)
			{
				m_bDrawFreeline = TRUE;
				m_ptFreelineStart = point;
				SetCapture();
				
				// set a pixel anyway
				CClientDC dc(this);
				ClientToDib(point);
				CDC * pDibDC = m_pDib->BeginPaint(&dc);
				pDibDC->SetPixel(point, m_crPenColor);
				m_pDib->EndPaint();
				Invalidate(FALSE);
			}
			else if (m_nDrawType == DT_ERASER)
			{
				m_bErasing = TRUE;
				SetCapture();
				
				// set a pixel anyway
				CClientDC dc(this);
				ClientToDib(point);
				CRect rc(point.x-4, point.y-4, point.x+4, point.y+4);
				CDC * pDibDC = m_pDib->BeginPaint(&dc);
				CBrush brush(m_crFillColor);
				CBrush *pOldBrush = pDibDC->SelectObject(&brush);
				CPen Pen(PS_SOLID, 1, m_crFillColor);
				CPen *pOldPen = pDibDC->SelectObject(&Pen);
				pDibDC->Ellipse(&rc);
				pDibDC->SelectObject(pOldPen);
				pDibDC->SelectObject(pOldBrush);
				m_pDib->EndPaint();
				Invalidate(FALSE);
			}
			else if (m_nDrawType == DT_PICKER)
			{
				CClientDC dc(this);
				COLORREF crColor = dc.GetPixel(point);
				CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
				ASSERT_KINDOF(CMainFrame, pAppFrame);
				if (pAppFrame->m_wndPaintParamBar.m_nSelectColorMode == PP_FILL_COLOR)
				{
					m_crFillColor = crColor;
					ShowFillColor();
				}
				else if (pAppFrame->m_wndPaintParamBar.m_nSelectColorMode == PP_PEN_COLOR)
				{
					m_crPenColor = crColor;
					ShowPenColor();
				}
			}
			else if(m_nDrawType==DT_ZOOMMODE)
			{
				switch(m_zoommode)
				{
				case 1:      break;
				case 2:zoom(point,2);
					break;
				case 3:zoom(point,0.5);
					break;
				default:	;	
				}		   
			}
			else if (m_nDrawType == DT_FILL)
			{
				CBrush brush(m_crFillColor);
				
				CClientDC dc(this);
				COLORREF crColor = dc.GetPixel(point);
				
				CBrush* pOldBrush = dc.SelectObject(&brush);
				dc.ExtFloodFill(point.x,			// x-coordinate where filling begins
					point.y,			// y-coordinate where filling begins
					crColor,			// fill color  
					FLOODFILLSURFACE);	// fill type
				dc.SelectObject(pOldBrush);
				
				ClientToDib(point);
				CDC * pDibDC = m_pDib->BeginPaint(&dc);
				pOldBrush = pDibDC->SelectObject(&brush);
				pDibDC->ExtFloodFill(point.x,				// x-coordinate where filling begins
					point.y,				// y-coordinate where filling begins
					crColor,				// fill color  
					FLOODFILLSURFACE);		// fill type
				pDibDC->SelectObject(pOldBrush);
				m_pDib->EndPaint();
			}
			else if (m_nDrawType == DT_CURVE)
			{
				if (! m_bDrawCurve)
				{
					m_bDrawCurve = TRUE;
					m_nDrawCurveStep = 1;
					ClientToDib(point);
					m_ptCurve[0] = point;
					m_ptCurve[1] = point;
					m_ptCurve[2] = point;
					m_ptCurve[3] = point;
					// capture mouse
					::SetCursor(m_hCursorCurve);
					SetCapture();
				}
				else
				{
					CRect rc;
					GetClientRect(&rc);
					if (! rc.PtInRect(point))
					{
						DrawTmpCurve();
						m_bDrawCurve = FALSE;
						ReleaseCapture();
						::SetCursor(m_hCursorGeneralDraw);
					}
					else
					{
						ClientToDib(point);
						DrawTmpCurve();
						if (m_nDrawCurveStep == 2)
							m_ptCurve[2] = point;
						else if (m_nDrawCurveStep == 3)
							m_ptCurve[1] = point;
						DrawTmpCurve();
					}
				}
			}
			else
			{
				if (m_nDrawType == DT_LINE)
				{
					// set a pixel anyway
					CClientDC dc(this);	
					dc.SetPixel(point, m_crPenColor);
					//ClientToDib(point);
					
					CDC * pDibDC = m_pDib->BeginPaint(&dc);
					pDibDC->SetPixel(point, m_crPenColor);
					m_pDib->EndPaint();
				}
				// start draw rectangle
				StartDrawRubber(point);
				m_bDrawingRubber = TRUE;
			}
		}
	}
	CScrollView::OnLButtonDown(nFlags, point);
}

void CimageCAMView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if (m_bDrawFreeline)
	{
		m_bDrawFreeline = FALSE;
		ReleaseCapture();
	}
	else if (m_bErasing)
	{
		m_bErasing = FALSE;
		ReleaseCapture();
	}
	else if (m_bDrawCurve)
	{
		m_nDrawCurveStep++;
		// finish draw curve
		if (m_nDrawCurveStep > 3)
		{
			ReleaseCapture();
			::SetCursor(m_hCursorGeneralDraw);
			m_bDrawCurve = FALSE;
			DrawCurve();
		}
	}
	else if (m_bDrawingRubber)
	{
		StopDrawRubber();
		m_bDrawingRubber = FALSE;
		
		if (! m_rcClip.IsRectEmpty())
		{
			// adjust position with scroll position
			CRect rcInDib(m_rcClip);
			ClientToDib(rcInDib);
			
			// create float DIB
			HDIB hDib = m_pDib->CopyRect(rcInDib);
			
			// create new float DIB window
			CreateFloatWnd(hDib, m_rcClip.TopLeft());
		}
	}
	
	CScrollView::OnLButtonUp(nFlags, point);
}

void CimageCAMView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	SetStatusBarCursorPosition(point);	
	
	// change rectangle
	if (m_bDrawingRubber)
		DrawRubber(point);
	
	if (m_bDrawCurve && nFlags == MK_LBUTTON)
	{
		DrawTmpCurve();
		ClientToDib(point);
		if (m_nDrawCurveStep == 1)
			m_ptCurve[3] = point;
		else if (m_nDrawCurveStep == 2)
			m_ptCurve[2] = point;
		else if (m_nDrawCurveStep == 3)
			m_ptCurve[1] = point;
		DrawTmpCurve();
	}
	
	// draw FREELINE
	if (m_bDrawFreeline)
	{
		CPen pen(m_nPenStyle, m_nPenWidth, m_crPenColor);
		
		CClientDC dc(this);
		
		CPoint pt = point;
		ClientToDib(m_ptFreelineStart);
		ClientToDib(pt);
		CDC * pDibDC = m_pDib->BeginPaint(&dc);
		CPen* pOldPen = pDibDC->SelectObject(&pen);
		pDibDC->MoveTo(m_ptFreelineStart);
		pDibDC->LineTo(pt);
		pDibDC->SelectObject(pOldPen);
		m_pDib->EndPaint();
		Invalidate(FALSE);
		
		// new start point
		m_ptFreelineStart = point;
	}
	else if (m_bErasing)
	{
		// set a pixel anyway
		CClientDC dc(this);
		ClientToDib(point);
		CRect rc(point.x-4, point.y-4, point.x+4, point.y+4);
		CDC * pDibDC = m_pDib->BeginPaint(&dc);
		CBrush brush(m_crFillColor);
		CBrush *pOldBrush = pDibDC->SelectObject(&brush);
		CPen Pen(PS_SOLID, 1, m_crFillColor);
		CPen *pOldPen = pDibDC->SelectObject(&Pen);
		pDibDC->Ellipse(&rc);
		pDibDC->SelectObject(pOldPen);
		pDibDC->SelectObject(pOldBrush);
		m_pDib->EndPaint();
		Invalidate(FALSE);
	}
	CScrollView::OnMouseMove(nFlags, point);
}
////////////////////////////////////////////////////////
void CimageCAMView::DrawRubber(CPoint point)
{
	// get DC and set its ROP
	CClientDC dc(this);
	
	// define used pen
	int nPenStyle;
	int nPenWidth;
	COLORREF color;
	int nOldRop = dc.SetROP2(R2_NOTXORPEN);
	if (m_nDrawType == DT_SELECT || m_nDrawType == DT_TEXT)
	{
		nPenStyle = PS_DOT;
		nPenWidth = 1;
		color = RGB(0,0,0);
	}
	else
	{
		nPenStyle = m_nPenStyle;
		nPenWidth = m_nPenWidth;
		color = m_crPenColor;
	}
	CPen pen(nPenStyle, nPenWidth, color);
	CPen* pOldPen = dc.SelectObject(&pen);
	
	// if there is rectangle drawn, clear it
	DoDrawRubber(&dc, m_rcRubber);
	
	// Adjust cooridnates for select
	if (m_nDrawType == DT_SELECT)
	{
		// get current scroll pos
		int nScrollX = GetScrollPos(SB_HORZ);
		int nScrollY = GetScrollPos(SB_VERT);
		// calculate new scroll pos, and set it
		CRect rcClient;
		GetClientRect(&rcClient);
		int nMinX, nMaxX, nMinY, nMaxY;
		GetScrollRange(SB_HORZ, &nMinX, &nMaxX);
		GetScrollRange(SB_VERT, &nMinY, &nMaxY);
		BOOL bNeedRedraw = FALSE;
		if ((rcClient.Width() < m_pDib->GetWidth()) &&
			(point.x < 0 || point.x > rcClient.right))
		{
			nScrollX += point.x;
			nScrollX = BOUND(nScrollX, nMinX, nMaxX);
			SetScrollPos(SB_HORZ, nScrollX);
			bNeedRedraw = TRUE;
		}
		if ((rcClient.Height() < m_pDib->GetHeight()) &&
			(point.y < 0 || point.y > rcClient.bottom))
		{
			nScrollY += point.y;
			nScrollY = BOUND(nScrollY, nMinY, nMaxY);
			SetScrollPos(SB_VERT, nScrollY);
			bNeedRedraw = TRUE;
		}
		if (bNeedRedraw)
		{
			// redraw
			Invalidate(FALSE);
			UpdateWindow();
		}
		// normalize point coordinate
		if (AdjustPointinDib(point))
		{
			ClientToScreen(&point);
			SetCursorPos(point.x, point.y);
			Screen

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -