www.pudn.com > YangyongDIP.rar > YangyongDIPView.cpp


// YangyongDIPView.cpp : implementation of the CYangyongDIPView class 
// 
 
#include "stdafx.h" 
#include "YangyongDIP.h" 
 
#include "YangyongDIPDoc.h" 
#include "YangyongDIPView.h" 
#include "mainfrm.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView 
 
IMPLEMENT_DYNCREATE(CYangyongDIPView, CView) 
 
BEGIN_MESSAGE_MAP(CYangyongDIPView, CView) 
	//{{AFX_MSG_MAP(CYangyongDIPView) 
	ON_WM_ERASEBKGND() 
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste) 
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy) 
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy) 
	ON_COMMAND(ID_FREQ_FOUR, OnFreqFour) 
	ON_UPDATE_COMMAND_UI(ID_FREQ_FOUR, OnUpdateFreqFour) 
	ON_COMMAND(ID_FILE_SAVE_AS_PCX, OnFileSaveAsPcx) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView construction/destruction 
 
CYangyongDIPView::CYangyongDIPView() 
{ 
	// TODO: add construction code here 
 
} 
 
CYangyongDIPView::~CYangyongDIPView() 
{ 
} 
 
BOOL CYangyongDIPView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView drawing 
 
void CYangyongDIPView::OnDraw(CDC* pDC) 
{ 
	CYangyongDIPDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	// TODO: add draw code for native data here 
	// 显示等待光标 
	BeginWaitCursor(); 
		 
	// 获取DIB 
	HDIB hDIB = pDoc->GetHDIB(); 
	 
	// 判断DIB是否为空 
	if (hDIB != NULL) 
	{ 
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB); 
		 
		// 获取DIB宽度 
		int cxDIB = (int) ::DIBWidth(lpDIB); 
		 
		// 获取DIB高度 
		int cyDIB = (int) ::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 
		::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(), 
			&rcDIB, pDoc->GetDocPalette()); 
	} 
	 
	// 恢复正常光标 
	EndWaitCursor(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView printing 
 
BOOL CYangyongDIPView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// 设置总页数为一。 
	pInfo->SetMaxPage(1); 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CYangyongDIPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CYangyongDIPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView diagnostics 
 
#ifdef _DEBUG 
void CYangyongDIPView::AssertValid() const 
{ 
	CView::AssertValid(); 
} 
 
void CYangyongDIPView::Dump(CDumpContext& dc) const 
{ 
	CView::Dump(dc); 
} 
 
CYangyongDIPDoc* CYangyongDIPView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CYangyongDIPDoc))); 
	return (CYangyongDIPDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CYangyongDIPView message handlers 
LRESULT CYangyongDIPView::OnDoRealize(WPARAM wParam, LPARAM) 
{ 
	ASSERT(wParam != NULL); 
 
	// 获取文档 
	CYangyongDIPDoc* pDoc = GetDocument(); 
	 
	// 判断DIB是否为空 
	if (pDoc->GetHDIB() == NULL) 
	{ 
		// 直接返回 
		return 0L; 
	} 
	 
	// 获取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); 
		} 
		else 
		{ 
			TRACE0("\tCYangyongDIPView::OnPaletteChanged中调用SelectPalette()失败!\n"); 
		} 
	} 
	 
	return 0L; 
} 
 
void CYangyongDIPView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView); 
 
	if (bActivate) 
	{ 
		ASSERT(pActivateView == this); 
		OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE); 
	} 
	//CView::OnActivateView(bActivate, pActivateView, pDeactiveView); 
} 
 
void CYangyongDIPView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	CScrollView::OnInitialUpdate(); 
	ASSERT(GetDocument() != NULL); 
	 
	SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize()); 
	//CView::CalcWindowRect(lpClientRect, nAdjustType); 
} 
 
BOOL CYangyongDIPView::OnEraseBkgnd(CDC* pDC)  
{ 
	// TODO: Add your message handler code here and/or call default 
	// 主要是为了设置子窗体默认的背景色 
	// 背景色由文档成员变量m_refColorBKG指定 
 
	// 获取文档 
	CYangyongDIPDoc* 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;//CView::OnEraseBkgnd(pDC); 
} 
 
void CYangyongDIPView::OnEditPaste()  
{ 
	// TODO: Add your command handler code here 
	// 创建新DIB 
	HDIB hNewDIB = NULL; 
	 
	// 打开剪贴板 
	if (OpenClipboard()) 
	{ 
		// 更改光标形状 
		BeginWaitCursor(); 
 
		// 读取剪贴板中的图像 
		hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB)); 
 
		// 关闭剪贴板 
		CloseClipboard(); 
		 
		// 判断是否读取成功 
		if (hNewDIB != NULL) 
		{ 
			// 获取文档 
			CYangyongDIPDoc* pDoc = GetDocument(); 
 
			// 替换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 CYangyongDIPView::OnUpdateEditPaste(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	 
} 
 
void CYangyongDIPView::OnEditCopy()  
{ 
	// TODO: Add your command handler code here 
	// 获取文档 
	CYangyongDIPDoc* pDoc = GetDocument(); 
	 
	// 打开剪贴板 
	if (OpenClipboard()) 
	{ 
		// 更改光标形状 
		BeginWaitCursor(); 
		 
		// 清空剪贴板 
		EmptyClipboard(); 
		 
		// 复制当前图像到剪贴板 
		SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) ); 
		 
		// 关闭剪贴板 
		CloseClipboard(); 
		 
		// 恢复光标 
		EndWaitCursor(); 
	} 
} 
 
