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

📄 edittextdlg.cpp

📁 小型中英文打字练习器
💻 CPP
字号:
// EditTextDlg.cpp : implementation file

#include "stdafx.h"
#include "EditText.h"
#include "EditTextDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CEditTextDlg dialog

CEditTextDlg::CEditTextDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CEditTextDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditTextDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CEditTextDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditTextDlg)
	DDX_Control(pDX, IDC_CHARNUM, m_CharNum);
	DDX_Control(pDX, IDC_CURLINE, m_CurLine);
	DDX_Control(pDX, IDC_TOPLINE, m_TopLine);
	DDX_Control(pDX, IDC_LINENUM, m_LineNum);
	DDX_Control(pDX, IDC_SINGLEEDIT, m_SEdit);
	DDX_Control(pDX, IDC_MULTILINEEDIT, m_MEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEditTextDlg, CDialog)
	//{{AFX_MSG_MAP(CEditTextDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CLEARBUTTON, OnClearbutton)
	ON_BN_CLICKED(IDC_UNDOBUTTON, OnUndobutton)
	ON_EN_SETFOCUS(IDC_SINGLEEDIT, OnSetfocusSingleedit)
	ON_EN_SETFOCUS(IDC_MULTILINEEDIT, OnSetfocusMultilineedit)
	ON_BN_CLICKED(IDC_COPYBUTTON, OnCopybutton)
	ON_BN_CLICKED(IDC_CUTBUTTON, OnCutbutton)
	ON_BN_CLICKED(IDC_PASTEBUTTON, OnPastebutton)
	ON_EN_CHANGE(IDC_MULTILINEEDIT, OnChangeMultilineedit)
	ON_BN_CLICKED(IDC_EXITBUTTON, OnExitbutton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditTextDlg message handlers

BOOL CEditTextDlg::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
	bFocusMEdit=FALSE;//多行文本框获得焦点初值
	bFocusSEdit=FALSE;//单行文本框获得焦点初值

   char chTemp[5];
   m_MEdit.GetWindowText(szAllText);
   nCharNum=0;
   nLineNum=0;
   nCurLine=0;
   nTopLine=0;
   itoa(nCharNum,chTemp,10);
   m_CharNum.SetWindowText(chTemp);
   itoa(nLineNum,chTemp,10);
   m_LineNum.SetWindowText(chTemp);
   itoa(nTopLine,chTemp,10);
   m_TopLine.SetWindowText(chTemp);
   itoa(nCurLine,chTemp,10);
   m_CurLine.SetWindowText(chTemp);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CEditTextDlg::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 CEditTextDlg::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();
	}
}

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

void CEditTextDlg::OnClearbutton() //全清处理
{
	// TODO: Add your control notification handler code here
    m_MEdit.SetSel(0,-1);//选中全部文本
	m_MEdit.ReplaceSel("");//用""替换选中文本
	m_SEdit.SetSel(0,-1);
	m_SEdit.ReplaceSel("");
	m_CharNum.SetWindowText("0");
	m_LineNum.SetWindowText("0");
	m_CurLine.SetWindowText("0");
	m_TopLine.SetWindowText("0");
		
}

void CEditTextDlg::OnUndobutton() //撤消处理
{
	// TODO: Add your control notification handler code here
	if(m_SEdit.CanUndo())
	{
		m_SEdit.Undo();
	}
	else
	{
		MessageBox("单行文本框不能撤消","Warning",MB_OK);
	}
	if(m_MEdit.CanUndo())
	{
		m_MEdit.Undo();
	}
	else
	{
		MessageBox("多行文本框不能撤消","Warning",MB_OK);
	}
}

void CEditTextDlg::OnSetfocusSingleedit()//单行文本框获得焦点 
{
	// TODO: Add your control notification handler code here
	bFocusMEdit=FALSE;
	bFocusSEdit=TRUE;
}

void CEditTextDlg::OnSetfocusMultilineedit() //多行文本框获得焦点
{
	// TODO: Add your control notification handler code here
    bFocusMEdit=TRUE;
	bFocusSEdit=FALSE;	
}

void CEditTextDlg::OnCopybutton()//复制处理 
{
	// TODO: Add your control notification handler code here
	if(bFocusMEdit==TRUE)
	{
		m_MEdit.Copy();
	}
	if(bFocusSEdit==TRUE)
	{
		m_SEdit.Copy();
	}
}

void CEditTextDlg::OnCutbutton() //剪切处理
{
	// TODO: Add your control notification handler code here
    if(bFocusMEdit==TRUE)
	{
		m_MEdit.Cut();
	}
	if(bFocusSEdit==TRUE)
	{
		m_SEdit.Cut();
	}	
}

void CEditTextDlg::OnPastebutton()//粘贴处理 
{
	// TODO: Add your control notification handler code here
   if(bFocusMEdit==TRUE)
	{
		m_MEdit.Paste();
	}
	if(bFocusSEdit==TRUE)
	{
		m_SEdit.Paste();
	}	
}

void CEditTextDlg::OnChangeMultilineedit()//多行文本框改变处理 
{
	// 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
   
	//计算并显示多行文本框数据
   m_MEdit.GetWindowText(szAllText);
   nCharNum=szAllText.GetLength();
   nLineNum=m_MEdit.GetLineCount();
   nCurLine=m_MEdit.LineFromChar()+1;
   nTopLine=m_MEdit.GetFirstVisibleLine()+1;
   itoa(nCharNum,chTemp,10);
   m_CharNum.SetWindowText(chTemp);
   itoa(nLineNum,chTemp,10);
   m_LineNum.SetWindowText(chTemp);
   itoa(nTopLine,chTemp,10);
   m_TopLine.SetWindowText(chTemp);
   itoa(nCurLine,chTemp,10);
   m_CurLine.SetWindowText(chTemp);

}

void CEditTextDlg::OnExitbutton() //退出
{
	// TODO: Add your control notification handler code here
  OnOK();	
}

⌨️ 快捷键说明

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