模拟拼音输入法Dlg.cpp
上传用户:sztcmy
上传日期:2013-01-09
资源大小:154k
文件大小:11k
- // 模拟拼音输入法Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "模拟拼音输入法.h"
- #include "模拟拼音输入法Dlg.h"
- #include <fstream.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // 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()
- /////////////////////////////////////////////////////////////////////////////
- // CMyDlg dialog
- CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CMyDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CMyDlg)
- // NOTE: the ClassWizard will add member initialization here
- flag=0;
- page=0;
- length=0;
- Num=0;
- strstr1="";
- strstr2="";
- m_text = _T("");
- m_hanzi = _T("");
- m_pinyin= _T("");
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CMyDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CMyDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- DDX_Text(pDX, IDC_EDITTEXT, m_text);
- DDX_Text(pDX, IDC_DISPLAYHZ, m_hanzi);
- DDX_Text(pDX, IDC_INPUTPY, m_pinyin);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
- //{{AFX_MSG_MAP(CMyDlg)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_EN_CHANGE(IDC_INPUTPY, OnChangeInputpy)
- ON_WM_KEYDOWN()
- ON_BN_CLICKED(IDEXIT, OnExit)
- ON_BN_CLICKED(IDC_ABOUT, OnAbout)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyDlg message handlers
- BOOL CMyDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 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
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
- void CMyDlg::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 CMyDlg::OnPaint()
- {
- 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();
- ReadFile();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CMyDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- /*****************把汉字内码表读入内存中************************/
- void CMyDlg::ReadFile()
- {
- ifstream inf("汉字内码表.txt",ios::in); //打开汉字内码表
- if(!inf)
- {
- AfxMessageBox("Can not open the file 汉字内码表.txt ");
- return;
- }
- CString str1,str2,str;
- char ch[3002];
- inf.getline(ch,3000,'#'); //一行一行地读取文件
- while(!inf.eof()) //文件未读完
- {
- str="";
- str=ch; //把读取的内容赋值给str
- str+='#'; //以分号“#”表示结束
- int i=0;
- str1="";
- str2="";
- while((str[i]==' ')||(str[i]=='n')||(str[i]=='r')) //抛弃空格符以及回车符
- i++;
- while(str[i]>='a'&&str[i]<='z') //接收拼音,存入变量str1中
- {
- str1+=str[i];
- i++;
- }
- while(str[i]!='#') //接收汉字,存入变量str2中
- {
- while((str[i]==' ')||(str[i]=='n')||(str[i]=='r')) //抛弃空格符以及回车符
- i++;
- if(str[i]=='#')
- break;
- else
- {
- str2+=str[i];
- i++;
- }
- }
- m_Map[str1]=str2; //把所接收的拼音跟汉字通过映射类的函数
- //建立一一对应的映射关系
- inf.getline(ch,3000,'#'); //读取下一行字符
- }
- }
- /*********************响应输入拼音******************************/
- void CMyDlg::OnChangeInputpy()
- {
- // TODO: If this is a RICHEDIT control, the control will not
- // send this notification unless you override the CDialog::OnInitDialog()
- // function and call CRichEditCtrl().SetEventMask()
- // with the ENM_CHANGE flag ORed into the mask.
-
- // TODO: Add your control notification handler code here
- UpdateData(TRUE); //接收用户输入
- CString str1;
- strstr2="";
- flag=0;
- m_hanzi="";
- if(m_Map.Lookup(m_pinyin,strstr2)) //输入拼音找到匹配的汉字时的处理
- { //调用Lookup()函数进行散列查找,
- //m_Input中存放匹配的拼音,找到的对应的汉字存放在str中
- strstr1=m_pinyin;
- int k=0;
- page=0; //翻页数初始化为0
- flag=1; //标记找到对应的汉字
- m_hanzi=""; //清空以前的内容
- length=strstr2.GetLength(); //得到串的长度
- for(int i=0;i<length;i++) //把找到的汉字存放到一个数组中
- {
- MyArray[k].ch[0]=strstr2[i];
- MyArray[k].ch[1]=strstr2[++i];
- k++;
- }
- DisplayHanzi(); //显示第一页的汉字
- }
- else //输入的拼音找不到匹配的汉字时的处理
- {
- strstr1=strstr2=""; //赋空
- m_hanzi="";
- flag=0; //标记找不到对应的汉字
- }
- UpdateData(FALSE);
- }
- /*************显示输入的拼音对应的汉字**************/
- void CMyDlg::DisplayHanzi()
- {
- int k=1;
- CString str1;
- m_hanzi=""; //清空以前的内容
- for(int i=page*10;i<page*10+10;i++) //将数组中的汉字保存到字符串m_hanzi中
- {
- k=k%10;
- str1.Format("%d",k); //为汉字前面加上编号
- m_hanzi+=str1;
- m_hanzi+=MyArray[i].ch[0]; //接收汉字
- m_hanzi+=MyArray[i].ch[1];
- m_hanzi+=" "; //汉字之间用空格隔开
- k++;
- if((i+1)==(length/2))
- break;
- }
- UpdateData(FALSE);
- }
- /*********************显示结果**************************/
- void CMyDlg::ShowText()
- {
- if(page*10+Num+1>length/2) //如果所选的编号没有字,则返回
- return;
- m_text+=MyArray[page*10+Num].ch[0];
- m_text+=MyArray[page*10+Num].ch[1]; //接收汉字
- m_pinyin=""; //清空提示
- m_hanzi="";
- UpdateData(FALSE);
- ChangeOrder(); //改变汉字的先后顺序
- }
- BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
- {
- // TODO: Add your specialized code here and/or call the base class
- if(pMsg->message == WM_KEYDOWN)
- {
- if(pMsg->wParam == VK_RETURN) //截取键盘中的回车键
- {
- UINT nID = GetFocus()->GetDlgCtrlID();
- switch(nID)
- {
- case IDC_INPUTPY:
- m_text+=m_pinyin; //按回车键时则输入拼音
- m_pinyin=""; //并清提示符
- m_hanzi="";
- UpdateData(FALSE);
- return TRUE;
- default:
- return FALSE;
- }
- }
- if(pMsg->wParam==VK_NEXT||pMsg->wParam==VK_ADD) //按PageDown或'+',向后翻页
- {
- UINT nID2 = GetFocus()->GetDlgCtrlID();
- switch(nID2)
- {
- case IDC_INPUTPY:
- if((flag==1)&&(m_pinyin!=""))
- {
- if((page+1)*10>length/2) //不能再翻页
- return TRUE;
- else
- {
- page++; //翻页变量加一
- DisplayHanzi();
- }
- }
- return TRUE;
- default:
- return FALSE;
- }
- }
- if(pMsg->wParam==VK_PRIOR||pMsg->wParam==VK_SUBTRACT) //按PageUp或'-',向前翻页
- {
- UINT nID2 = GetFocus()->GetDlgCtrlID();
- switch(nID2)
- {
- case IDC_INPUTPY:
- if((flag==1)&&(m_pinyin!=""))
- {
- if(page==0) //不能再翻页
- return TRUE;
- else
- {
- page--; //翻页变量减一
- DisplayHanzi();
- }
- }
- return TRUE;
- default:
- return FALSE;
- }
- }
- if( ((pMsg->wParam >= VK_NUMPAD0)&&(pMsg->wParam <= VK_NUMPAD9)) //选字的处理
- ||((pMsg->wParam >= _T('0'))&&(pMsg->wParam <= _T('9')))||(pMsg->wParam==_T(' ')))
- {
- UINT nID2 = GetFocus()->GetDlgCtrlID();
- switch(nID2)
- {
- case IDC_INPUTPY:
- UpdateData(FALSE);
- if((flag==1)&&(m_pinyin!=""))
- {
- if((pMsg->wParam>=VK_NUMPAD1)&&(pMsg->wParam<=VK_NUMPAD9))
- Num=pMsg->wParam-VK_NUMPAD1; //输入数字键时
- else if(pMsg->wParam==_T(' ')) //输入空格时
- Num=0;
- else if(pMsg->wParam==_T('0')||pMsg->wParam>=VK_NUMPAD0) //输入0时
- Num=9;
- else
- Num=pMsg->wParam-_T('1'); //输入1~9时
- ShowText(); //显示所选的字
- UpdateData(FALSE);
- }
- return TRUE;
- default:
- return FALSE;
- }
- }
- }
- return CDialog::PreTranslateMessage(pMsg);
- }
- void CMyDlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
-
- CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- /*****改变汉字次序的函数,将上次所选的汉字在下次显示中排在前面*****/
- void CMyDlg::ChangeOrder()
- {
- CString str1="";
- str1+=MyArray[page*10+Num].ch[0];
- str1+=MyArray[page*10+Num].ch[1]; //保存用户刚输入的汉字
- strstr2.Delete((page*10+Num)*2,2); //在原字符串中删除用户刚输入的汉字
- strstr2.Insert(0,str1); //把用户刚输入的汉字放在前面
- m_Map.SetAt(strstr1,strstr2); //重新修改映射中关键字对应的元素
- }
- /********************响应关于按钮***************************/
- void CMyDlg::OnAbout()
- {
- CAboutDlg Dlg;
- Dlg.DoModal();;
- }
- /*********************退出程序******************************/
- void CMyDlg::OnExit()
- {
- // TODO: Add your control notification handler code here
- CDialog::OnOK();
-
- }