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

📄 zhdlg.cpp

📁 简单的手写识别程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// zhDlg.cpp : implementation file
//

#include "stdafx.h"
#include "math.h"
#include "zh.h"
#include "zhDlg.h"
#include "ziku.h"

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

#define Startx 30
#define Starty 29
#define wid  194
#define lon 193
CPoint down_point,up_point;
CString zi[100];
int m;
int num[100];

int fat=0;//判断是否最值点已经赋值
CPoint allzz,allzy,allzs,allzx;//用来就出外接矩形
CPoint alldata[10000];//存放每个点的坐标
int allnum=0;//总点数
CPoint min,max,zero;

#define FONTS "data\\hzk24s"//标准24点阵宋体汉字字库
CFile cf;CString hzfile;BOOL bOpenOK;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZhDlg dialog

CZhDlg::CZhDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CZhDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CZhDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CZhDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CZhDlg)
	DDX_Control(pDX, IDC_LIST4, m_showb);
	DDX_Control(pDX, IDC_LIST1, m_showa);
	DDX_Control(pDX, IDC_SPIN1, m_counting);
	DDX_Control(pDX, IDC_EDIT1, m_hzs);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CZhDlg, CDialog)
	//{{AFX_MSG_MAP(CZhDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_NOTIFY(NM_OUTOFMEMORY, IDC_SPIN1, OnOutofmemorySpin1)
	ON_NOTIFY(NM_CLICK, IDC_LIST1, OnClickList1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_TIMER()
	ON_NOTIFY(NM_CLICK, IDC_LIST4, OnClickList4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZhDlg message handlers

BOOL CZhDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	mouseDown=0;
	i=j=m=0;
	arch=arcs=arcp=arcn=arcz=0;
	m_counting.SetRange ( -1 ,100 ) ; //spin控件设置
	zero.x=40;zero.y=39;
	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here


	hzfile=FONTS;
	bOpenOK = cf.Open(hzfile,CFile::modeRead );
	if (bOpenOK==0)
	{
		CString Msg="字库文件:"+hzfile+"不能打开!";
		AfxMessageBox(Msg,MB_OK|MB_ICONINFORMATION);
		OnCancel();
	} 
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CZhDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CZhDlg::OnPaint() 
{
	CPaintDC dc(this);
	HBRUSH hB;
	hB=CreateSolidBrush(RGB(255,255,255));
	SelectObject(dc,hB);
	Rectangle(dc,Startx,Starty,Startx+wid-1,Starty+lon-1);
	DeleteObject(hB);
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CZhDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CZhDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);
	if((point.x>=Startx)&&(point.x<Startx+wid)&&(point.y>=Starty)&&(point.y<Starty+lon))
	{
		mouseDown=1;
		HCURSOR m_HCross;
		m_HCross=AfxGetApp()->LoadCursor(IDC_CURSOR2);
		SetCursor(m_HCross);
	}
	down_point=point;
	SetCapture();
	i=0;
	p[i]=point;i++;
	CDialog::OnLButtonDown(nFlags, point);
}

void CZhDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	mouseDown=0;
	if(((point.x>=Startx)&&(point.x<Startx+wid)&&(point.y>=Starty)&&(point.y<Starty+lon))||mouseDown==1)
	{
//		if (mouseDown)  	
		m_counting.SetPos(m_counting.GetPos()+1); 
		p[i]=point;i++;
		up_point=point;
		ReleaseCapture(); 
		bihua=m_counting.GetPos();
//		getzd(p[300],i);
		int ds;
		float tan,k1,k2,a,b;
		float arc;
		int hvalue=0;
		int svalue=0;
		float hm=0;
		float sm=0;
		CString ms;
		zz=zy=zs=zx=p[0];
		ds=i;
		for(i=0;i<ds;i++)
		{
			if(p[i].x<zz.x) zz=p[i];
			if(p[i].x>zy.x) zy=p[i];
			if(p[i].y<zx.y) zx=p[i];
			if(p[i].y>zs.y) zs=p[i];
			hvalue=hvalue+p[i].x;
			svalue=svalue+p[i].y;

		}

		if(fat==0)
		{allzz=zz;allzy=zy;allzx=zx;allzs=zs;fat++;}
		else
		{
			if(zz.x<allzz.x) allzz=zz;
			if(zy.x>allzy.x) allzy=zy;
			if(zx.y<allzx.y) allzx=zx;
			if(zs.y>allzs.y) allzs=zs;
		}
		for(i=allnum;i<allnum+ds;i++)
			alldata[i]=p[i-allnum];
		allnum=allnum+ds;

		for(i=0;i<ds;i++)
		{
			hm=hm+fabs((float)p[i].x-(float)hvalue/(float)ds);
			sm=sm+fabs((float)p[i].y-(float)svalue/(float)ds);
		}
		a=(float)(zy.x-zz.x);
		b=(float)(zs.x-zx.x);
		if(a==0)   a=0.001;
		if(b==0)   b=0.001;
		if(zz.x==zy.x && zy.x==zs.x && zs.x==zx.x && zx.x==zz.x) {tan=0;ms.Format("竖");arcs++;}
		else
		{
//			if(((fabs(zz.x-zy.x)<10) && (fabs(zz.y-zy.y)<10)) && ((fabs(zs.x-zx.x)>10) || (fabs(zs.y-zx.y)>10))) tan=0;
//			else if(((fabs(zs.x-zx.x)<10) && (fabs(zs.y-zx.y)<10)) && ((fabs(zz.x-zy.x)>10) || (fabs(zz.y-zy.y)>10))) tan=0; //防止始点抖动
//			else
//			{
				if(((fabs(up_point.x-zz.x)<5)||(fabs(up_point.y-zz.y)<5))||((fabs(up_point.x-zy.x)<5)||(fabs(up_point.y-zy.y)<5))||((fabs(up_point.x-zs.x)<5)||(fabs(up_point.y-zs.y)<5))||((fabs(up_point.x-zx.x)<5)||(fabs(up_point.y-zx.y)<5)))  //防止最末点抖动
				{
					if((float)(hm/(float)ds)<3) {tan=0;ms.Format("竖");arcs++;}
					else if((float)(sm/(float)ds)<3) {tan=0;ms.Format("横");arch++;}
					else
					{
						k1=(float)((zy.y-zz.y)/a);
						k2=(float)((zs.y-zx.y)/b);
						tan=(float)fabs((k2-k1)/(1+k1*k2));
					}
				}
				else tan=100;
//			}
		}//计算两条直线的夹角以区别出折
		for(i=0;i<ds;i++)
		{
			p[i].x=0;
		    p[i].y=0;
		}//清除所有p[]点
		if (down_point.x==up_point.x)  arc=100;
		else arc=(float)(up_point.y-down_point.y)/(up_point.x-down_point.x);  //计算水平夹角

		if(tan>0.1) {ms.Format("折");arcz++;}
		else
		{
			if(up_point.y<down_point.y) 
			{
				if(bihua>(arch+arcs+arcp+arcn+arcz))
				{ms.Format("横");arch++;}
			}
			else
			{
				if((up_point.x<down_point.x)&&(down_point.x-up_point.x>10)) {ms.Format("撇");arcp++;}
				else
				{
				//	if ((float)fabs(arc)<(float)0.314)  {ms.Format("横");arch++;}
				//	if (fabs(arc)>3.414)  {ms.Format("竖");arcs++;}
				//	if ((arc<=-0.414)&&(arc>=-3.414))  {ms.Format("撇");arcp++;}
					if ((arc>=0.314)&&(arc<=3.414))
					{ms.Format("捺");arcn++;}
				}
			}
		}

		int   nLength= m_hzs.SendMessage(WM_GETTEXTLENGTH);//获得文本长度
		m_hzs.SetSel(nLength,nLength);
		m_hzs.ReplaceSel(ms);

		if(bihua>10)
		{ 
			AfxMessageBox("输入笔画过多,无法识别");
			OnButton1();
		}
		else
		{
//			if((point.x>=Startx)&&(point.x<Startx+wid)&&(point.y>=Starty)&&(point.y<Starty+lon))
//			{
				SetTimer(1, 1000,NULL) ;
//			}
		
		}
	}
	mouseDown=0;
	CDialog::OnLButtonUp(nFlags, point);
//	CString zi;

}

void CZhDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
//	int x,y;
	CBrush Brush (RGB (0, 0 , 255) );
	CBrush* pOldBrush;
	CDC *pDC=GetDC();
	CRgn Rgn;
	Rgn.CreateRectRgn(Startx,Starty,Startx+wid-1,Starty+lon-1);
	pDC->SelectClipRgn(&Rgn); //为指定设备场景选择新的剪裁区
	if((point.x>=Startx)&&(point.x<Startx+wid)&&(point.y>=Starty)
		&&(point.y<Starty+lon))
	{
		HCURSOR m_HCross;
		m_HCross=AfxGetApp()->LoadCursor(IDC_CURSOR2);
		SetCursor(m_HCross);
	} //当鼠标移到那个写字区域的时候,设置鼠标样式 
	if((mouseDown==1)&&(point.x>=Startx)&&(point.x<Startx+wid)
		&&(point.y>=Starty)&&(point.y<Starty+lon))
	{
		// Select the brush into the device context .
		CBrush* pOldBrush = pDC->SelectObject (&Brush);
		pDC->Ellipse(point.x-4,point.y-4,point.x+4,point.y+4);
		pDC->SelectObject (pOldBrush );
		p[i]=point;i++;

⌨️ 快捷键说明

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