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

📄 kingbook.cpp

📁 个人电子图书管理系统.提供电子书的书名和其他信息.检查电子书信息的合法性(E-1) .为这本电子书生成 id.使用分类id
💻 CPP
字号:
// KingBook.cpp : 定义应用程序的类行为。
//

#include "stdafx.h"
#include "KingBook.h"
#include "MainFrm.h"

#include "KingBookDoc.h"
#include "KingBookView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CKingBookApp

BEGIN_MESSAGE_MAP(CKingBookApp, CWinApp)
    ON_COMMAND(ID_APP_ABOUT, &CKingBookApp::OnAppAbout)
    // 基于文件的标准文档命令
    ON_COMMAND(ID_FILE_NEW, &CWinApp::OnFileNew)
    ON_COMMAND(ID_FILE_OPEN, &CWinApp::OnFileOpen)
    // 标准打印设置命令
    ON_COMMAND(ID_FILE_PRINT_SETUP, &CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()


// CKingBookApp 构造

CKingBookApp::CKingBookApp()
{
    EnableHtmlHelp();

    // TODO: 在此处添加构造代码,
    // 将所有重要的初始化放置在 InitInstance 中
}

CKingBookApp::~CKingBookApp()
{
#ifdef DEBUG
    newMemState.Checkpoint();
    diffMemState.Difference( oldMemState, newMemState );
    diffMemState.DumpStatistics();
#endif 
}
// 唯一的一个 CKingBookApp 对象

CKingBookApp theApp;


// CKingBookApp 初始化



/// <summary>CKingBookApp的初始化</summary>
/// <returns>成功返回TRUE; 失败返回FALSE, 应用程序初始化失败.</returns>
/// <exception>NULL</exception>
/// <remarks>执行的初始化:
/// <list type="bullet">
/// <item><description>加载配置信息</description></item>
/// </list>
/// </remarks>
BOOL CKingBookApp::InitInstance()
{
#ifdef DEBUG
    AfxEnableMemoryTracking(TRUE);
    afxMemDF = allocMemDF | delayFreeMemDF | checkAlwaysMemDF;
    oldMemState.Checkpoint();
#endif 
    // 加载配置文件 
    if(this->LoadConfigFile() != 0)
    {
        AfxMessageBox(_T("加载配置文件失败!"), MB_ICONSTOP);
        return FALSE;
    }

    // 如果一个运行在 Windows XP 上的应用程序清单指定要
    // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,
    //则需要 InitCommonControlsEx()。否则,将无法创建窗口。
    //
    //InitCommonControlsEx
    INITCOMMONCONTROLSEX InitCtrls;
    InitCtrls.dwSize = sizeof(InitCtrls);
    // 将它设置为包括所有要在应用程序中使用的
    // 公共控件类。
    InitCtrls.dwICC = ICC_WIN95_CLASSES;
    InitCommonControlsEx(&InitCtrls);

    CWinApp::InitInstance();

    // 初始化 OLE 库
    if (!AfxOleInit())
    {
        AfxMessageBox(IDP_OLE_INIT_FAILED);
        return FALSE;
    }
    AfxEnableControlContainer();
    // 标准初始化
    // 如果未使用这些功能并希望减小
    // 最终可执行文件的大小,则应移除下列
    // 不需要的特定初始化例程
    // 更改用于存储设置的注册表项
    // TODO: 应适当修改该字符串,
    // 例如修改为公司或组织名
    SetRegistryKey(_T("应用程序向导生成的本地应用程序"));
    LoadStdProfileSettings(4);  // 加载标准 INI 文件选项(包括 MRU)
    // 注册应用程序的文档模板。文档模板
    // 将用作文档、框架窗口和视图之间的连接
    CSingleDocTemplate* pDocTemplate;
    pDocTemplate = new CSingleDocTemplate(
        IDR_MAINFRAME,
        RUNTIME_CLASS(CKingBookDoc),
        RUNTIME_CLASS(CMainFrame),       // 主 SDI 框架窗口
        RUNTIME_CLASS(CKingBookView));
    if (!pDocTemplate)
        return FALSE;
    AddDocTemplate(pDocTemplate);

    // 分析标准外壳命令、DDE、打开文件操作的命令行
    CCommandLineInfo cmdInfo;
    ParseCommandLine(cmdInfo);

    // 调度在命令行中指定的命令。如果
    // 用 /RegServer、/Register、/Unregserver 或 /Unregister 启动应用程序,则返回 FALSE。
    if (!ProcessShellCommand(cmdInfo))
        return FALSE;

    // 唯一的一个窗口已初始化,因此显示它并对其进行更新
    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    // 仅当具有后缀时才调用 DragAcceptFiles
    //  在 SDI 应用程序中,这应在 ProcessShellCommand  之后发生
    return TRUE;
}


/// <summary>保存/更新应用程序配置文件</summary>
/// <returns>成功返回0; 失败返回 其他值.</returns>
/// <exception>NULL</exception>
/// <remarks>配置文件为:config.xml</remarks>
int CKingBookApp::SaveConfigFile()
{
    // 填充数据成员
    TiXmlNode* root = m_cfgXml.FirstChild("config");
    if(root == NULL)
        return 2;

    root->ToElement()->SetAttribute("bookXml", this->m_cfgBookXml);

    root->ToElement()->SetAttribute("bookInfo", this->m_cfgBookInfo);

    root->ToElement()->SetAttribute("xslt", this->m_cfgXslt);

    root->ToElement()->SetAttribute("temp", this->m_cfgTemp);

    root->ToElement()->SetAttribute("moreonce", "true");

    this->m_cfgXml.SaveFile(_T("config.xml"));
    if(this->m_cfgXml.Error())
    {
        // AfxMessageBox(_T("加载配置文件失败!"), MB_ICONSTOP);
        return 1;
    }
    return 0;
}


/// <summary>加载配置文件</summary>
/// <returns>成功返回0;失败返回 其他</returns>
/// <exception>NULL</exception>
/// <remarks>配置文件为:config.xml</remarks>
int CKingBookApp::LoadConfigFile()
{
    // 从文件系统载入
    this->m_cfgXml.LoadFile(_T("config.xml"), TIXML_ENCODING_UTF8);
    if(this->m_cfgXml.Error())
    {
        // AfxMessageBox(_T("加载配置文件失败!"), MB_ICONSTOP);
        return 1;
    }

    // 填充数据成员
    TiXmlNode* root = m_cfgXml.FirstChild("config");
    if(root == NULL)
        return 2;

    const char* moreOnce = root->ToElement()->Attribute("moreonce");
    if(std::string("true") == std::string(moreOnce)) // 不是第一次运行
    {
        const char* bookXml = root->ToElement()->Attribute("bookXml");
        if(bookXml == NULL)
            return 3;
        this->m_cfgBookXml = bookXml;

        const char* bookInfo = root->ToElement()->Attribute("bookInfo");
        if(bookInfo == NULL)
            return 4;
        this->m_cfgBookInfo = bookInfo;

        const char* xslt = root->ToElement()->Attribute("xslt");
        if(xslt == NULL)
            return 5;
        this->m_cfgXslt = xslt;

        const char* temp = root->ToElement()->Attribute("temp");
        if(temp == NULL)
            return 6;
        this->m_cfgTemp = temp;
    }
    else //第一次运行,进行首次运行的配置
    {
#if BLOCK("构造配置文件")
        TCHAR szCurPath[256] = {0};
        DWORD nSize = 256;
        int iLastSperate   =   0; 
        int i = 0;

        GetModuleFileName(NULL, szCurPath, 256);
        for(i=0; i<256; i++)   
        {   
            if(szCurPath[i] == _T('\\'))     
            {   
                iLastSperate = i;   
            }   
            else if(szCurPath[i] == _T('\0'))   
            {   
                break;   
            }   
        }   

        if(iLastSperate > 0 && i <   256)   
        {   
            szCurPath[iLastSperate] = _T('\0');   
        }   
        else   
        {   
            return   (-1);   
        }  
        std::string strCurPath = szCurPath;
        m_cfgBookXml = strCurPath + "\\BookData.xml" ;   
        m_cfgBookInfo = strCurPath + "\\bookInfo.xml";  
        m_cfgXslt = strCurPath + "\\RedBookInfo.xsl"; 
        {// TODO: 可能返回多个。
            // 得到 AppData 的位置
            HKEY hKey;
            char dirAppData[256]; //所查询注册表键值的内容,即AppData 的位置
            DWORD dwType=REG_SZ; //定义读取数据类型
            DWORD dwLength=256;
            HKEY ReRootKey = HKEY_CURRENT_USER; // 注册表主键名称
            TCHAR *ReSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"; 
            // 欲打开注册表值的地址
            TCHAR *ReValueName = "AppData"; /// /欲设置值的名称

            if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey) != ERROR_SUCCESS)
            {
                AfxMessageBox("错误:无法打开有关的hKEY");
                return 1;            
            }
            if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(unsigned char *)
                dirAppData,&dwLength) != ERROR_SUCCESS)
            {
                AfxMessageBox("错误:无法查询有关的注册表信息");
                return 1;
            }
            RegCloseKey(hKey);
            std::string strPathTemp =  dirAppData;
            strPathTemp += "\\KingBook\\Temp";
            BOOL hr = SHCreateDirectoryEx(NULL, strPathTemp.c_str(), NULL);
            if(hr == ERROR_SUCCESS )
            {
                this->m_cfgTemp = strPathTemp;
            }
            else
            {                
                if(hr == ERROR_ALREADY_EXISTS)
                {
                    this->m_cfgTemp = strPathTemp;
                }
                else
                {
                    AfxMessageBox("创建文件夹失败!");
                    AfxMessageBox(IntToString(hr).c_str());
                    return 1;
                }                   
            }
        }
#endif BLOCK("构造配置文件")
        // 修改数据文件的几个默认数据,以使其能够正常使用
        // 加上正确的路径
#if BLOCK("首次运行 修改数据文件")
        std::string bookDataFile = "BookData.xml";
        TiXmlDocument bookDataDoc;
        bookDataDoc.LoadFile(bookDataFile);
        if(bookDataDoc.Error())
        {
            MessageBox(NULL, bookDataDoc.ErrorDesc(), "Error Info", 0);

        }
        TiXmlNode* pRootNode = bookDataDoc.FirstChild("KingBook");
        if(pRootNode == NULL)
            return 11;
        TiXmlNode* pNode = pRootNode->FirstChild("Books");
        if(pNode == NULL)
            return 12;
        TiXmlNode* pBookItem = pNode->FirstChild("Book");
        if(pBookItem == NULL)
            return 13;
        TiXmlNode* pImageNode = pBookItem->FirstChild("image");
        if(pImageNode == NULL)
            return 14;

        TiXmlElement* pImage = (TiXmlElement*)pImageNode->FirstChild();
        if(pImage == NULL)
            return 15;
        std::string strImg = pImage->ToText()->Value();//->Value();
        //cout<< strImg << endl;
        strImg = strCurPath + "\\" + strImg;
        pImage->SetValue(strImg);
        pImage->ToText()->SetValue(strImg);

        TiXmlNode* pURLsNode = pBookItem->FirstChild("urls");
        if(pURLsNode == NULL)
            return 16;
        //cout<< *pURLsNode<<endl;
        TiXmlNode* pUrlNode = pURLsNode->FirstChild("url");
        if(pUrlNode == NULL)
            return 17;

        TiXmlElement* pUrl = (TiXmlElement*)pUrlNode->FirstChild();
        if(pUrl == NULL)
            return 18;
        std::string strUrl = pUrl->ToText()->Value();
        //cout<< strUrl<<endl;
        strUrl = strCurPath + "\\" + strUrl;
        pUrl->ToText()->SetValue(strUrl);

        if(! bookDataDoc.SaveFile())
            return 100;
#endif BLOCK("首次运行 修改数据文件")       

        SaveConfigFile();
    }
    return 0;
}

// 用于应用程序“关于”菜单项的 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()

// 用于运行对话框的应用程序命令
void CKingBookApp::OnAppAbout()
{
    CAboutDlg aboutDlg;
    aboutDlg.DoModal();
}


// CKingBookApp 消息处理程序

⌨️ 快捷键说明

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