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

📄 reviewdlg.cpp

📁 用VC开发的背单词小软件
💻 CPP
字号:
// ReviewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "winbdc.h"
#include "WinBDCDlg.h"
#include "ReviewDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CReviewDlg dialog


CReviewDlg::CReviewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CReviewDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CReviewDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pMainDlg = (CWinBDCDlg *)(AfxGetApp()->GetMainWnd());
	m_bIsManagement = false;
}


void CReviewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReviewDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CReviewDlg, CDialog)
	//{{AFX_MSG_MAP(CReviewDlg)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_BN_CLICKED(IDC_ADDWORD, OnAddword)
	ON_BN_CLICKED(IDC_EDITWORD, OnEditword)
	ON_BN_CLICKED(IDC_DELWORD, OnDelword)
	ON_BN_CLICKED(IDC_MODIFYOK, OnModifyok)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReviewDlg message handlers

BOOL CReviewDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	pList = (CListBox *)GetDlgItem(IDC_LIST1);

	if(m_pMainDlg->m_WordList.GetCount() == 0)
	{
		MessageBox("请打开词库文件后再作浏览!");
		EndDialog(IDOK);
		return TRUE;
	}
	else
	{		
		for(int i=0; i<m_pMainDlg->m_WordList.GetCount(); i++)
		{
			pList->AddString((LPCTSTR)m_pMainDlg->m_WordList.GetAt
				(m_pMainDlg->m_WordList.FindIndex(i))->m_English);
		}
	}

	((CListBox *)GetDlgItem(IDC_LIST1))->SetCurSel(0);
	OnSelchangeList1();

	((CEdit *)GetDlgItem(IDC_ADDWORD))->EnableWindow(m_bIsManagement);
	((CEdit *)GetDlgItem(IDC_EDITWORD))->EnableWindow(m_bIsManagement);
	((CEdit *)GetDlgItem(IDC_DELWORD))->EnableWindow(m_bIsManagement);
	((CEdit *)GetDlgItem(IDC_MODIFYOK))->EnableWindow(m_bIsManagement);

	if(m_bIsManagement)
	{
		m_FilePath = ((CWinBDCDlg *)AfxGetMainWnd())->m_FilePath;
	}
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CReviewDlg::OnSelchangeList1()
{
	// TODO: Add your control notification handler code here
	int n = pList->GetCurSel();
	CMyWord *pWord = m_pMainDlg->m_WordList.GetAt(m_pMainDlg->m_WordList.FindIndex(n));
	CEdit *pEdit = (CEdit *)GetDlgItem(IDC_EDIT1);
	CRichEditCtrl *pRichEditCtrl = (CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT1);
	pEdit->SetWindowText(pWord->m_English);
	pRichEditCtrl->SetWindowText(pWord->GetFormatChinese());
}

void CReviewDlg::OnAddword() 
{
	// TODO: Add your control notification handler code here
	((CEdit *)GetDlgItem(IDC_ADDWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_EDITWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_DELWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_MODIFYOK))->EnableWindow(true);
	((CButton *)GetDlgItem(IDOK))->EnableWindow(false);
	((CListBox *)GetDlgItem(IDC_LIST1))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_EDIT1))->SetReadOnly(false);
	((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT1))->SetReadOnly(false);
	m_nFlag = 1;
}

void CReviewDlg::OnEditword() 
{
	// TODO: Add your control notification handler code here
	((CEdit *)GetDlgItem(IDC_ADDWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_EDITWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_DELWORD))->EnableWindow(false);
	((CEdit *)GetDlgItem(IDC_MODIFYOK))->EnableWindow(true);
	((CButton *)GetDlgItem(IDOK))->EnableWindow(false);
	((CListBox *)GetDlgItem(IDC_LIST1))->EnableWindow(false);
	((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT1))->SetReadOnly(false);
	m_nFlag = 2;
}

