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

📄 dip_systemview.cpp

📁 image processing work:put the 256 color picture into incode and encodel.
💻 CPP
字号:
// DIP_SystemView.cpp : implementation of the CDIP_SystemView class
//

#include "stdafx.h"
#include "DIP_System.h"
#include "DIP_SystemDoc.h"
#include "DIP_SystemView.h"
#include "MainFrm.h"
#include "Coding.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif



/////////////////////////////////////////////////////////////////////////////
// CDIP_SystemView

IMPLEMENT_DYNCREATE(CDIP_SystemView, CScrollView)

BEGIN_MESSAGE_MAP(CDIP_SystemView, CScrollView)
	//{{AFX_MSG_MAP(CDIP_SystemView)
//	ON_COMMAND(ID_OPEN_OPERATE, OnOpenOperate)
//	ON_COMMAND(ID_CLOSE_OPERATE, OnCloseOperate)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_LZW_ENCODING, OnLzwEncoding)
	ON_COMMAND(ID_LZW_DECODING, OnLzwDecoding)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDIP_SystemView construction/destruction

CDIP_SystemView::CDIP_SystemView()
{
	m_nInteract = 0;
}

CDIP_SystemView::~CDIP_SystemView()
{
}

BOOL CDIP_SystemView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////[辅助函数]/////////////////////////////////////////////////

///////////////////////////////////////////
//	获取并报告当前点的颜色值			 //	
///////////////////////////////////////////

void CDIP_SystemView::CurrentRGB(CPoint point)
{
	// 获取设备DC
	CDC* pDC = GetDC();

	// 获取当前点的RGB值
	COLORREF clrT = pDC->GetPixel(point);
	
	// 各颜色分量
	int R = GetRValue(clrT);
	int G = GetGValue(clrT);
	int B = GetBValue(clrT);

	// 报告当前点颜色值
	CString  strRGB;
	strRGB.Format("当前点颜色值:\r\n\r\n  R = %d\r\n  G = %d\r\n  B = %d", R, G, B);

	MessageBox(strRGB, "返回结果", MB_OK);
}





///////////////////////////////////////////
//	在当前点画圆						 //
///////////////////////////////////////////

void CDIP_SystemView::DrawCircle(CPoint point)
{
	if (m_nInteract > 0)
	{
		// 获取文档
		CDIP_SystemDoc* pDoc = GetDocument();

		// 获取当前图象尺寸
		CSize size = pDoc->GetDocSize();

		// 获取当前滚动位置
		CPoint pt = GetScrollPosition();
		
		// 标定的区域
		CRect rect(point.x + pt.x + 8, point.y + pt.y + 8, point.x + pt.x + 24, point.y + pt.y + 24);

		// 越界保护
		if (rect.top < 0 || rect.bottom > size.cy + 8 || rect.left < -8 || rect.right > size.cx + 8)
			return;		
			
		// 获取设备环境句柄
		CDC* pDC = GetDC();
		
		// 设置选中区域的颜色
		COLORREF col;
		if (m_nInteract == 1)
			col = RGB(255, 0, 0);
		if (m_nInteract == 2)
			col = RGB(255, 255, 255);
		if (m_nInteract == 2)
			col = RGB(0, 0, 0);
		pDoc->m_clsDIB.SetCircleColor(pDoc->GetHDIB(), rect, col);
		
		// 重画视图
		InvalidateRect(rect);

		// 释放资源
		ReleaseDC(pDC);
	}
}



///////////////////////////////////////////
//	重画视图							 //
///////////////////////////////////////////

void CDIP_SystemView::OnDraw(CDC* pDC)
{
	// 显示等待光标
	BeginWaitCursor();
	
	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// 获取DIB
	HDIB hDIB = pDoc->GetHDIB();
	
	// 判断DIB是否为空
	if (hDIB != NULL)
	{
		LPBYTE lpDIB = (LPBYTE) ::GlobalLock((HGLOBAL) hDIB);
		
		// 获取DIB宽度
		int cxDIB = (int) pDoc->m_clsDIB.DIBWidth(lpDIB);
		
		// 获取DIB高度
		int cyDIB = (int) pDoc->m_clsDIB.DIBHeight(lpDIB);

		::GlobalUnlock((HGLOBAL) hDIB);
		
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		
		CRect rcDest;
		
		// 判断是否是打印
		if (pDC->IsPrinting())
		{
			// 是打印,计算输出图像的位置和大小,以便符合页面
			
			// 获取打印页面的水平宽度(象素)
			int cxPage = pDC->GetDeviceCaps(HORZRES);
			
			// 获取打印页面的垂直高度(象素)
			int cyPage = pDC->GetDeviceCaps(VERTRES);
			
			// 获取打印机每英寸象素数
			int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
			int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
			
			// 计算打印图像大小(缩放,根据页面宽度调整图像大小)
			rcDest.top = rcDest.left = 0;
			rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch) / ((double)cxDIB * cxInch));
			rcDest.right = cxPage;
			
			// 计算打印图像位置(垂直居中)
			int temp = cyPage - (rcDest.bottom - rcDest.top);
			rcDest.bottom += temp/2;
			rcDest.top += temp/2;
		}
		else // 非打印
		{
			// 不必缩放图像
			rcDest = rcDIB;
		}
		// 输出DIB
		pDoc->m_clsDIB.PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
			&rcDIB, pDoc->GetDocPalette());
	}
	// 恢复正常光标
	EndWaitCursor();
}

void CDIP_SystemView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
}

/////////////////////////////////////////////////////////////////////////////
//									 图像打印							   //
/////////////////////////////////////////////////////////////////////////////

BOOL CDIP_SystemView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 设置总页数为一。
	pInfo->SetMaxPage(1);

	return DoPreparePrinting(pInfo);
}

void CDIP_SystemView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CDIP_SystemView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CDIP_SystemView diagnostics

#ifdef _DEBUG
void CDIP_SystemView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CDIP_SystemView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CDIP_SystemDoc* CDIP_SystemView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDIP_SystemDoc)));
	return (CDIP_SystemDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDIP_SystemView message handlers
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
//																		   //
//								基本位图操作							   //
//																		   //
/////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////
//	设置子窗体默认背景色为m_refColorBKG  //
///////////////////////////////////////////

BOOL CDIP_SystemView::OnEraseBkgnd(CDC* pDC) 
{
	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();

	// 创建一个Brush
	CBrush brush(pDoc->m_refColorBKG);                                              
                                                                                  
	// 保存以前的Brush
	CBrush* pOldBrush = pDC->SelectObject(&brush);
	
	// 获取重绘区域
	CRect rectClip;
	pDC->GetClipBox(&rectClip);
	
	// 重绘
	pDC->PatBlt(rectClip.left, rectClip.top, rectClip.Width(), rectClip.Height(), PATCOPY);

	// 恢复以前的Brush
	pDC->SelectObject(pOldBrush);                                                  

	// 返回
	return TRUE;
}


///////////////////////////////////////////
//	实现新的调色板						 //
///////////////////////////////////////////

void CDIP_SystemView::OnDoRealize(WPARAM wParam, LPARAM lParam)
{
	ASSERT(wParam != NULL);

	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();
	
	// 判断DIB是否为空
	if (pDoc->GetHDIB() == NULL)
	{
		// 直接返回
		return;
	}
	
	// 获取Palette
	CPalette* pPal = pDoc->GetDocPalette();
	if (pPal != NULL)
	{
		// 获取MainFrame
		CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
		ASSERT_KINDOF(CMainFrame, pAppFrame);
		
		CClientDC appDC(pAppFrame);

		// All views but one should be a background palette.
		// wParam contains a handle to the active view, so the SelectPalette
		// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
		CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
		
		if (oldPalette != NULL)
		{
			UINT nColorsChanged = appDC.RealizePalette();
			if (nColorsChanged > 0)
				pDoc->UpdateAllViews(NULL);
			appDC.SelectPalette(oldPalette, TRUE);
		}
	}
}


///////////////////////////////////////////
//	计算窗口视图大小,以计算滚动条设置   //	
///////////////////////////////////////////

void CDIP_SystemView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	CScrollView::CalcWindowRect(lpClientRect, nAdjustType);
	ASSERT(GetDocument() != NULL);
	SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}

void CDIP_SystemView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
	if (bActivate)
	{
		ASSERT(pActivateView == this);
		OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE);
	}
}


///////////////////////////////////////////
//	复制当前DIB到剪贴版					 //	
///////////////////////////////////////////

