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

📄 qimwindowdlg.cpp

📁 我们一个小组
💻 CPP
字号:
// QimWindowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "QimWindow.h"
#include "QimWindowDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

const int MAX_RESULT_PER_PAGE = 5 ;

const int STATUS_INPUT_PY = 0 ;
const int STATUS_CHOOSE_PHRASE = 1;

const int INPUT_NEED_UPDATE = 0 ;
const int INPUT_NOT_NEED_UPDATE = 1 ;


// CAboutDlg dialog used for App About

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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

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

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

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CQimWindowDlg dialog




CQimWindowDlg::CQimWindowDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQimWindowDlg::IDD, pParent), m_nCurrentDisplayPage(1), m_nCurrentStatus(STATUS_INPUT_PY), m_nCurrentHandledPos(0), m_bInputNeedUpdate(INPUT_NEED_UPDATE)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CQimWindowDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CQimWindowDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_EN_CHANGE(IDC_EDIT_PINYIN, &CQimWindowDlg::OnInput)
	ON_WM_DESTROY()
END_MESSAGE_MAP()


// CQimWindowDlg message handlers

BOOL CQimWindowDlg::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

	if ( !m_Qim.Load(".\\dic\\static_table.txt", ".\\dic\\dynamic_table.txt", ".\\dic\\hash_table.txt") )
	{
		MessageBox("Load dictionary error!") ;
		return FALSE ;
	}

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CQimWindowDlg::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 CQimWindowDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CQimWindowDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CQimWindowDlg::OnInput()
{
	if (m_bInputNeedUpdate == INPUT_NOT_NEED_UPDATE)
	{
		m_bInputNeedUpdate = INPUT_NEED_UPDATE ;
		return ;
	}

	CString strPy; 
	CWnd* pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
	pWnd->GetWindowText(strPy) ;
	int nLen=pWnd->GetWindowTextLength();   
	((CEdit*)pWnd)->SetSel(nLen, nLen) ;

	if (strPy.GetLength() == 0)
	{
		CWnd* pWnd = GetDlgItem(IDC_EDIT_PHRASE);
		pWnd->SetWindowText("") ;
		return ;
	}

	//in case that the input is a digit from the beginning
	/*if( isdigit(strPy.GetAt(0)) )
	{
		return ;
	}*/

	m_vecResultWord.clear() ;
	

	if (m_nCurrentStatus == STATUS_INPUT_PY)
	{
		m_strCurrentPy.assign( (LPCTSTR)strPy) ;

		m_nCurrentDisplayPage = 1 ;
		string strInput ((LPCTSTR)strPy) ;

		vector<string> vecDynamicResult ;
		int nDynamicLongestMatchLength ;
		if ( ( nDynamicLongestMatchLength = m_Qim.GetDynamicPraseByLongestMatchedPy(strInput, vecDynamicResult) ) > 0)
		{
			for (size_t i = 0; i<vecDynamicResult.size(); i++)
			{
				m_vecResultWord.push_back(vecDynamicResult[i]) ;
			}
		}

		//add static phrase
		vector<string> vecStaticResult;
		int nStaticLongestMatchLength ;
		if ( ( nStaticLongestMatchLength = m_Qim.GetStaticWordByLongestMatchedPy(strInput, vecStaticResult)) > 0)
		{
			for (size_t i = 0; i<vecStaticResult.size(); i++)
			{
				m_vecResultWord.push_back(vecStaticResult[i]) ;
			}
		}

		OnDisplayPhrase(m_nCurrentDisplayPage);
	}
	else if (m_nCurrentStatus == STATUS_CHOOSE_PHRASE)
	{
		string strUnhandledPy = m_strCurrentPy.substr(m_nCurrentHandledPos) ;

		if (strUnhandledPy.size() == 0)
		{
			return ;
		}

		char c = strPy.GetAt(strPy.GetLength()-1) ;

		if( c < '0' || c> '9' )
		{
			m_nCurrentHandledPos = 0;
			m_strCurrentPhrase = "";
			m_nCurrentDisplayPage = 1; 
			CWnd* pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
			m_nCurrentStatus = STATUS_INPUT_PY ;
			m_strCurrentPy += c ;
			pWnd->SetWindowText(m_strCurrentPy.c_str()) ;

			return ;
		}

		vector<string> vecDynamicResult ;
		int nDynamicLongestMatchLength ;
		if ( ( nDynamicLongestMatchLength = m_Qim.GetDynamicPraseByLongestMatchedPy(strUnhandledPy, vecDynamicResult) ) > 0)
		{
			for (size_t i = 0; i<vecDynamicResult.size(); i++)
			{
				m_vecResultWord.push_back(vecDynamicResult[i]) ;
			}
		}

		//add static phrase
		vector<string> vecStaticResult;
		int nStaticLongestMatchLength ;
		if ( ( nStaticLongestMatchLength = m_Qim.GetStaticWordByLongestMatchedPy(strUnhandledPy, vecStaticResult)) > 0)
		{
			for (size_t i = 0; i<vecStaticResult.size(); i++)
			{
				m_vecResultWord.push_back(vecStaticResult[i]) ;
			}
		}

		int choice = static_cast<int>( strPy.GetAt(strPy.GetLength()-1) - '0' );
		choice = (m_nCurrentDisplayPage-1)*MAX_RESULT_PER_PAGE + choice ;
		

		//move to next possible py
		if (choice <= vecDynamicResult.size())
		{
			m_strCurrentSlimcutPy += m_strCurrentPy.substr(m_nCurrentHandledPos, nDynamicLongestMatchLength) ;
			m_nCurrentHandledPos += nDynamicLongestMatchLength ;
			
			
		}
		else
		{
			m_strCurrentSlimcutPy += m_strCurrentPy.substr(m_nCurrentHandledPos, 1) ;
			m_nCurrentHandledPos += nStaticLongestMatchLength ;
			
		}

		strPy = m_strCurrentPy.substr(m_nCurrentHandledPos).c_str() ;

		m_strCurrentPhrase += m_vecResultWord[choice-1].c_str() ;
		strPy = m_strCurrentPhrase.c_str() + strPy ;

		pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
		m_bInputNeedUpdate = INPUT_NOT_NEED_UPDATE ;
		pWnd->SetWindowText(strPy) ;
		int   nLen=pWnd->GetWindowTextLength();   
		((CEdit*)pWnd)->SetSel(nLen,nLen);
		m_nCurrentDisplayPage = 1 ;

		m_vecResultWord.clear() ;

		strUnhandledPy = m_strCurrentPy.substr(m_nCurrentHandledPos) ;
		vecDynamicResult.clear() ;
		if ( ( nDynamicLongestMatchLength = m_Qim.GetDynamicPraseByLongestMatchedPy(strUnhandledPy, vecDynamicResult) ) > 0)
		{
			for (size_t i = 0; i<vecDynamicResult.size(); i++)
			{
				m_vecResultWord.push_back(vecDynamicResult[i]) ;
			}
		}

		//add static phrase
		vecStaticResult.clear();
		if ( ( nStaticLongestMatchLength = m_Qim.GetStaticWordByLongestMatchedPy(strUnhandledPy, vecStaticResult)) > 0)
		{
			for (size_t i = 0; i<vecStaticResult.size(); i++)
			{
				m_vecResultWord.push_back(vecStaticResult[i]) ;
			}
		}

		if (m_nCurrentHandledPos == m_strCurrentPy.size()||m_vecResultWord.size() == 0)
		{
			m_nCurrentHandledPos = 0 ;
			
			m_bInputNeedUpdate = INPUT_NOT_NEED_UPDATE ;
			m_nCurrentStatus = STATUS_INPUT_PY ;
			pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
			pWnd->SetWindowText("") ;
			pWnd = GetDlgItem(IDC_EDIT_PHRASE) ;
			pWnd->SetWindowText("") ;

			CString strText ;
			pWnd = GetDlgItem(IDC_EDIT_TEXT) ;
			pWnd->GetWindowText(strText) ;
			strText += m_strCurrentPhrase.c_str() ;
			pWnd->SetWindowText(strText) ;

			//MessageBox( (m_strCurrentPy +" "+ m_strCurrentSlimcutPy +" "+ m_strCurrentPhrase).c_str() ) ;
			m_Qim.InsertDynamicPhrase1(m_strCurrentPy, m_strCurrentSlimcutPy, m_strCurrentPhrase, 1.5) ;

			m_strCurrentPy = "" ;
			m_strCurrentSlimcutPy = "" ;
			m_strCurrentPhrase = "" ;
		}
		else
		{
			OnDisplayPhrase(m_nCurrentDisplayPage);
		}		
	}
	
}