void CReviewDlg::OnDelword() 
{
	// TODO: Add your control notification handler code here
	CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1);
	if(pListBox->GetCount() >0 )
	{
		int nIndex = pListBox->GetCurSel();
		pListBox->DeleteString(nIndex);
		m_pMainDlg->m_WordList.RemoveAt(m_pMainDlg->m_WordList.FindIndex(nIndex));
		if(nIndex < pListBox->GetCount())
		{
			pListBox->SetCurSel(nIndex);
			OnSelchangeList1();
		}
		else
			if(pListBox->GetCount() != 0)
			{
				pListBox->SetCurSel(nIndex-1);
				OnSelchangeList1();
			}
			else
			{
				SetDlgItemText(IDC_EDIT1, "");
				SetDlgItemText(IDC_RICHEDIT1, "");
			}
	}
}

void CReviewDlg::OnModifyok() 
{
	// TODO: Add your control notification handler code here
	CString str1, str2;
	GetDlgItemText(IDC_EDIT1, str1);
	GetDlgItemText(IDC_RICHEDIT1, str2);
	switch(m_nFlag)
	{
	case 1:
		if(str1.GetLength() == 0 || str2.GetLength() == 0)
		{
			MessageBox("单词和解释均不能为空,请重试!");
		}
		else
		{
			CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1);
			pListBox->AddString(str1);
			int nIndex = pListBox->FindString(0, str1);
			CMyWord *pMyWord = new CMyWord(str1, str2, false);
			if(nIndex == m_pMainDlg->m_WordList.GetCount())
				m_pMainDlg->m_WordList.InsertAfter(m_pMainDlg->m_WordList.FindIndex(nIndex), pMyWord);
			else
				m_pMainDlg->m_WordList.InsertBefore(m_pMainDlg->m_WordList.FindIndex(nIndex), pMyWord);
			pListBox->SetCurSel(nIndex);
			OnSelchangeList1();

			((CEdit *)GetDlgItem(IDC_ADDWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_EDITWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_DELWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_MODIFYOK))->EnableWindow(false);
			((CListBox *)GetDlgItem(IDC_LIST1))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_EDIT1))->SetReadOnly(true);
			((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT1))->SetReadOnly(true);
			((CButton *)GetDlgItem(IDOK))->EnableWindow(true);
		}
		break;
	case 2:
		if(str2.GetLength() == 0)
		{
			MessageBox("解释不能为空,请重试!");
		}
		else
		{
			CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1);
			int nIndex = pListBox->FindString(0, str1);
			CMyWord *pMyWord = NULL;
			pMyWord = m_pMainDlg->m_WordList.GetAt(m_pMainDlg->m_WordList.FindIndex(nIndex));
			pMyWord->m_Chinese = str2;

			((CEdit *)GetDlgItem(IDC_ADDWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_EDITWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_DELWORD))->EnableWindow(true);
			((CEdit *)GetDlgItem(IDC_MODIFYOK))->EnableWindow(false);
			((CButton *)GetDlgItem(IDOK))->EnableWindow(true);
			((CListBox *)GetDlgItem(IDC_LIST1))->EnableWindow(true);
			((CRichEditCtrl *)GetDlgItem(IDC_RICHEDIT1))->SetReadOnly(true);
		}
		break;
	}
}

void CReviewDlg::OnOK() 
{
	// TODO: Add extra validation here
	if(m_bIsManagement)
	{
		FILE *fp = fopen(m_FilePath, "w");
		CListBox *pListBox = (CListBox *)GetDlgItem(IDC_LIST1);
		int nCount = pListBox->GetCount();
		CMyWord *pMyWord;
		fprintf(fp, "%d\n", nCount);
		for(int i=0; i<nCount; i++)
		{
			pMyWord = m_pMainDlg->m_WordList.GetAt(m_pMainDlg->m_WordList.FindIndex(i));
			fprintf(fp, "%s\n%s", pMyWord->m_English, pMyWord->m_Chinese);
			if(i+1 < nCount)
				fprintf(fp, "\n");
		}
		fclose(fp);

		CString str;
		str.Format("%d", pListBox->GetCount());
		((CStatic *)m_pMainDlg->GetDlgItem(IDC_STATIC1))->SetWindowText(str);
	}
	CDialog::OnOK();
}

⌨️ 快捷键说明

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