void CDIP_SystemView::OnEditCopy() 
{
	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();
	
	// 打开剪贴板
	if (OpenClipboard())
	{
		// 更改光标形状
		BeginWaitCursor();
		
		// 清空剪贴板
		EmptyClipboard();
		
		// 复制当前图像到剪贴板
		SetClipboardData (CF_DIB, pDoc->m_clsDIB.CopyHandle((HANDLE) pDoc->GetHDIB()) );
		
		// 关闭剪贴板
		CloseClipboard();
		
		// 恢复光标
		EndWaitCursor();
	}
}

void CDIP_SystemView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	// 如果当前DIB对象不空,复制菜单项有效
	pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}


///////////////////////////////////////////
//	从剪贴版粘贴DIB到当前视图			 //	
///////////////////////////////////////////

void CDIP_SystemView::OnEditPaste() 
{
	// 创建新DIB
	HDIB hNewDIB = NULL;

	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();

	// 打开剪贴板
	if (OpenClipboard())
	{
		// 更改光标形状
		BeginWaitCursor();

		// 读取剪贴板中的图像
		hNewDIB = (HDIB) pDoc->m_clsDIB.CopyHandle(::GetClipboardData(CF_DIB));

		// 关闭剪贴板
		CloseClipboard();
		
		// 判断是否读取成功
		if (hNewDIB != NULL)
		{
			// 替换DIB,同时释放旧DIB对象
			pDoc->ReplaceHDIB(hNewDIB);

			// 更新DIB大小和调色板
			pDoc->InitDIBData();

			// 设置脏标记
			pDoc->SetModifiedFlag(TRUE);
			
			// 重新设置滚动视图大小
			SetScrollSizes(MM_TEXT, pDoc->GetDocSize());

			// 实现新的调色板
			OnDoRealize((WPARAM)m_hWnd,0);

			// 更新视图
			pDoc->UpdateAllViews(NULL);
		}

		// 恢复光标
		EndWaitCursor();
	}
}

void CDIP_SystemView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	// 如果当前剪贴板中有DIB对象,粘贴菜单项有效
	pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}


///////////////////////////////////////////


///////////////////////////////////////////
//	对图像实施GIF编码压缩				 //	
///////////////////////////////////////////

void CDIP_SystemView::OnLzwEncoding() 
{
	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();
	
	// 选择保存文件
    CFileDialog fileDlg(FALSE, "*.GIF", "*.GIF", NULL, "GIF (*.GIF)|*.GIF||", this);
	fileDlg.m_ofn.Flags |= OFN_FILEMUSTEXIST;
	fileDlg.m_ofn.lpstrTitle = "File to save as";

	// 保存压缩图象
	if (fileDlg.DoModal() == IDOK)
	{
		CFile file;
		file.Open(fileDlg.GetPathName(), CFile::modeCreate|CFile::modeReadWrite);


		CCoding clsCoding;
		clsCoding.DIBToGIF(pDoc->GetHDIB(), file, TRUE);
		file.Close();
	}            
}


///////////////////////////////////////////
//	对图像实施GIF编码解压缩				 //	
///////////////////////////////////////////

void CDIP_SystemView::OnLzwDecoding() 
{
	// 创建新DIB
	HDIB hNewDIB = NULL;
	
	// 获取文档
	CDIP_SystemDoc* pDoc = GetDocument();

	// 选择保存文件
    CFileDialog fileDlg(TRUE, "*.GIF", "*.GIF", NULL, "GIF文件 (*.GIF)|*.GIF||", this);
	fileDlg.m_ofn.Flags |= OFN_FILEMUSTEXIST;
	fileDlg.m_ofn.lpstrTitle = "Open File";

	// 保存压缩图象
	if (fileDlg.DoModal() == IDOK)
	{
		// 调用ZoomDIB()函数获取缩小后的DIB
		CCoding clsCoding;
		CFile file;
		file.Open(fileDlg.GetPathName(),CFile::modeReadWrite);
		hNewDIB = clsCoding.GIFToDIB(file);
		file.Close();

		// 判断缩放是否成功
		if (hNewDIB != NULL)
		{		
			// 替换DIB,同时释放旧DIB对象
			pDoc->ReplaceHDIB(hNewDIB);
	
			// 更新DIB大小和调色板
			pDoc->InitDIBData();
		
			// 重新设置滚动视图大小
			SetScrollSizes(MM_TEXT, pDoc->GetDocSize());

			// 更新视图
			pDoc->UpdateAllViews(NULL);
		}
		else
		{
			// 提示用户
			MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
		}
	}
}




void CDIP_SystemView::OnFileOpen() 
{
	// TODO: Add your command handler code here
	
}

⌨️ 快捷键说明

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