int CQimWindowDlg::OnDisplayPhrase(int nPage)
{
	if ( ((nPage-1)*MAX_RESULT_PER_PAGE+1) < 0 )
	{
		return nPage+1;
	}
	
	
	if ( ( (nPage-1)*MAX_RESULT_PER_PAGE+1 ) > static_cast<int>(m_vecResultWord.size()) )
	{
		return nPage-1 ;
	}

	CString strResult ;

	if (nPage*MAX_RESULT_PER_PAGE >= static_cast<int>(m_vecResultWord.size()))
	{
		// from (nPage-1)*MAX_RESULT_PER_PAGE  to  m_vecResultWord.size()
		int index=1 ;
		for ( size_t i=(nPage-1)*MAX_RESULT_PER_PAGE; i<m_vecResultWord.size(); i++, index++)
		{
			CString temp ;

			temp.Format("%d.", index) ;
			temp += ( m_vecResultWord[i] + "\t").c_str() ;

			strResult += temp ;
		}
	}

	else 
	{
		//from (nPage-1)*MAX_RESULT_PER_PAGE, display 5 result
		int index=1 ;
		for ( size_t i= (nPage-1)*MAX_RESULT_PER_PAGE; index <6; i++, index++)
		{
			CString temp ;

			temp.Format("%d.", index) ;
			temp += ( m_vecResultWord[i] + "\t").c_str() ;

			strResult += temp ;
		}
	}

	CWnd* pWnd = GetDlgItem(IDC_EDIT_PHRASE);
	pWnd->SetWindowText(strResult) ;

	return nPage;

}


