📄 xmlviewerdlg.cpp
字号:
// XMLViewerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "XMLViewer.h"
#include "XMLViewerDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
END_MESSAGE_MAP()
// CXMLViewerDlg dialog
CXMLViewerDlg::CXMLViewerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CXMLViewerDlg::IDD, pParent)
, m_strDocName(_T(""))
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CXMLViewerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_STC_DOC_NAME, m_strDocName);
DDX_Control(pDX, IDC_TREE_DOC_CONTENTS, m_treeDocContents);
}
BEGIN_MESSAGE_MAP(CXMLViewerDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDOK, OnBnClickedOpenDoc)
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedExpandAll)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedCollapseAll)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
END_MESSAGE_MAP()
// CXMLViewerDlg message handlers
BOOL CXMLViewerDlg::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
// Initialize COM
::CoInitialize(NULL);
HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument);
if (FAILED(hr))
{
_com_error er(hr);
AfxMessageBox(er.ErrorMessage());
EndDialog(1);
}
return TRUE;
}
void CXMLViewerDlg::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 CXMLViewerDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CXMLViewerDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CXMLViewerDlg::OnBnClickedOpenDoc()
{
// Display file open dialog and let user specify
// XML file.
CFileDialog fileDlg(TRUE);
fileDlg.m_ofn.lpstrFilter = "XML Documents (*.XML)\0*.xml\0\0";
if (IDOK == fileDlg.DoModal())
{
m_strDocName = fileDlg.GetPathName();
m_treeDocContents.DeleteAllItems();
UpdateData(FALSE); // <-- Just in case we don't get anything
// specify xml file name
CString strFileName(m_strDocName);
// convert xml file name string to something
// COM can handle (BSTR)
_bstr_t bstrFileName;
bstrFileName = strFileName.AllocSysString();
// call the IXMLDOMDocumentPtr's load function
// to load the XML document
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName);
if (((bool)vResult) == TRUE) // success!
{
m_pDocRoot = m_plDomDocument->documentElement;
DisplayChildren(TVI_ROOT, m_pDocRoot);
if (0 == m_treeDocContents.GetCount())
{
m_treeDocContents.InsertItem("Empty document");
}
else
{
OnBnClickedExpandAll();
}
}
else
{
m_treeDocContents.InsertItem("Document FAILED to load!");
}
UpdateData(FALSE);
}
}
void CXMLViewerDlg::DisplayChildren(HTREEITEM hParent, MSXML::IXMLDOMNodePtr pParent)
{
// display the current node's name
HTREEITEM hItem = DisplayChild(hParent, pParent);
// simple for loop to get all children
for (MSXML::IXMLDOMNodePtr pChild = pParent->firstChild;
NULL != pChild;
pChild = pChild->nextSibling)
{
// for each child, call this function so that we get
// its children as well
DisplayChildren(hItem, pChild);
}
}
HTREEITEM CXMLViewerDlg::DisplayChild(HTREEITEM hParent, MSXML::IXMLDOMNodePtr pChild)
{
// Determine the node type and based on that type
// retrieve the value we want in the treeview
CString strElement;
if (MSXML::NODE_TEXT == pChild->nodeType)
{
strElement = (LPCTSTR)pChild->text;
}
else
{
strElement = (LPCTSTR)pChild->nodeName;
}
// Add the element抯 node name to the treeview
// and return the hItem.
return m_treeDocContents.InsertItem(strElement, hParent);
}
void CXMLViewerDlg::OnBnClickedExpandAll()
{
HTREEITEM hItem = m_treeDocContents.GetRootItem();
ExpandBranch(hItem);
m_treeDocContents.Select(hItem, TVGN_FIRSTVISIBLE);
}
void CXMLViewerDlg::OnBnClickedCollapseAll()
{
ExpandBranch(m_treeDocContents.GetRootItem(), FALSE);
}
void CXMLViewerDlg::ExpandBranch(HTREEITEM hItem, BOOL bExpand /* = TRUE */)
{
if (m_treeDocContents.ItemHasChildren(hItem))
{
m_treeDocContents.Expand(hItem, bExpand ? TVE_EXPAND : TVE_COLLAPSE);
hItem = m_treeDocContents.GetChildItem(hItem);
do{
ExpandBranch(hItem);
} while ((hItem = m_treeDocContents.GetNextSiblingItem(hItem)) != NULL);
}
}
void CXMLViewerDlg::OnBnClickedButton3()
{
CAboutDlg().DoModal();
}
void CAboutDlg::OnBnClickedOk()
{
CWaitCursor wait;
CString strUrl = "http://www.thecodechannel.com/redirect.asp?u=/&s=vcnb";
if (32 >= (int)ShellExecute(NULL, "open", strUrl, NULL, NULL, SW_SHOWNORMAL))
{
AfxMessageBox("::ShellExecute failed to open this link!");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -