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

📄 zftdialog.cpp

📁 matlab版图像数字处理这本书的配套代码
💻 CPP
字号:
// ZFTDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ImageProcessing.h"
#include "ZFTDialog.h"
#include "dibapi.h"

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

/////////////////////////////////////////////////////////////////////////////
// CZFTDialog dialog


CZFTDialog::CZFTDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CZFTDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CZFTDialog)
	m_nHD = 0;
	m_nXS = 0;
	m_nLow = 0;
	m_nUp = 0;
	//}}AFX_DATA_INIT
}


void CZFTDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZFTDialog)
	DDX_Text(pDX, IDC_EDIT_HD, m_nHD);
	DDX_Text(pDX, IDC_EDIT_XS, m_nXS);
	DDX_Text(pDX, IDC_EDIT_LOW, m_nLow);
	DDX_Text(pDX, IDC_EDIT_UP, m_nUp);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CZFTDialog, CDialog)
	//{{AFX_MSG_MAP(CZFTDialog)
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_EN_KILLFOCUS(IDC_EDIT1, OnKillfocusEdit1)
	ON_EN_KILLFOCUS(IDC_EDIT2, OnKillfocusEdit2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZFTDialog message handlers

void CZFTDialog::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	//UpdateData(false);
	// TODO: Add your message handler code here
	CDC *pDC;
	CWnd *pwnd=GetDlgItem(IDC_GRAPHIC);
	pDC=pwnd->GetDC();
	pwnd->Invalidate();
	pwnd->UpdateWindow();
	pDC->Rectangle(0,0,320,320);
	CPen *pPenRed=new CPen;
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
	CPen *pPenGreen=new CPen;
	pPenGreen->CreatePen(PS_SOLID,1,RGB(0,255,0));
	CPen *pPenBlue=new CPen;
	pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));
	CGdiObject *pOldPen=pDC->SelectObject(pPenRed);
	pDC->MoveTo(10,10);
	pDC->LineTo(10,280);
	pDC->LineTo(310,280);

    long MaxCount;
    CString str;
	str.Format("0"); 
    pDC->TextOut(10,285,str);
    str.Format("50"); 
    pDC->TextOut(10+50,285,str);
	str.Format("100"); 
    pDC->TextOut(10+100,285,str);
	str.Format("150"); 
    pDC->TextOut(10+150,285,str);
	str.Format("200"); 
    pDC->TextOut(10+200,285,str);
	//str.Format("250"); 
    //pDC->TextOut(10+250,285,str);
    str.Format("255"); 
    pDC->TextOut(10+255,285,str);
	
	pDC->MoveTo(10,10);
	pDC->LineTo(5,15);
	pDC->MoveTo(10,10);
	pDC->LineTo(15,15);
	pDC->MoveTo(310,280);
	pDC->LineTo(305,275);
	pDC->MoveTo(310,280);
	pDC->LineTo(305,285);

	for(int i=0;i<256;i+=5)
	{
		if(i%10==0)
		{
			pDC->MoveTo(i+10,280);
			pDC->LineTo(i+10,285);
		}
		else
		{
			pDC->MoveTo(i+10,280);
			pDC->LineTo(i+10,283);
		}
	}
	for( i=m_nLow;i<=m_nUp;i++)
	{
		
	if(m_nCount[i]>MaxCount)
		MaxCount=m_nCount[i];
	}
	pDC->MoveTo(10,25);
	pDC->LineTo(14,25);
	//str.Format("最大像素数:%d",MaxCount);
	str.Format("最大像素数:%d",MaxCount); 
	pDC->TextOut(11,26,str);
    pDC->SelectObject(pPenGreen);
	pDC->MoveTo(10+m_nLow,280);
	pDC->LineTo(10+m_nLow,25);
    pDC->MoveTo(10+m_nUp,280);
	pDC->LineTo(10+m_nUp,25);
    pDC->SelectObject(pPenBlue);
	if(MaxCount>0)
	{
		for(int i=m_nLow;i<=m_nUp;i++)
		{
			pDC->MoveTo(i+10,280);
			int j=(int)(m_nCount[i]*256/MaxCount);
			pDC->LineTo(i+10,280-j);
		}
	}
	pDC->SelectObject(pOldPen);
	delete pPenRed;
    delete pPenBlue;
    delete pPenGreen;
	// Do not call CDialog::OnPaint() for painting messages
}

