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

📄 toolscondlg.cpp

📁 Visual C++编写的工程解析器源代码
💻 CPP
字号:
// ToolsConDlg.cpp : implementation file
//

#include "stdafx.h"
#include "parsecproj.h"
#include "MainFrm.h"
#include "ToolsConDlg.h"

#include "InputDlg.h"
#include "FileEx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CToolsConDlg dialog


CToolsConDlg::CToolsConDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CToolsConDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CToolsConDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CToolsConDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CToolsConDlg)
	DDX_Control(pDX, IDC_TOOLS_LIST, m_wndListCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CToolsConDlg, CDialog)
	//{{AFX_MSG_MAP(CToolsConDlg)
	ON_COMMAND(ID_EXITDLG, OnExitdlg)
	ON_COMMAND(ID_ADDTOOL, OnAddtool)
	ON_COMMAND(ID_DELTOOL, OnDeltool)
	ON_COMMAND(ID_PREVTOOL, OnPrevtool)
	ON_COMMAND(ID_NEXTTOOL, OnNexttool)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/*****************************用户自定义函数位置********************************/

//得到系统工具配置文件的路径
CString CToolsConDlg::GetSysToolsInfoFilePath()
{
	char strPath[MAX_PATH];
	memset(strPath, 0x0, sizeof(strPath));

	::GetModuleFileName(::AfxGetInstanceHandle(), strPath, MAX_PATH);
	
	CString strRet = strPath;

	int nIndex = strRet.ReverseFind('\\');

	strRet = strRet.Left(nIndex + 1);

	strRet = strRet + GLOBAL_TOOLS_INFO_FILE_NAME;

	return strRet;
}

//初始化名值对列表
BOOL CToolsConDlg::InitAppInfoList()
{
	CFileFind* ffFinder = new CFileFind;
	
	if(ffFinder->FindFile((LPCTSTR)m_strInfoFilepath) == 0)
	{
		HANDLE hHandle = ::CreateFile((LPCTSTR)m_strInfoFilepath, GENERIC_READ | GENERIC_WRITE,
			FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_HIDDEN, 0);

		//如创建文件失败
		if(hHandle == INVALID_HANDLE_VALUE)
			return FALSE;
		else
			::CloseHandle(hHandle);
	}

	delete ffFinder;

	CFileEx file;
	try
	{
		if(file.Open((LPCTSTR)m_strInfoFilepath, CFile::modeRead))
		{			
			m_sltAppList.RemoveAll();
			
			while(!file.IsEof())
			{
				CString str = file.ReadALine();
				str.Remove('\n');
				str.TrimLeft();	str.TrimRight();
				m_sltAppList.AddTail(str);
			}
			file.Close();
		}
	}
	catch(...)
	{
		return FALSE;
	}

	return TRUE;
}

//更新ListCtrl区域[图标]
BOOL CToolsConDlg::UpdateView(int nCurForceItem)
{
	POSITION pos = m_sltAppList.GetHeadPosition();
	
	m_wndListCtrl.DeleteAllItems();
	
	//清除图标列表
	for(int i = 0; i < m_ilListCtrl.GetImageCount(); ++i)
		m_ilListCtrl.Remove(i);

	while(pos != NULL)
	{
		CString strAppPath = m_sltAppList.GetNext(pos), 
			strDispName = strAppPath;

		int i = strAppPath.Find(']');

		//程序名
		strAppPath = strAppPath.Left(i);
		strAppPath = strAppPath.Right(strAppPath.GetLength() - 1);

		//菜单名
		strDispName = strDispName.Right(strDispName.GetLength() - i - 2);
		strDispName = strDispName.Left(strDispName.GetLength() - 1);

		//程序系统信息
		SHFILEINFO* lpsfi;
		lpsfi = GetAppSysInfo((LPCTSTR)strAppPath);		
		
		if(lpsfi != NULL)
		{
			m_ilListCtrl.Add(lpsfi->hIcon);

			m_wndListCtrl.InsertItem(m_wndListCtrl.GetItemCount(), 
					(LPCTSTR)strDispName, m_ilListCtrl.GetImageCount() - 1);
		}
		else
			return FALSE;
	}	
	//
	return TRUE;
}

