kechengdlg.cpp

来自「数据结构课程设计 图形界面 数据结构课程设计 图形界面 文本编辑器用MFC做」· C++ 代码 · 共 532 行

CPP
532
字号
// kechengDlg.cpp : implementation file
//

#include "stdafx.h"
#include "kecheng.h"
#include "kechengDlg.h"
#include "textedit.h"
#include"c4_2.h"

//#include "ToopTipHelp.h"
#include"openDlg.h"
#include "resource.h"
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CKechengDlg dialog

CKechengDlg::CKechengDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CKechengDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CKechengDlg)
	m_mainstr = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
}

void CKechengDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CKechengDlg)
	DDX_Text(pDX, IDC_EDIT1, m_mainstr);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CKechengDlg, CDialog)
	ON_WM_CONTEXTMENU()
	//{{AFX_MSG_MAP(CKechengDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_BN_CLICKED(IDC_LIST, OnList)
	ON_BN_CLICKED(IDC_INSERT, OnInsert)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_COPY, OnCopy)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	ON_BN_CLICKED(IDC_SEARCH, OnSearch)
	ON_BN_CLICKED(IDC_REPLACE, OnReplace)
	ON_BN_CLICKED(IDC_SAVE, OnSave)
	ON_COMMAND(IDD_ABOUTBOX, OnAboutbox)
	ON_COMMAND(IDD_COPY, OnCopyM)
	ON_COMMAND(IDD_DELETE, OnDeleteM)
	ON_COMMAND(IDD_EXIT, OnExitM)
	ON_COMMAND(IDD_INSERT, OnInsertM)
	ON_COMMAND(IDD_LIST, OnListM)
	ON_COMMAND(IDD_MODIFY, OnModifyM)
	ON_COMMAND(IDD_OPEN, OnOpenM)
	ON_COMMAND(IDD_REPLACE, OnReplaceM)
	ON_COMMAND(IDD_SAVE, OnSaveM)
	ON_COMMAND(IDD_SEARCH, OnSearchM)
	ON_COMMAND(IDDINSERT, OnDinsert)
	ON_COMMAND(IDDDELETE, OnDdeleteD)
	ON_COMMAND(ID_COLOR_BACKCOLOR, OnColorBackcolor)
	ON_COMMAND(ID_COLOR_FORECOLOR, OnColorForecolor)
	ON_BN_CLICKED(IDC_COUNT, OnCount)
	ON_COMMAND(ID_VIEW_TEST, OnViewTest)
	ON_COMMAND(ID_TIME, OnTime)
	ON_COMMAND(ID_COUNT, OnCountM)
	ON_COMMAND(ID_GAME1, OnGame1)
	ON_COMMAND(ID_GAME2, OnGame2)
	ON_COMMAND(ID_PAINT, OnPainthuitu)
	ON_COMMAND(ID_TIMET, OnTimet)
	ON_COMMAND(ID_TXT, OnTxt)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CKechengDlg message handlers
BOOL CKechengDlg::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
	m_tooltips.Create(this);
	//m_tooltips.SetWidth(200);
	//m_tooltips.SetBkColor(RGB(213,253,224));
	//m_tooltips.SetFrameColor(RGB(0,106,53));
	m_tooltips.ADDTool(IDC_EDIT1,"显示打开的文件\n先打开文件然后,显示文件内容\n");
	m_tooltips.ADDTool(IDC_OPEN,"打开文件");
	m_tooltips.ADDTool(IDC_LIST,"显示文件内容");
	m_tooltips.ADDTool(IDC_INSERT,"插入行");
	m_tooltips.ADDTool(IDC_DELETE,"删除行数据");
	m_tooltips.ADDTool(IDC_COUNT,"统计中英文空格和其它字符个数");
	m_tooltips.ADDTool(IDC_MODIFY,"修改行");
	m_tooltips.ADDTool(IDC_SEARCH,"查找字符串");
	m_tooltips.ADDTool(IDC_REPLACE,"替换字符串");
	m_tooltips.ADDTool(IDC_SAVE,"保存文件到指定位置");
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CKechengDlg::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 CKechengDlg::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 CKechengDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CKechengDlg::OnOpen() 
{
	// TODO: Add your control notification handler code here
	Open();
}