void CYangyongDIPView::OnUpdateEditCopy(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	pCmdUI->Enable(GetDocument()->GetHDIB() != NULL); 
} 
 
void CYangyongDIPView::OnFreqFour()  
{ 
	// TODO: Add your command handler code here 
	// 图像付立叶变换 
	 
	// 获取文档 
	CYangyongDIPDoc* pDoc = GetDocument(); 
	 
	// 指向DIB的指针 
	LPSTR	lpDIB; 
	 
	// 指向DIB象素指针 
	LPSTR    lpDIBBits; 
	 
	// 锁定DIB 
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB()); 
	 
	// 找到DIB图像象素起始位置 
	lpDIBBits = ::FindDIBBits(lpDIB); 
	 
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的付立叶变换,其它的可以类推) 
	if (::DIBNumColors(lpDIB) != 256) 
	{ 
		// 提示用户 
		MessageBox("目前只支持256色位图的付立叶变换!", "系统提示" , 
			MB_ICONINFORMATION | MB_OK); 
		 
		// 解除锁定 
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB()); 
		 
		// 返回 
		return; 
	} 
	 
	// 更改光标形状 
	BeginWaitCursor(); 
	 
	// 调用Fourier()函数进行付立叶变换 
	if (::Fourier(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB))) 
	{ 
		 
		// 设置脏标记 
		pDoc->SetModifiedFlag(TRUE); 
		 
		// 更新视图 
		pDoc->UpdateAllViews(NULL); 
	} 
	else 
	{ 
		// 提示用户 
		MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK); 
	} 
	 
	// 解除锁定 
	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB()); 
	 
	// 恢复光标 
	EndWaitCursor(); 
} 
 
void CYangyongDIPView::OnUpdateFreqFour(CCmdUI* pCmdUI)  
{ 
	// TODO: Add your command update UI handler code here 
	 
} 
 
void CYangyongDIPView::OnInitialUpdate()  
{ 
	CView::OnInitialUpdate(); 
	 
	// TODO: Add your specialized code here and/or call the base class 
	 
} 
 
void CYangyongDIPView::OnFileSaveAsPcx()  
{ 
	// TODO: Add your command handler code here 
	// 获取文档 
	CYangyongDIPDoc* pDoc = GetDocument(); 
	 
	// 指向DIB的指针 
	LPSTR	lpDIB; 
	 
	// 指向DIB象素指针 
	LPSTR    lpDIBBits; 
	 
	// 锁定DIB 
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB()); 
	 
	// 找到DIB图像象素起始位置 
	lpDIBBits = ::FindDIBBits(lpDIB); 
	 
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的行程编码) 
	if (::DIBNumColors(lpDIB) != 256) 
	{ 
		// 提示用户 
		MessageBox("目前只支持256色位图的行程编码!", "系统提示" , 
			MB_ICONINFORMATION | MB_OK); 
		 
		// 解除锁定 
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB()); 
		 
		// 返回 
		return; 
	} 
	 
	// 更改光标形状 
	BeginWaitCursor(); 
	 
	// 文件保存路径 
	CString strFilePath; 
	 
	// 初始化文件名为原始文件名 
	strFilePath = pDoc->GetPathName(); 
	 
	// 更改后缀为PCX 
	if (strFilePath.Right(4).CompareNoCase(".BMP") == 0) 
	{ 
		// 更改后缀为PCX 
		strFilePath = strFilePath.Left(strFilePath.GetLength()-3) + "PCX"; 
	} 
	else 
	{ 
		// 直接添加后缀PCX 
		strFilePath += ".PCX"; 
	} 
	 
	// 创建SaveAs对话框 
	CFileDialog dlg(FALSE, "PCX", strFilePath, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,  
		"PCX图像文件 (*.PCX) | *.PCX|所有文件 (*.*) | *.*||", NULL); 
	 
	// 提示用户选择保存的路径 
	if (dlg.DoModal() != IDOK) 
	{ 
		// 解除锁定 
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB()); 
		 
		// 恢复光标 
		EndWaitCursor(); 
		 
		// 返回 
		return; 
	} 
	 
	// 获取用户指定的文件路径 
	strFilePath = dlg.GetPathName(); 
	 
	// CFile和CFileException对象 
	CFile file; 
	CFileException fe; 
	 
	// 尝试创建指定的PCX文件 
	if (!file.Open(strFilePath, CFile::modeCreate | 
	  CFile::modeReadWrite | CFile::shareExclusive, &fe)) 
	{ 
		// 提示用户 
		MessageBox("打开指定PCX文件时失败!", "系统提示" ,  
			MB_ICONINFORMATION | MB_OK); 
		 
		// 返回 
		return; 
	} 
	 
	// 调用DIBToPCX256()函数将当前的DIB保存为256色PCX文件 
	if (::DIBToPCX256(lpDIB, file)) 
	{ 
		// 提示用户 
		MessageBox("成功保存为PCX文件!", "系统提示" ,  
			MB_ICONINFORMATION | MB_OK); 
	} 
	else 
	{ 
		// 提示用户 
		MessageBox("保存为PCX文件失败!", "系统提示" ,  
			MB_ICONINFORMATION | MB_OK); 
	} 
	 
	// 解除锁定 
	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB()); 
	 
	// 恢复光标 
	EndWaitCursor(); 
}