//根据显示名找到其在列表中的位置
POSITION CToolsConDlg::FindByDispName(LPCTSTR lpszDiapName)
{
	POSITION pos = m_sltAppList.GetHeadPosition(), posPrev = pos;

	while(pos != NULL)
	{
		CString str = m_sltAppList.GetNext(pos);

		int nIndex = str.Find(']');
		str = str.Right(str.GetLength() - nIndex - 1);

		if(str.Find('[') == 0)
		{
			str = str.Left(str.GetLength() - 1);
			str = str.Right(str.GetLength() - 1);

			CString strTmp = (CString)lpszDiapName;	
			strTmp.TrimLeft();	strTmp.TrimRight();

			if(str.Compare((LPCTSTR)strTmp) == 0)
				return posPrev;
		}
		posPrev = pos;
	}
	return NULL;
}

//通过菜单显示名找到对应的应用程序路径
CString CToolsConDlg::GetPathByDispName(LPCTSTR lpszDiapName)
{
	POSITION pos = FindByDispName(lpszDiapName);

	CString strRet = "";
	if(pos != NULL)
	{
		strRet = m_sltAppList.GetAt(pos);
		
		int nIndex = strRet.Find(']');
		strRet = strRet.Left(nIndex);
		strRet = strRet.Right(strRet.GetLength() - 1);
	}

	return strRet;
}

//新增工具
BOOL CToolsConDlg::AddTools(LPCTSTR lpszAppPath, LPCTSTR lpszDiapName)
{
	POSITION pos = FindByDispName(lpszDiapName);
	if(pos != NULL)
	{
		MessageBox("该菜单名已被占用,请改换其他名称!", "提示...", MB_OK | MB_ICONINFORMATION);
		return FALSE;
	}

	CFileFind* ffFinder = new CFileFind;
	
	if(ffFinder->FindFile(lpszAppPath) == TRUE)
	{
		CString str = "";
		
		//构造名值对
		str = '[' + (CString)lpszAppPath + ']';
		str += '[' + (CString)lpszDiapName + ']';
		
		m_sltAppList.AddTail(str);

		delete ffFinder;
		return TRUE;
	}
	
	delete ffFinder;
	return FALSE;
}

//删除工具
BOOL CToolsConDlg::DelTools(LPCTSTR lpszDiapName)
{
	POSITION pos = FindByDispName(lpszDiapName);
	if(pos != NULL)
	{
		m_sltAppList.RemoveAt(pos);
		return TRUE;
	}

	return FALSE;
}

//向上移动工具所在的菜单位置
BOOL CToolsConDlg::MoveUp(LPCTSTR lpszDiapName)
{
	POSITION pos = FindByDispName(lpszDiapName);

	if(pos != NULL && pos != m_sltAppList.GetHeadPosition())
	{
		POSITION posOld = pos;

		CString str1 = m_sltAppList.GetPrev(pos);
		CString str2 = m_sltAppList.GetAt(pos);

		m_sltAppList.SetAt(pos, str1);
		m_sltAppList.SetAt(posOld, str2);

		return TRUE;
	}

	return FALSE;
}

//向下移动工具所在的菜单位置
BOOL CToolsConDlg::MoveDown(LPCTSTR lpszDiapName)
{
	POSITION pos = FindByDispName(lpszDiapName);

	if(pos != NULL && pos != m_sltAppList.GetTailPosition())
	{
		POSITION posOld = pos;

		CString str1 = m_sltAppList.GetNext(pos);
		CString str2 = m_sltAppList.GetAt(pos);

		m_sltAppList.SetAt(pos, str1);
		m_sltAppList.SetAt(posOld, str2);

		return TRUE;
	}

	return FALSE;
}

//根据文件路径名得到其程序内置信息
SHFILEINFO* CToolsConDlg::GetAppSysInfo(LPCTSTR lpszAppPath)
{
	SHFILEINFO* lpsfi;
	lpsfi = (SHFILEINFO*)malloc(sizeof(SHFILEINFO));
	memset(lpsfi, 0x0, sizeof(lpsfi));

	if( !::SHGetFileInfo(lpszAppPath, 0, lpsfi, sizeof(lpsfi), 
		SHGFI_DISPLAYNAME | SHGFI_ICON) )
	{
		MessageBox("打开文件时发生错误...", "错误", MB_OK | MB_ICONERROR);
		return NULL;
	}
	return lpsfi;
}

/***************************结束用户自定义函数位置******************************/
//本窗体的消息处理函数
BOOL CToolsConDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_strInfoFilepath = GetSysToolsInfoFilePath();	//得到系统配置文件路径

	if(InitAppInfoList() == FALSE)
	{
		MessageBox("初始化系统工具列表出错,请检查...", "错误信息", MB_OK | MB_ICONERROR);
		return FALSE;
	}
		
	//创建256色工具栏
	m_wndToolbar = new CToolBar;

	if(!m_wndToolbar->Create(this, WS_CHILD | WS_VISIBLE | 
		CBRS_TOP | CBRS_FLYBY | CBRS_TOOLTIPS , GLOBAL_TOOLS_TOOLBAR_ID))
		return FALSE;

	if(!m_wndToolbar->LoadToolBar(IDR_TOOLSBAR))
		return FALSE;

	CRect rc;
	this->GetClientRect(rc);

	m_wndToolbar->SetWindowPos(NULL, 0, 0, rc.Width(), 50, SWP_SHOWWINDOW);

	if(!m_ilListCtrl.Create(32, 32, ILC_COLOR8, 4, 4))
		return FALSE;

	m_wndListCtrl.SetImageList(&m_ilListCtrl, LVSIL_NORMAL);

	//初始化工具列表显示区域(ListCtrl区域)GetPathByDispName
	POSITION pos = m_sltAppList.GetHeadPosition();
	while(pos != NULL)
	{
		CString str = m_sltAppList.GetNext(pos);
		
		CString str1, str2;	//路径和菜单名
		int i = str.Find(']');
		
		str1 = str.Left(i);	str1 = str1.Right(str1.GetLength() - 1);
		str2 = str.Right(str.GetLength() - i - 2);	str2 = str2.Left(str2.GetLength() - 1);

		SHFILEINFO* lpsfi;
		lpsfi = GetAppSysInfo(str1.GetBuffer(0));
		
		if(lpsfi != NULL)
		{
			m_ilListCtrl.Add(lpsfi->hIcon);

			m_wndListCtrl.InsertItem(m_wndListCtrl.GetItemCount(), 
					(LPCTSTR)str2, m_ilListCtrl.GetImageCount() - 1);
		}
	}
	//
	
	return TRUE;
}

//Exit this Dialog
void CToolsConDlg::OnExitdlg() 
{	
	try
	{
		CFileEx file;
		
		if( file.Open((LPCTSTR)m_strInfoFilepath, CFile::modeWrite) )
		{
			POSITION pos = m_sltAppList.GetHeadPosition();
			while(pos != NULL)
			{
				CString str = m_sltAppList.GetNext(pos);	
				str += '\n';
				file.Write(str.GetBuffer(0), str.GetLength());		
			}
			file.Close();
		}
		else
		{
			//更新文件失败
			MessageBox("保存设置出错,请重试!", "错误...", MB_OK | MB_ICONERROR);
		}	
	}
	catch(...)
	{
		MessageBox("保存设置出错,请重试!", "错误...", MB_OK | MB_ICONERROR);
		return ;
	}

	//退出窗体
	this->OnOK();
}

