XML_TestDlg.cpp
资源名称:VC++XML.rar [点击查看]
上传用户:testhan
上传日期:2013-10-13
资源大小:49k
文件大小:5k
源码类别:
xml/soap/webservice
开发平台:
Visual C++
- // XML_TestDlg.cpp : 实现文件
- //
- #include "stdafx.h"
- #include "XML_Test.h"
- #include "XML_TestDlg.h"
- #include "msxml.h"
- #include ".xml_testdlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
- class CAboutDlg : public CDialog
- {
- public:
- CAboutDlg();
- // 对话框数据
- enum { IDD = IDD_ABOUTBOX };
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
- // 实现
- 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()
- // CXML_TestDlg 对话框
- CXML_TestDlg::CXML_TestDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CXML_TestDlg::IDD, pParent)
- , m_strDocName(_T(""))
- , m_strDocContents(_T(""))
- , m_bTreeView(FALSE)
- {
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CXML_TestDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- DDX_Text(pDX, IDC_STC_DOC_NAME, m_strDocName);
- DDX_Text(pDX, IDC_EDT_DOC_CONTENTS, m_strDocContents);
- DDX_Check(pDX, IDC_CHECK1, m_bTreeView);
- DDX_Control(pDX, IDC_TREE_CONTENTS, m_TreeContents);
- }
- BEGIN_MESSAGE_MAP(CXML_TestDlg, CDialog)
- ON_WM_SYSCOMMAND()
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
- ON_BN_CLICKED(IDC_CHECK1, OnBnClickedCheck1)
- END_MESSAGE_MAP()
- // CXML_TestDlg 消息处理程序
- BOOL CXML_TestDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // 将“关于...”菜单项添加到系统菜单中。
- // IDM_ABOUTBOX 必须在系统命令范围内。
- 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);
- }
- }
- // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
- // 执行此操作
- SetIcon(m_hIcon, TRUE); // 设置大图标
- SetIcon(m_hIcon, FALSE); // 设置小图标
- // TODO: 在此添加额外的初始化代码
- ::OleInitialize(NULL);
- HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument);
- if(FAILED(hr))
- {
- _com_error er(hr);
- AfxMessageBox(er.ErrorMessage());
- EndDialog(1);
- }
- return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
- }
- void CXML_TestDlg::OnSysCommand(UINT nID, LPARAM lParam)
- {
- if ((nID & 0xFFF0) == IDM_ABOUTBOX)
- {
- CAboutDlg dlgAbout;
- dlgAbout.DoModal();
- }
- else
- {
- CDialog::OnSysCommand(nID, lParam);
- }
- }
- // 如果向对话框添加最小化按钮,则需要下面的代码
- // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
- // 这将由框架自动完成。
- void CXML_TestDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // 用于绘制的设备上下文
- SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
- // 使图标在工作矩形中居中
- 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;
- // 绘制图标
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
- //当用户拖动最小化窗口时系统调用此函数取得光标显示。
- HCURSOR CXML_TestDlg::OnQueryDragIcon()
- {
- return static_cast<HCURSOR>(m_hIcon);
- }
- void CXML_TestDlg::OnBnClickedButton1()
- {
- CFileDialog fileDlg(TRUE);
- if(IDOK == fileDlg.DoModal())
- {
- m_strDocName = fileDlg.GetPathName();
- m_strDocContents = "Empty Document";
- m_TreeContents.DeleteAllItems();
- UpdateData(FALSE);
- CString strFileName(m_strDocName);
- _bstr_t bstrFileName;
- bstrFileName = strFileName.AllocSysString();
- variant_t vResult;
- vResult = m_plDomDocument->load(bstrFileName);
- if(((bool)vResult) == TRUE)
- {
- _bstr_t bstrDocContents = m_plDomDocument->xml;
- m_strDocContents = (LPCTSTR)bstrDocContents;
- m_pDocRoot = m_plDomDocument->documentElement;
- DisplayChildren(TVI_ROOT, m_pDocRoot);
- if(m_TreeContents.GetCount() == 0)
- m_TreeContents.InsertItem("Empty Document");
- }
- else
- {
- m_strDocContents = "Document FAILED to load!";
- m_TreeContents.InsertItem("Document FAILED to load!");
- }
- UpdateData(FALSE);
- }
- }
- void CXML_TestDlg::OnBnClickedCheck1()
- {
- UpdateData(TRUE);
- if(m_bTreeView)
- {
- GetDlgItem(IDC_EDT_DOC_CONTENTS)->ShowWindow(SW_HIDE);
- m_TreeContents.ShowWindow(SW_SHOW);
- }
- else
- {
- m_TreeContents.ShowWindow(SW_HIDE);
- GetDlgItem(IDC_EDT_DOC_CONTENTS)->ShowWindow(SW_SHOW);
- }
- }
- void CXML_TestDlg::DisplayChildren(HTREEITEM hParent, MSXML::IXMLDOMNodePtr pParent)
- {
- HTREEITEM hItem = DisplayChild(hParent, pParent);
- for(MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;
- pChild != NULL;
- pChild = pChild->nextSibling)
- DisplayChildren(hItem, pChild);
- }
- HTREEITEM CXML_TestDlg::DisplayChild(HTREEITEM hParent, MSXML::IXMLDOMNodePtr pChild)
- {
- CString strElement;
- if(MSXML::NODE_TEXT == pChild->nodeType)
- strElement = (LPCTSTR)pChild->text;
- else
- strElement = (LPCTSTR)pChild->nodeName;
- return m_TreeContents.InsertItem(strElement, hParent);
- }