void CKechengDlg::OnList() 
{
	// TODO: Add your control notification handler code here
	char strT[32678];
	strcpy(strT,List());
	CString str;
	str.Format("%s ",strT);
	m_mainstr=str;
	UpdateData(FALSE);
}

void CKechengDlg::OnInsert() 
{
	// TODO: Add your control notification handler code here
	Insert();OnList() ;
}

void CKechengDlg::OnDelete() 
{
	// TODO: Add your control notification handler code here
	Delete();OnList(); 
}

void CKechengDlg::OnCopy() 
{
	// TODO: Add your control notification handler code here
	Copy();OnList() ;
}

void CKechengDlg::OnModify() 
{
	// TODO: Add your control notification handler code here
	Modify();	OnList() ;
}

void CKechengDlg::OnSearch() 
{
	// TODO: Add your control notification handler code here
	Search();OnList() ;
}

void CKechengDlg::OnReplace() 
{
	// TODO: Add your control notification handler code here
	Replace();OnList() ;
}

void CKechengDlg::OnSave() 
{
	// TODO: Add your control notification handler code here
	Save();
}
//////////////////////////////////////////////////////////////////////////////////////////////
void CKechengDlg::OnAboutbox() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();	
}

void CKechengDlg::OnCopyM() 
{
	// TODO: Add your command handler code here
	OnCopy();	
}

void CKechengDlg::OnDeleteM() 
{
	// TODO: Add your command handler code here
	OnDelete();	
}

void CKechengDlg::OnExitM() 
{
	// TODO: Add your command handler code here
	CDialog::OnOK();
}

void CKechengDlg::OnInsertM() 
{
	// TODO: Add your command handler code here
	OnInsert();	
}

void CKechengDlg::OnListM() 
{
	// TODO: Add your command handler code here
    OnList();	
}

void CKechengDlg::OnModifyM() 
{
	// TODO: Add your command handler code here
	OnModify();
}

void CKechengDlg::OnOpenM() 
{
	// TODO: Add your command handler code here
	OnOpen();	
}

void CKechengDlg::OnReplaceM() 
{
	// TODO: Add your command handler code here
	OnReplace();	
}

void CKechengDlg::OnSaveM() 
{
	// TODO: Add your command handler code here
		Save();
}

void CKechengDlg::OnSearchM() 
{
	// TODO: Add your command handler code here
	OnSearch();	
}

void CKechengDlg::OnContextMenu(CWnd*, CPoint point)
{
	// CG: This block was added by the Pop-up Menu component	{		if (point.x == -1 && point.y == -1){			//keystroke invocation			CRect rect;			GetClientRect(rect);			ClientToScreen(rect);			point = rect.TopLeft();			point.Offset(5, 5);		}		CMenu menu;		VERIFY(menu.LoadMenu(CG_IDR_POPUP_KECHENG_DLG));		CMenu* pPopup = menu.GetSubMenu(0);		ASSERT(pPopup != NULL);		CWnd* pWndPopupOwner = this;		while (pWndPopupOwner->GetStyle() & WS_CHILD)			pWndPopupOwner = pWndPopupOwner->GetParent();		pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,			pWndPopupOwner);	}
}

void CKechengDlg::OnDinsert() 
{
	// TODO: Add your command handler code here
	OnInsert();	
}

void CKechengDlg::OnDdeleteD() 
{
	// TODO: Add your command handler code here
	OnDelete();	
}

void CKechengDlg::OnColorBackcolor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	if(dlg.DoModal())
	{
	}
}

void CKechengDlg::OnColorForecolor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	if(dlg.DoModal())
	{
	}	
}