//Add a Tool's Application form System Menu
void CToolsConDlg::OnAddtool() 
{
	//Select Executeable File
	static char strFilter[] = "可执行文件(.exe)|*.exe||";

	CFileDialog ofFileDlg(TRUE, "exe", NULL, 
		OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT | OFN_OVERWRITEPROMPT, 
		(LPCTSTR)strFilter, NULL);

	ofFileDlg.m_ofn.Flags &= ~OFN_ALLOWMULTISELECT;

	//if Selected Executeable File is Successful
	if(ofFileDlg.DoModal() == IDOK)
	{
		CString strAppPath = ofFileDlg.GetPathName();

		SHFILEINFO* lpsfi;
		lpsfi = GetAppSysInfo((LPCTSTR)strAppPath);		
		
		if(lpsfi != NULL)
		{		
			CInputDlg inputDlg;
		
			inputDlg.m_strCaption = "请输入工具菜单名:";
			inputDlg.m_strInputStr = lpsfi->szDisplayName;
			
			CString strDispName = (CString)lpsfi->szDisplayName;

			if(inputDlg.DoModal() == IDOK)			
				strDispName = inputDlg.m_strInputStr;

			if(this->AddTools((LPCTSTR)strAppPath, (LPCTSTR)strDispName))
			{
				m_ilListCtrl.Add(lpsfi->hIcon);

				m_wndListCtrl.InsertItem(m_wndListCtrl.GetItemCount(), 
					(LPCTSTR)strDispName, m_ilListCtrl.GetImageCount() - 1);
			}
		}
	}
}

//Delete a Tool's Application form System Menu
void CToolsConDlg::OnDeltool() 
{
	int nCount = m_wndListCtrl.GetSelectedCount();
	if(nCount > 0)
	{
		CString str = "";
		str.Format("您确认要删除这%d项工具菜单吗(Y/N)?", nCount);

		if(MessageBox(str, "提示...", MB_YESNO | MB_ICONQUESTION) == IDYES)
		{
			POSITION pos = NULL;
			pos = m_wndListCtrl.GetFirstSelectedItemPosition();
			
			while(pos != NULL)
			{
				int i = m_wndListCtrl.GetNextSelectedItem(pos);
				CString strText = m_wndListCtrl.GetItemText(i, 0);
				
				if(this->DelTools((LPCTSTR)strText))
					m_wndListCtrl.DeleteItem(i);
			}
		}
	}
	else
		MessageBox("您没选中任何菜单项,请先选择您要删除的菜单项,\n然后再进行删除操作...", "提示...", MB_OK | MB_ICONINFORMATION);
}

//Move a Tool's Application form System Menu to Prev
void CToolsConDlg::OnPrevtool() 
{
	int nCount = m_wndListCtrl.GetSelectedCount();
	if(nCount > 0)
	{
		POSITION pos = NULL;
		pos = m_wndListCtrl.GetFirstSelectedItemPosition();
		
		while(pos != NULL)
		{
			int i = m_wndListCtrl.GetNextSelectedItem(pos);
			CString strText = m_wndListCtrl.GetItemText(i, 0);
			
			if(MoveUp((LPCTSTR)strText))							
				//在界面上反应出来调整结果
				UpdateView(i - 1);			
		}		
	}
	else
		MessageBox("您没选中任何菜单项,请先选择您要移位的菜单项,\n然后再进行移位操作...", "提示...", MB_OK | MB_ICONINFORMATION);
}

//Move a Tool's Application form System Menu to Next
void CToolsConDlg::OnNexttool() 
{
	int nCount = m_wndListCtrl.GetSelectedCount();
	if(nCount > 0)
	{		
		POSITION pos = NULL;
		pos = m_wndListCtrl.GetFirstSelectedItemPosition();
		
		while(pos != NULL)
		{
			int i = m_wndListCtrl.GetNextSelectedItem(pos);
			CString strText = m_wndListCtrl.GetItemText(i, 0);
			
			if(MoveDown((LPCTSTR)strText))				
				//在界面上反应出来调整结果			
				UpdateView(i + 1);	
		}		
	}
	else
		MessageBox("您没选中任何菜单项,请先选择您要移位的菜单项,\n然后再进行移位操作...", "提示...", MB_OK | MB_ICONINFORMATION);
}

⌨️ 快捷键说明

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