BOOL CQimWindowDlg::PreTranslateMessage(MSG* pMsg)
{
	HWND hwnd; 
	GetDlgItem(IDC_EDIT_PINYIN,&hwnd); 

	if( pMsg->message == WM_KEYDOWN && pMsg->hwnd == hwnd)
	{ 
		WORD low = LOWORD(pMsg->lParam) ;
		WORD high = HIWORD(pMsg->lParam);
		if (pMsg->lParam & 0x01000000)
		{
			
			int a=0 ;
			a++ ;
		}
		if (pMsg->wParam >= 'A' && pMsg->wParam <= 'Z') 
		{
			return CDialog::PreTranslateMessage(pMsg); 
		}
		else if (pMsg->wParam == VK_PRIOR)
		{
			m_nCurrentDisplayPage--;
			m_nCurrentDisplayPage = OnDisplayPhrase(m_nCurrentDisplayPage);
		}
		else if (pMsg->wParam == VK_NEXT )
		{
			m_nCurrentDisplayPage++;
			m_nCurrentDisplayPage = OnDisplayPhrase(m_nCurrentDisplayPage);
		}
		else if (pMsg->wParam >= '0' && pMsg->wParam <='9')
		{
			CString strPy; 
			CWnd* pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
			pWnd->GetWindowText(strPy) ;

			if (m_nCurrentStatus == STATUS_INPUT_PY && strPy.GetLength()==0 )	//input numbers
			{
				CString strText ;
				pWnd = GetDlgItem(IDC_EDIT_TEXT) ;
				pWnd->GetWindowText(strText) ;
				CString strInput ;
				strInput.Format("%c", pMsg->wParam) ;

				pWnd->SetWindowText(strText+strInput) ;

				return TRUE;
			}
			else if (m_nCurrentStatus == STATUS_INPUT_PY && strPy.GetLength()>0 )	//the first time to choose 
			{
				m_nCurrentStatus = STATUS_CHOOSE_PHRASE ;

				
				m_nCurrentHandledPos = 0 ;
				m_vecResultWord.clear() ;
			}
		}
		else if ( pMsg->wParam == VK_BACK)
		{
			if (m_nCurrentStatus == STATUS_CHOOSE_PHRASE)
			{
				m_nCurrentHandledPos = 0;
				m_strCurrentPhrase = "";
				m_nCurrentDisplayPage = 1; 
				CWnd* pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
				m_nCurrentStatus = STATUS_INPUT_PY ;
				pWnd->SetWindowText(m_strCurrentPy.c_str()) ;
				return TRUE ;
			}
		}
		else if ( pMsg->wParam == VK_RETURN )
		{
			CString strText ;
			CWnd* pWnd = GetDlgItem(IDC_EDIT_TEXT) ;
			pWnd->GetWindowText(strText) ;
			pWnd->SetWindowText(strText + m_strCurrentPy.c_str()) ;

			m_strCurrentPy = "" ;
			m_nCurrentHandledPos = 0;
			m_strCurrentPhrase = "";
			m_nCurrentDisplayPage = 1; 
			m_nCurrentStatus = STATUS_INPUT_PY ;

			pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
			pWnd->SetWindowText("");
			pWnd = GetDlgItem(IDC_EDIT_PHRASE) ;
			pWnd->SetWindowText("");

			return TRUE ;
		}
		else if ( pMsg->wParam == VK_SPACE)
		{
			CString strPy; 
			CWnd* pWnd = GetDlgItem(IDC_EDIT_PINYIN) ;
			pWnd->GetWindowText(strPy) ;

			/*if (strPy.GetLength()>0)	//
			{
				m_nCurrentStatus = STATUS_CHOOSE_PHRASE ;
				strPy += "1" ;
				pWnd->SetWindowText(strPy) ;
			}*/
			return TRUE ;
		}
		else
		{
			return TRUE ;

		}
	
		//return TRUE; 
		return CDialog::PreTranslateMessage(pMsg);
	} 

	return CDialog::PreTranslateMessage(pMsg);
}

void CQimWindowDlg::OnDestroy()
{
	CDialog::OnDestroy();

	m_Qim.Save(".\\dic\\static_table.txt", ".\\dic\\dynamic_table.txt", ".\\dic\\hash_table.txt");
}

⌨️ 快捷键说明

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