wwwview.cpp

来自「一个简单的打开位图程序,并实现正交傅立叶变换.」· C++ 代码 · 共 439 行

CPP
439
字号
// wwwView.cpp : implementation of the CWwwView class
//

#include "stdafx.h"
#include "www.h"

#include "wwwDoc.h"
#include "wwwView.h"
#include "mainfrm.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWwwView

IMPLEMENT_DYNCREATE(CWwwView, CScrollView)

BEGIN_MESSAGE_MAP(CWwwView, CScrollView)
	//{{AFX_MSG_MAP(CWwwView)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_COMMAND(ID_FREQ_FOUR, OnFreqFour)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CWwwView construction/destruction

CWwwView::CWwwView()
{
	// TODO: add construction code here

}

CWwwView::~CWwwView()
{
}

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

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CWwwView drawing

void CWwwView::OnDraw(CDC* pDC)
{
	// 显示等待光标
	BeginWaitCursor();
	
	// 获取文档
	CWwwDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	
	// 获取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();
}

//void CWwwView::OnInitialUpdate()
//{
//	CScrollView::OnInitialUpdate();

//	CSize sizeTotal;
	// TODO: calculate the total size of this view
//	sizeTotal.cx = sizeTotal.cy = 100;
	//SetScrollSizes(MM_TEXT, sizeTotal);
//}

/////////////////////////////////////////////////////////////////////////////
// CWwwView printing

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

	return DoPreparePrinting(pInfo);
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CWwwView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CWwwView message handlers
BOOL CWwwView::OnEraseBkgnd(CDC* pDC) 
{
	// 主要是为了设置子窗体默认的背景色
	// 背景色由文档成员变量m_refColorBKG指定

	// 获取文档
	CWwwDoc* 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;

}

LRESULT CWwwView::OnDoRealize(WPARAM wParam, LPARAM)
{
	ASSERT(wParam != NULL);

	// 获取文档
	CWwwDoc* 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("\tCCh1_1View::OnPaletteChanged中调用SelectPalette()失败!\n");
		}
	}
	
	return 0L;
}

void CWwwView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	
}

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

void CWwwView::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);
	}
}

void CWwwView::OnEditCopy() 
{
	// 复制当前图像

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

void CWwwView::OnEditPaste() 
{
	// 粘贴图像
	
	// 创建新DIB
	HDIB hNewDIB = NULL;
	
	// 打开剪贴板
	if (OpenClipboard())
	{
		// 更改光标形状
		BeginWaitCursor();

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

		// 关闭剪贴板
		CloseClipboard();
		
		// 判断是否读取成功
		if (hNewDIB != NULL)
		{
			// 获取文档
			CWwwDoc* 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 CWwwView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	// 如果当前DIB对象不空,复制菜单项有效
	pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}

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



void CWwwView::OnFreqFour() 
{
// 图像付立叶变换
	
	// 获取文档
	CWwwDoc* 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();	
}

⌨️ 快捷键说明

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