void CKechengDlg::OnCount() 
{
	// TODO: Add your control notification handler code here
	int lenth=m_mainstr.GetLength();//获得字符串长度
	int nEnglish=0;
	int nChinese=0;
	int nDigit=0;
	int nOther=0;
	int nSpace=0;
	int nTotal=0;
	char charch[4000];
	strcpy(charch,m_mainstr.GetBuffer(0));
	for(int i=0;i<lenth;i++)
	{
		char c=m_mainstr.GetAt(i);
		if(c<0||c>255)//是中文字符
			//nChinese++;
			continue;
		char ch=charch[i];
		if(isalpha(ch))//是英文字符
			nEnglish++;
		else if(isdigit(ch))//是数字字符
			nDigit++;
		else if(isspace(ch))//是空格字符
			nSpace++;
		else
			nOther++;
	
	}
	nChinese=(lenth-nEnglish-nDigit-nSpace-nOther)/2;
	nTotal=nChinese+nEnglish+nDigit+nSpace+nOther;
	CString strTotal,strChinese,strEnglish,strDigit,strSpace,strOther;
	strTotal.Format("总共字符数:%d\n",nTotal);
	strChinese.Format("中文字符数:%d\n",nChinese);
	strEnglish.Format("英文字符数:%d\n",nEnglish);
	strDigit.Format("数字字符数:%d\n",nDigit);
	strOther.Format("其它字符数:%d\n",nOther);
	strSpace.Format("空格字符数:%d\n",nSpace);
	MessageBox(strTotal+strChinese+strEnglish+strDigit+strSpace+strOther,"统计字数...",MB_OK|MB_ICONINFORMATION);
}

void CKechengDlg::OnViewTest() //保存文件
{
	// TODO: Add your command handler code here
	int lenth=m_mainstr.GetLength();//获得字符串长度
	char charch[2768];
    CFileException fe;
    BOOL bFileIsOpen = FALSE;
	CFile destFile;

	char szFilter[]="ALL Files(*.*)\0*.*\0Test Files(*.txt)\0*.txt\0Test Files(*.dat)\0*.dat\0\0";
	CString m_namee,msg="文件保存在:";
	CFileDialog dlg(false);//显示对话框 
	dlg.m_ofn.lpstrFilter=(LPSTR)szFilter;//初始化对话框
	CopenDlg opendlg;
	strcpy(dlg.m_ofn.lpstrFile,opendlg.m_filename);//保存文件时的默认文件名
	if(dlg.DoModal()==IDOK)
	{
		msg+=dlg.GetPathName();
		m_namee=dlg.GetPathName();
		MessageBox(msg);
	}
	else goto end;

	//用从文件保存对话框中得到的文件名在指定位置创建文件

    if( !( bFileIsOpen = destFile.Open( m_namee, CFile::modeCreate | 
        CFile::modeWrite | CFile::typeBinary, &fe ) ) )
    {
        TCHAR strCause[256];
        fe.GetErrorMessage( strCause, 255 );
		
		CString strerror1;
		strerror1.Format("保存文件时出现了错误!!详细错误:File name = %s,Cause = %s,m_cause = %d,m_IOsError = %d ",
			fe.m_strFileName, strCause, fe.m_cause, fe.m_lOsError );
		MessageBox(strerror1,"错误",MB_OK);	
		//exit(0);//防止出现错误的,可略去
    }

	strcpy(charch,m_mainstr.GetBuffer(0));
	destFile.Write(charch,lenth);
	destFile.Close();
end:;
}


void CKechengDlg::OnTime() 
{
	// TODO: Add your command handler code here
	CTime time =CTime::GetCurrentTime();
	CString sTime=time.Format("%Y-%m-%d.%H:%M %P");
	MessageBox("It's time "+sTime);
}

void CKechengDlg::OnCountM() 
{
	// TODO: Add your command handler code here
	OnCount() ;
}

void CKechengDlg::OnGame1() 
{
	// TODO: Add your command handler code here
	WinExec("sol.exe",SW_SHOW);	
}

void CKechengDlg::OnGame2() 
{
	// TODO: Add your command handler code here
		WinExec("winmine.exe",SW_SHOW);
}

void CKechengDlg::OnPainthuitu() 
{
	// TODO: Add your command handler code here
		WinExec("mspaint.exe",SW_SHOW);
}

void CKechengDlg::OnTimet() 
{
	// TODO: Add your command handler code here
	OnTime() ;
}

void CKechengDlg::OnTxt() 
{
	// TODO: Add your command handler code here
	WinExec("notepad.exe",SW_SHOW);
}

⌨️ 快捷键说明

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