📄 hzxmlapp.cpp
字号:
// HzXMLApp.cpp : Defines the class behaviors for the application.
//
// XML文档存取类库与编辑器-数据结构大作业
// 华南理工大学-计算机03(1)班-肖天华
// 200345003136 65#
// http://xth.blogone.net
// QQ:4023727 萧萧
#include "stdafx.h"
#include "HzXMLApp.h"
#include "MainFrm.h"
#include "ChildFrm.h"
#include "HzXMLDoc.h"
#include "HzXMLView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHzXMLApp
BEGIN_MESSAGE_MAP(CHzXMLApp, CWinApp)
//{{AFX_MSG_MAP(CHzXMLApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_APP_TEST, OnAppTest)
//}}AFX_MSG_MAP
// Standard file based document commands
ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
// Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHzXMLApp construction
CHzXMLApp::CHzXMLApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CHzXMLApp object
CHzXMLApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CHzXMLApp initialization
BOOL CHzXMLApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Set app title
CString csVersion, csV0, csV1;
csVersion.LoadString( ID_APP_VERSION );
AfxExtractSubString( csV0, csVersion, 0, ',' );
AfxExtractSubString( csV1, csVersion, 1, ',' );
CString csBuild;
#if defined( _DEBUG )
csBuild += _T(" Debug");
#endif
#if defined( _UNICODE )
csBuild += _T(" Unicode");
#endif
m_csTitle.Format( _T("XML文档存取类库与编辑器-数据结构大作业^_^ %s"), csBuild );
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_HzXMLTYPE,
RUNTIME_CLASS(CHzXMLDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CHzXMLView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
return FALSE;
// TestHzXML();
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}
void CHzXMLApp::CreateNewDoc( CString csText, CString csTitle )
{
// This routine is used to open a document with the given text
POSITION pos = m_pDocManager->GetFirstDocTemplatePosition();
CDocTemplate* pTemplate = m_pDocManager->GetNextDocTemplate(pos);
ASSERT(pTemplate != NULL);
ASSERT_KINDOF(CDocTemplate, pTemplate);
CHzXMLDoc* pDoc = (CHzXMLDoc*)pTemplate->OpenDocumentFile(NULL);
pDoc->m_csText = csText;
pDoc->m_doc.SetDoc( csText );
pDoc->SetTitle( csTitle );
pDoc->UpdateAllViews( NULL, 1 );
pDoc->SetModifiedFlag( FALSE );
}
/////////////////////////////////////////////////////////////////////////////
// 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)
virtual BOOL OnInitDialog();
//}}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)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Display build in title bar of About Box
CString csTitle = _T("About ") + ((CHzXMLApp*)AfxGetApp())->m_csTitle;
SetWindowText(csTitle);
// Display full version
CString csVersion;
csVersion.LoadString( ID_APP_VERSION );
GetDlgItem( IDC_STATIC_VERSION )->SetWindowText( csVersion );
return TRUE;
}
// App command to run the dialog
void CHzXMLApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CHzXML Test
#include "HzXML.h"
void CHzXMLApp::OnAppTest()
{
// “文件->测试” 菜单调用
// 向主窗口发送层叠文档窗口消息(ID_WINDOW_CASCADE)
m_pMainWnd->SendMessage( WM_COMMAND, ID_WINDOW_CASCADE, 0 );
//
// CHzXML类封装了关于XML文件的所有操作
// 以下代码调用了CHzXML类的部分函数
//
CHzXML xml;
xml.AddElem( _T("作者") ); //添加项目
xml.AddChildElem( _T("学籍信息") ); //添加子项
xml.IntoElem(); //进入下一级项目,原来的ChildElem相当于现在的Elem
xml.AddChildElem( _T("姓名"), _T("肖天华") );
xml.AddChildElem( _T("学号"), _T("200345003136") );
xml.AddChildElem( _T("班级"), _T("计算机03(1)班") );
xml.AddChildElem( _T("职务"), _T("学习委员") );
xml.AddElem( _T("联系方式") );
xml.AddAttrib( _T("方式"),_T("网络") ); //添加属性
xml.AddChildElem( _T("QQ"), _T("4023727") );
xml.AddChildElem( _T("MSN"), _T("xth21@msn.com") );
xml.AddChildElem( _T("UC"), _T("642414") );
xml.AddChildElem( _T("ICQ"), _T("177128766") );
CreateNewDoc( xml.GetDoc(), _T("关于作者华仔") );
// 深度优先搜索
// 执行后正确的csListOfTagNames的值应该为:"ABCCCDB"
xml.SetDoc( _T("<A><B><C/><C/><C><D/></C></B><B/></A>") );
CString csListOfTagNames;
BOOL bFinished = FALSE;
if ( xml.FindElem() )
csListOfTagNames = xml.GetTagName();
if ( ! xml.FindChildElem() )
bFinished = TRUE;
while ( ! bFinished )
{
// 进入项目
xml.IntoElem();
csListOfTagNames += xml.GetTagName();
// 下一项 (深度优先)
BOOL bFound = xml.FindChildElem();
while ( ! bFound && ! bFinished )
{
if ( xml.OutOfElem() )
bFound = xml.FindChildElem();
else
bFinished = TRUE;
}
}
//AfxMessageBox( csListOfTagNames );
//将上行的注释符去掉后,可看到csListOfTagNames的值是正确的
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -