// HandWriteDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "HandWrite.h"
#include "HandWriteDlg.h"
#include "afxdialogex.h"
#include "opencv2/opencv.hpp"
#include "windows.h"
#include "fstream"
#include
//#pragma comment(lib,"ml.lib")
using namespace std;
using namespace cv;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CHandWriteDlg 对话框
CHandWriteDlg::CHandWriteDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CHandWriteDlg::IDD, pParent)
, m_strRes(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CHandWriteDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_RESULT, m_strRes);
}
BEGIN_MESSAGE_MAP(CHandWriteDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_BUTTON_ERASE, &CHandWriteDlg::OnBnClickedButtonErase)
ON_BN_CLICKED(IDC_BUTTON_IDENTIFY, &CHandWriteDlg::OnBnClickedButtonIdentify)
END_MESSAGE_MAP()
// CHandWriteDlg 消息处理程序
BOOL CHandWriteDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
down_flag = false;
m_firstFlag = false;
m_start.x = 0;
m_start.y = 0;
m_start.x = 0;
m_start.y = 0;
CWnd* pWnd = GetDlgItem(IDC_STATIC_HAND);
pWnd->GetClientRect(&m_rect); //获取Picture控件的屏幕坐标区域
//this->ScreenToClient(&m_rect);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CHandWriteDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CHandWriteDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CHandWriteDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}
void CHandWriteDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (!PtInRect(m_rect,CPoint(point)))
{
down_flag=false;
}
CWnd* pWnd = GetDlgItem(IDC_STATIC_HAND);
CDC* dc = pWnd->GetDC();
CPen pen(PS_SOLID,10,RGB(0,0,0));
CPen *pOldPen=dc->SelectObject(&pen); //将其选入设备表
if (down_flag)
{
dc->MoveTo(m_start); /* 开始画图 */
dc->LineTo(point);
m_start=point;
}
dc->SelectObject(pOldPen);
ReleaseDC(dc);
CDialogEx::OnMouseMove(nFlags, point);
}
void CHandWriteDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
down_flag=false;
CDialogEx::OnLButtonUp(nFlags, point);
}
void CHandWriteDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if (PtInRect(m_rect,CPoint(point)))
{
down_flag=true;
m_firstFlag = true;
m_start=point;
}
CDialogEx::OnLButtonDown(nFlags, point);
}
void CHandWriteDlg::OnBnClickedButtonErase()
{
// TODO: 在此添加控件通知处理程序代码
m_firstFlag = false;
Invalidate();
}
void CHandWriteDlg::OnBnClickedButtonIdentify()
{
// TODO: 在此添加控件通知处理程序代码
if(m_firstFlag == false)
{
MessageBox(_T("请现在手写区写入数字!"));
return ;
}
CvSVM svm;
IplImage *test;
CString strResult;
char* str;
CRect pRect;
CWnd* pwnd=GetDlgItem(IDC_STATIC_HAND);//获取静态控件的指针 CRect pRect;
CDC* pDC=pwnd->GetDC(); //使用控件指针创建绘图用的DC
pwnd->GetClientRect(&pRect);
int w, h;
w = pRect.right - pRect.left;
h = pRect.bottom - pRect.top;
CBitmap bm;
bm.CreateCompatibleBitmap(pDC, w, h);
CDC memdc;
memdc.CreateCompatibleDC(pDC);
CBitmap*pOld=memdc.SelectObject(&bm);
memdc.BitBlt( 0, 0, w, h, pDC, pRect.left, pRect.top, SRCCOPY );
BITMAP btm;
bm.GetBitmap(&btm);
DWORD size=btm.bmWidthBytes*btm.bmHeight;
LPSTR lpData=(LPSTR)::GlobalAlloc(GPTR,size);
BITMAPINFOHEADER bih;
bih.biBitCount=btm.bmBitsPixel;
bih.biClrImportant=0;
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=btm.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=btm.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
GetDIBits(memdc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
BITMAPFILEHEADER bfh;
bfh.bfReserved1=bfh.bfReserved2=0;
bfh.bfType=((WORD)('M'<< 8)|'B');
bfh.bfSize=54+size;
bfh.bfOffBits=54;
CFile bf;
if(bf.Open(_T("a.bmp"),CFile::modeCreate|CFile::modeWrite))
{
bf.Write(&bfh,sizeof(BITMAPFILEHEADER));
bf.Write(&bih,sizeof(BITMAPINFOHEADER));
bf.Write(lpData,size);
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………