📄 edgecontourview.cpp
字号:
// EdgeContourView.cpp : implementation of the CEdgeContourView class
//
#include "stdafx.h"
#include "EdgeContour.h"
#include "EdgeContourDoc.h"
#include "EdgeContourView.h"
#include "mainfrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView
IMPLEMENT_DYNCREATE(CEdgeContourView, CScrollView)
BEGIN_MESSAGE_MAP(CEdgeContourView, CScrollView)
//{{AFX_MSG_MAP(CEdgeContourView)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_COMMAND(ID_EDGE_CONTOUR, OnEdgeContour)
ON_COMMAND(ID_EDGE_FILL, OnEdgeFill)
ON_COMMAND(ID_EDGE_FILL2, OnEdgeFill2)
ON_COMMAND(ID_EDGE_GAUSS, OnEdgeGauss)
ON_COMMAND(ID_EDGE_HOUGH, OnEdgeHough)
ON_COMMAND(ID_EDGE_KIRSCH, OnEdgeKirsch)
ON_COMMAND(ID_EDGE_PREWITT, OnEdgePrewitt)
ON_COMMAND(ID_EDGE_ROBERT, OnEdgeRobert)
ON_COMMAND(ID_EDGE_SOBEL, OnEdgeSobel)
ON_COMMAND(ID_EDGE_TRACE, OnEdgeTrace)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView construction/destruction
CEdgeContourView::CEdgeContourView()
{
// TODO: add construction code here
}
CEdgeContourView::~CEdgeContourView()
{
}
BOOL CEdgeContourView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView drawing
void CEdgeContourView::OnDraw(CDC* pDC)
{
BeginWaitCursor();
CEdgeContourDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int cxDIB = (int) pDoc->GetDibImage()->DIBWidth(lpDIB);
int cyDIB = (int) pDoc->GetDibImage()->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;
}
pDoc->GetDibImage()->PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}
EndWaitCursor();
}
void CEdgeContourView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// TODO: calculate the total size of this view
}
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView printing
BOOL CEdgeContourView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 设置总页数为一。
pInfo->SetMaxPage(1);
return DoPreparePrinting(pInfo);
}
void CEdgeContourView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CEdgeContourView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView diagnostics
#ifdef _DEBUG
void CEdgeContourView::AssertValid() const
{
CScrollView::AssertValid();
}
void CEdgeContourView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CEdgeContourDoc* CEdgeContourView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEdgeContourDoc)));
return (CEdgeContourDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CEdgeContourView message handlers
BOOL CEdgeContourView::OnEraseBkgnd(CDC* pDC)
{
// 主要是为了设置子窗体默认的背景色
// 背景色由文档成员变量m_refColorBKG指定
CEdgeContourDoc* pDoc = GetDocument();
CBrush brush(pDoc->m_refColorBKG);
CBrush* pOldBrush = pDC->SelectObject(&brush);
CRect rectClip;
pDC->GetClipBox(&rectClip); // 获取重绘区域
pDC->PatBlt(rectClip.left, rectClip.top, rectClip.Width(),
rectClip.Height(), PATCOPY);
pDC->SelectObject(pOldBrush);
return TRUE;
}
void CEdgeContourView::OnEditCopy()
{
// 复制当前图像
CEdgeContourDoc* pDoc = GetDocument();
if (OpenClipboard())
{
BeginWaitCursor();
EmptyClipboard();
// 复制当前图像到剪贴板
SetClipboardData (CF_DIB, pDoc->GetDibImage()->
CopyHandle((HANDLE) pDoc->GetHDIB()) );
CloseClipboard();
EndWaitCursor();
}
}
void CEdgeContourView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
// 如果当前DIB对象不空,复制菜单项有效
pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}
void CEdgeContourView::OnEditPaste()
{
// 粘贴图像
HDIB hNewDIB = NULL;
CEdgeContourDoc* pDoc = GetDocument();
if (OpenClipboard())
{
BeginWaitCursor();
// 读取剪贴板中的图像
hNewDIB = (HDIB)pDoc->GetDibImage()->CopyHandle(::GetClipboardData(CF_DIB));
CloseClipboard();
if (hNewDIB != NULL)
{
pDoc->ReplaceHDIB(hNewDIB); // 替换DIB,同时释放旧DIB对象
pDoc->InitDIBData(); // 更新DIB大小和调色板
pDoc->SetModifiedFlag(TRUE); // 设置脏标记
SetScrollSizes(MM_TEXT, pDoc->GetDocSize()); // 重新设置滚动视图大小
OnDoRealize((WPARAM)m_hWnd,0); // 实现新的调色板
pDoc->UpdateAllViews(NULL); // 更新视图
}
EndWaitCursor();
}
}
void CEdgeContourView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
// 如果当前剪贴板中有DIB对象,粘贴菜单项有效
pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}
LRESULT CEdgeContourView::OnDoRealize(WPARAM wParam, LPARAM)
{
ASSERT(wParam != NULL);
CEdgeContourDoc* pDoc = GetDocument();
if (pDoc->GetHDIB() == NULL)
{
return 0L;
}
CPalette* pPal = pDoc->GetDocPalette();
if (pPal != NULL)
{
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 CEdgeContourView::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 CEdgeContourView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType)
{
CScrollView::OnInitialUpdate();
ASSERT(GetDocument() != NULL);
SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}
void CEdgeContourView::OnEdgeContour()
{
//轮廓提取运算
CEdgeContourDoc* pDoc = GetDocument();
LPSTR lpDIB; // 指向DIB的指针
LPSTR lpDIBBits; // 指向DIB象素指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的轮廓提取,其它的可以类推)
if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
{
MessageBox("目前只支持256色位图的运算!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
return;
}
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB);
// 调用FillDIB()函数对DIB进行轮廓提取
if (pDoc->GetDibImage()->ContourDIB(lpDIBBits,
WIDTHBYTES(pDoc->GetDibImage()->DIBWidth(lpDIB) * 8),
pDoc->GetDibImage()->DIBHeight(lpDIB)))
{
pDoc->SetModifiedFlag(TRUE); // 设置脏标记
pDoc->UpdateAllViews(NULL); // 更新视图
}
else
{
MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
EndWaitCursor();
}
void CEdgeContourView::OnEdgeFill()
{
//种子填充运算
CEdgeContourDoc* pDoc = GetDocument();
LPSTR lpDIB; // 指向DIB的指针
LPSTR lpDIBBits; // 指向DIB象素指针
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的种子填充,其它的可以类推)
if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
{
MessageBox("目前只支持256色位图的运算!", "系统提示" ,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -