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

📄 docerdlg.cpp

📁 这是一个能够自动生成文档的程序
💻 CPP
字号:
// CodeDocerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DocerDlg.h"
#include "DocMaker.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define PDIRS_FILE_NAME  "pdir.txt"
#define MACRO_FILE_NAME  "macro.txt"
#define DOC_FILE_NAME    "doc.txt"

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CDocerDlg dialog

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

void CDocerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDocerDlg)
	DDX_Control(pDX, IDC_DELETE_LIB_DIR, mcDeleteLibDir);
	DDX_Control(pDX, IDC_DELETE_PROJECT_DIR, mcDeleteProjectDir);
	DDX_Control(pDX, IDC_PROJECT_DIR, mcProjectDir);
	DDX_Control(pDX, IDC_LIB_DIR, mcLibDir);
	DDX_Text(pDX, IDC_CONDITION, mCondition);
	DDX_Text(pDX, IDC_DOC_DIR, mDocDir);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDocerDlg, CDialog)
	//{{AFX_MSG_MAP(CDocerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_SELECT_PROJECT_DIR, OnSelectProjectDir)
	ON_BN_CLICKED(IDC_DELETE_PROJECT_DIR, OnDeleteProjectDir)
	ON_NOTIFY(NM_CLICK, IDC_PROJECT_DIR, OnClickProjectDir)
	ON_BN_CLICKED(IDC_SELECT_LIB_DIR, OnSelectLibDir)
	ON_BN_CLICKED(IDC_DELETE_LIB_DIR, OnDeleteLibDir)
	ON_NOTIFY(NM_CLICK, IDC_LIB_DIR, OnClickLibDir)
	ON_BN_CLICKED(IDC_SELECT_DOC_DIR, OnSelectDocDir)
	ON_BN_CLICKED(IDC_MAKE_DOC, OnMakeDoc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDocerDlg message handlers

BOOL CDocerDlg::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
    CStdioFile file;
	if(file.Open(PDIRS_FILE_NAME, CFile::modeRead))
	{
		CString path;
		int index = 0;
		while(file.ReadString(path))
		{
			if(!path.IsEmpty() && gPages.AddUserDir(path))
			    mcProjectDir.InsertItem(index++, path);
		}
		file.Close();
	}

	if(file.Open(MACRO_FILE_NAME, CFile::modeRead))
	{
		file.ReadString(mCondition);
		file.Close();
	}

	if(file.Open(DOC_FILE_NAME, CFile::modeRead))
	{
		file.ReadString(mDocDir);
		file.Close();
	}

	UpdateData(FALSE);
	UpdateUI();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CDocerDlg::OnSelectProjectDir() 
{
	CString path;
	BrowsePath(path);

	if(path.IsEmpty() || !gPages.AddUserDir(path))
		return;

    int count = mcProjectDir.GetItemCount();
    mcProjectDir.InsertItem(count, path);
	UpdateUI();
}

void CDocerDlg::OnDeleteProjectDir() 
{
	POSITION pos = mcProjectDir.GetFirstSelectedItemPosition();
	if(pos != NULL)
	{
	    int index = mcProjectDir.GetNextSelectedItem(pos);
		CString str = mcProjectDir.GetItemText(index, 0);
		gPages.DeleteUserDir(str);
        mcProjectDir.DeleteItem(index);
	}

	UpdateUI();
}


void CDocerDlg::OnSelectLibDir() 
{
	CString path;
	BrowsePath(path);
	if(path.IsEmpty() || !gPages.AddLibDir(path))
		return;

	int count = mcLibDir.GetItemCount();
	mcLibDir.InsertItem(count, path);
	UpdateUI();
}

void CDocerDlg::OnDeleteLibDir() 
{
	POSITION pos = mcLibDir.GetFirstSelectedItemPosition();
	if(pos != NULL)
	{
	    int index = mcLibDir.GetNextSelectedItem(pos);
		CString str = mcLibDir.GetItemText(index, 0);
		gPages.DeleteLibDir(str);
        mcLibDir.DeleteItem(index);
	}

	UpdateUI();
}


int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, 
                                  LPARAM lParam, LPARAM lpData)
{
    _TCHAR szDir[_MAX_PATH];

    switch(uMsg)
    {
    case BFFM_INITIALIZED:  // 初始化
        break;
    case BFFM_SELCHANGED:   // 路径改变
        if(SHGetPathFromIDList((LPITEMIDLIST)lParam, szDir))
        SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM)szDir);
         break;
    default:
        break;
    }
    return 0;
}


void CDocerDlg::BrowsePath(CString& oPath)
{
    _TCHAR          pszDisplayName[MAX_PATH];
    BROWSEINFO      bi;
    LPITEMIDLIST    lpID;

    // 设置参数
    bi.hwndOwner = GetSafeHwnd();          
    bi.pidlRoot = NULL;                    
    bi.pszDisplayName = pszDisplayName;    
    bi.lpszTitle = _T("选择路径");         
    bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;   
    bi.lpfn = BrowseCallbackProc;          
    bi.lParam = 0;                         
    bi.iImage = NULL;                      

    lpID = SHBrowseForFolder( &bi );
    if( lpID != NULL )
    {
        if(SHGetPathFromIDList(lpID, pszDisplayName ))
        {
            oPath = pszDisplayName;
            if(oPath[oPath.GetLength() - 1] != '\\')
			{
                oPath += _T('\\');
			}
        }
    }
}

void CDocerDlg::OnClickProjectDir(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateUI();
	
	*pResult = 0;
}

void CDocerDlg::UpdateUI()
{
	POSITION pos = mcProjectDir.GetFirstSelectedItemPosition();
	if(pos != NULL)
	    mcDeleteProjectDir.EnableWindow(TRUE);
	else
		mcDeleteProjectDir.EnableWindow(FALSE);

	pos = mcLibDir.GetFirstSelectedItemPosition();
	if(pos != NULL)
	    mcDeleteLibDir.EnableWindow(TRUE);
	else
		mcDeleteLibDir.EnableWindow(FALSE);
}


void CDocerDlg::OnClickLibDir(NMHDR* pNMHDR, LRESULT* pResult) 
{
	UpdateUI();
	
	*pResult = 0;
}

void CDocerDlg::OnSelectDocDir() 
{
	BrowsePath(mDocDir);
	//mDocDir += "Doc";
    UpdateData(FALSE);
	UpdateUI();
}

BOOL FindDir(const CString& iDirName)
{
	CFileFind finder;
	BOOL found = finder.FindFile(iDirName + "\"" + "\\*.*", 0);
	return found;
}

BOOL CreateDirectory(const CString& iPath)
{
	if(FindDir(iPath))
		return TRUE;

	CString path = iPath;
	if(path.Right(1) == "\\")
		path.Delete(path.GetLength()-1);

	CString parentDir;
	int pos = path.ReverseFind('\\');
	if(pos != -1)
	{
		parentDir = path.Left(pos);
		CreateDirectory(parentDir);
	}
	else
	{
		return TRUE;
	}

	::CreateDirectory(iPath, NULL);
	return TRUE;
}

void CDocerDlg::OnMakeDoc() 
{
	//gIdens.Init();
	UpdateData();

	if(gPages.IsUserDirEmpty())
	{
		::AfxMessageBox("请选择工程目录");
		return;
	}

	if(mDocDir.IsEmpty())
	{
		::AfxMessageBox("请选择文档的保存目录");
		return;
	}

	CreateDirectory(mDocDir);
	CDocMaker maker;
	POSITION pos = gPages.GetFirstUserDirPosition();
	gIdens.AddGlobalMacro(mCondition);
	while(pos != NULL)
	{
		CString dir(*gPages.GetNextuserDir(pos));
	    maker.ParseFiles(dir, mDocDir);
	}

	gSymbols.OutputSymbols(mDocDir);
	::AfxMessageBox("文件已生成。\n\n请将XSL目录下的文件拷贝到文档目录中,\n双击index.htm即可浏览文档。");

    CStdioFile file;
	if(file.Open(PDIRS_FILE_NAME, CFile::modeCreate | CFile::modeWrite))
	{
		for(int i=0; i<mcProjectDir.GetItemCount(); i++)
		{
			file.WriteString(mcProjectDir.GetItemText(i, 0));
			file.WriteString("\n");
		}
		file.Close();
	}

	if(file.Open(MACRO_FILE_NAME, CFile::modeCreate | CFile::modeWrite))
	{
		file.WriteString(mCondition);
		file.Close();
	}

	if(file.Open(DOC_FILE_NAME, CFile::modeCreate | CFile::modeWrite))
	{
		file.WriteString(mDocDir);
		file.Close();
	}
}

⌨️ 快捷键说明

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