void CZFTDialog::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_MouseRect.PtInRect(point))
	{
		int x=point.x-m_MouseRect.left;
		m_nHD=x;
		m_nXS=m_nCount[x];
		UpdateData(false);
	}
	//InvalidateRect(m_MouseRect);
	CDialog::OnMouseMove(nFlags, point);
}

BOOL CZFTDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	unsigned char *lpDIBBits;
	CWnd *pwnd=GetDlgItem(IDC_GRAPHIC);
	pwnd->GetClientRect(&m_MouseRect);
	pwnd->ClientToScreen(&m_MouseRect);
	CRect rect;
	GetClientRect(&rect);
	ClientToScreen(&rect);
	m_MouseRect.top-=rect.top ;
	m_MouseRect.left-=rect.left;
	m_MouseRect.top+=25 ;
	m_MouseRect.left+=10;
	m_MouseRect.bottom=m_MouseRect.top+255 ;
	m_MouseRect.right=m_MouseRect.left+256;
	for(long i=0;i<256;i++)
	{
		m_nCount[i]=0;
	}
	long lineBytes;
	lineBytes=WIDTHBYTES(m_nWidth*8);
	for(long j=0;j<m_nHeight;j++)
	{
		for(long i=0;i<lineBytes;i++)
		{
			lpDIBBits=(unsigned char *)m_pDIBBits+lineBytes*j+i;
			m_nCount[*lpDIBBits]++;
		}
	}
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CZFTDialog::OnOK() 
{
	// TODO: Add extra validation here
	//because "CDialog::OnOK()" has been omitted,you should add "UpdateData();"!
	UpdateData();
	if(m_nLow>m_nUp)
	{
	int temp;
	temp=m_nLow;
	m_nLow=m_nUp;
	m_nUp=temp;
	UpdateData(false);
	}
	/*
	CRect rect;
	GetClientRect(rect);
	CString str;
	str.Format("(%d,%d)",rect.left ,rect.top );
	AfxMessageBox(str);
	*///output:(0,0)!
/*
    CRect rect;
	GetClientRect(rect);
	ClientToScreen(&rect);
	CString str2;
	str2.Format("rect:(%d,%d)",rect.left ,rect.top );
	AfxMessageBox(str2);

	CWnd *pWnd=GetDlgItem(IDC_GRAPHIC);
	pWnd->GetClientRect(m_MouseRect);
	pWnd->ClientToScreen(&m_MouseRect);
	m_MouseRect.top-=rect.top ;
	m_MouseRect.left-=rect.left;
	CString str1;
	str1.Format("m_MouseRect:(%d,%d)",m_MouseRect.left ,m_MouseRect.top );
	AfxMessageBox(str1);
*/
	

	InvalidateRect(m_MouseRect);
	//CDialog::OnOK();
}

void CZFTDialog::OnKillfocusEdit1() 
{
	// TODO: Add your control notification handler code here
    UpdateData();
	if(m_nLow>255||m_nLow<0)
	{
	CString str="请输入一个0和255之间的整数!";
	AfxMessageBox(str);
	CWnd *pWnd=GetDlgItem(IDC_EDIT1);
	pWnd->SetFocus(); 
	}
	
}

void CZFTDialog::OnKillfocusEdit2() 
{
	// TODO: Add your control notification handler code here
	UpdateData();
	if(m_nUp>255||m_nUp<0)
	{
	CString str="请输入一个0和255之间的整数!";
	AfxMessageBox(str);
	CWnd *pWnd=GetDlgItem(IDC_EDIT2);
	pWnd->SetFocus(); 
	}
	
}

⌨️ 快捷键说明

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