• 工具管理操作类介绍


    操作类CToolBoxCtr是被主对话框调用的。所有的底层操作都是由CToolBoxCtr实现的。

    功能:

         1、单例模式

        2、添加工具信息

        3、删除工具信息

        4、获取添加工具的图标信息。

        5、启动添加的工具

        6、初始化的时候,从配置文件读取上次添加的工具信息

        7、退出的时候,更新工具信息到配置文件里。

      1 #include "StdAfx.h"
      2 #include "ToolBoxCtr.h"
      3 
      4 // 单例模式
      5 CToolBoxCtr * CToolBoxCtr::m_pInstance = NULL;
      6 
      7 CToolBoxCtr::CToolBoxCtr(void)
      8 {
      9     m_toolFileInfoArray.RemoveAll();
     10 
     11     m_strWorkDir = GetCurrentFolder();
     12 
     13     m_strConfigFile.Format(_T("%s\\%s"), m_strWorkDir, TOOLBOXFILE);
     14 
     15     m_imageList.Create(32, 32, ILC_COLOR32, 0, 5);
     16     m_imageList.SetBkColor(DLG_BKCOLOR);
     17     ReadConfigFile();
     18 }
     19 
     20 CToolBoxCtr::~CToolBoxCtr(void)
     21 {
     22     SaveConfigFile();
     23     ClearToolFileInfoArray();
     24 }
     25 
     26 CToolBoxCtr * CToolBoxCtr::GetInstance()
     27 {
     28     if (NULL == m_pInstance)
     29     {
     30         m_pInstance = new CToolBoxCtr();
     31     }
     32 
     33     return m_pInstance;
     34 }
     35 
     36 void CToolBoxCtr::Destroy()
     37 {
     38     if (NULL != m_pInstance)
     39     {
     40         delete m_pInstance;
     41         m_pInstance = NULL;
     42     }
     43 }
     44 
     45 void CToolBoxCtr::ClearToolFileInfoArray()
     46 {
     47     for (int i = 0; i < m_toolFileInfoArray.GetCount(); i++)
     48     {
     49         delete m_toolFileInfoArray[i];
     50         m_toolFileInfoArray[i] = NULL;
     51     }
     52 
     53     m_toolFileInfoArray.RemoveAll();
     54 }
     55 
     56 // 读取工具信息
     57 void CToolBoxCtr::ReadConfigFile()
     58 {
     59     CStdioFile file;
     60 
     61     // 以只读模式打开
     62     if (!file.Open(m_strConfigFile, CFile::modeRead))
     63     {
     64          return;
     65     }
     66 
     67     CToolFileInfo * pToolFileInfo = NULL;
     68     CString strLine;
     69 
     70     while (file.ReadString(strLine))
     71     {
     72         strLine = strLine.Trim();
     73 
     74         // 注释,跳过
     75         if (strLine.IsEmpty()
     76             || 0 == strLine.Left(1).Compare(Comment_Flag))
     77         {
     78             continue;
     79         }
     80         else if (0 == strLine.Left(1).Compare(ToolInfoLeft_Flag)
     81                && 0 == strLine.Right(1).Compare(ToolInfoRight_Flag))
     82         {
     83             if (NULL != pToolFileInfo)
     84             {
     85                 m_toolFileInfoArray.Add(pToolFileInfo);
     86             }
     87             pToolFileInfo = new CToolFileInfo();
     88         }
     89         else if (NULL != pToolFileInfo)
     90         {
     91             int index = strLine.Find(_T("="));
     92 
     93             if (Not_Find != index)
     94             {
     95                 CString strLeft = strLine.Left(index).Trim();
     96                 CString strRight = strLine.Mid(index + 1).Trim();
     97 
     98                 if (strLeft.IsEmpty())
     99                 {
    100                     continue;
    101                 }
    102 
    103                 if (0 == strLeft.CompareNoCase(ToolInfoName))
    104                 {
    105                     pToolFileInfo->SetToolName(strRight);
    106                 }
    107                 else if (0 == strLeft.CompareNoCase(ToolInfoPath))
    108                 {
    109                     if (strRight.IsEmpty() || !PathFileExists(strRight))
    110                     {
    111                         delete pToolFileInfo;
    112                         pToolFileInfo = NULL;
    113                         continue;
    114                     }
    115                     pToolFileInfo->SetToolPath(strRight);
    116                     GetFileIconFromPath(pToolFileInfo);
    117                 }
    118             }
    119         }
    120     }
    121 
    122     if (NULL != pToolFileInfo)
    123     {
    124         m_toolFileInfoArray.Add(pToolFileInfo);
    125     }
    126 
    127     file.Close();
    128 }
    129 
    130 void CToolBoxCtr::SaveConfigFile()
    131 {
    132     CStdioFile file;
    133     if (!file.Open(m_strConfigFile, CFile::modeCreate | CFile::modeReadWrite))
    134     {
    135         return;
    136     }
    137 
    138     CString strLine;
    139 
    140     for (int i = 0; i < m_toolFileInfoArray.GetCount(); i++)
    141     {
    142         strLine.Format(_T("[ToolInfo%d]\r\n"), i + 1);
    143         file.WriteString(strLine);
    144 
    145         strLine.Format(_T("ToolName = %s\r\n"), m_toolFileInfoArray[i]->GetToolName());
    146         file.WriteString(strLine);
    147 
    148         strLine.Format(_T("ToolPath = %s\r\n\r\n"), m_toolFileInfoArray[i]->GetToolPath());
    149         file.WriteString(strLine);
    150     }
    151 
    152     file.Close();
    153 }
    154 
    155 CString CToolBoxCtr::GetCurrentFolder()
    156 {
    157     char szPath[_MAX_PATH] = {0};
    158     GetModuleFileName(NULL, szPath, sizeof(szPath));
    159 
    160     char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
    161     _splitpath_s(szPath, drive, dir, fname, ext);
    162 
    163     CString strPath;
    164     strPath.Format(_T("%s\\%s"), drive, dir);
    165 
    166     return strPath;
    167 }
    168 
    169 void CToolBoxCtr::GetFileIconFromPath(CToolFileInfo * pFileInfo)
    170 {
    171     if (NULL == pFileInfo)
    172     {
    173         return;
    174     }
    175 
    176     CString strPath = pFileInfo->GetToolPath();
    177     if (strPath.IsEmpty() || !PathFileExists(strPath))
    178     {
    179         return;
    180     }
    181 
    182     SHFILEINFO stFileInfo;
    183     :: SHGetFileInfo( strPath, 0, &stFileInfo, sizeof(stFileInfo), 
    184                      SHGFI_ICON | SHGFI_LARGEICON | SHGFI_TYPENAME);
    185     m_imageList.Add(stFileInfo.hIcon);
    186 
    187     if (IsExeType(stFileInfo.szTypeName))
    188     {
    189         pFileInfo->SetToolType(TToolType_Exe_Yes);
    190     }
    191     else
    192     {
    193         pFileInfo->SetToolType(TToolType_Exe_No);
    194     }
    195 
    196 }
    197 
    198 BOOL CToolBoxCtr::IsExeType(CString strTypeName)
    199 {
    200     char szType[][20] = {_T("应用程序"), _T("Windows 批处理文件")};
    201 
    202     for (int i = 0; i < sizeof(szType)/sizeof(szType[0]); i++)
    203     {
    204         if (0 == strTypeName.CompareNoCase(szType[i]))
    205         {
    206             return TRUE;
    207         }
    208     }
    209     return FALSE;
    210 }
    211 
    212 void CToolBoxCtr::AddNewTool(CString strToolName, CString strToolPath)
    213 {
    214     if (strToolName.IsEmpty() 
    215         || strToolPath.IsEmpty()
    216         || !PathFileExists(strToolPath))
    217     {
    218         return;
    219     }
    220 
    221     CToolFileInfo * PToolInfo = new CToolFileInfo;
    222     PToolInfo->SetToolName(strToolName);
    223     PToolInfo->SetToolPath(strToolPath);
    224     GetFileIconFromPath(PToolInfo);
    225 
    226     m_toolFileInfoArray.Add(PToolInfo);
    227 }
    228 
    229 CString CToolBoxCtr::GetToolNameFromPath(CString strPath)
    230 {
    231     char drive[_MAX_DRIVE], dir[_MAX_DIR], fname[_MAX_FNAME], ext[_MAX_EXT];
    232     _splitpath_s(strPath, drive, dir, fname, ext);
    233 
    234     return fname;
    235 }
    236 
    237 void CToolBoxCtr::RemoveTool(int nItem)
    238 {
    239     if (0 > nItem || nItem > m_toolFileInfoArray.GetCount()-1)
    240     {
    241         return;
    242     }
    243     delete m_toolFileInfoArray[nItem];
    244     m_toolFileInfoArray[nItem] = NULL;
    245     m_toolFileInfoArray.RemoveAt(nItem);
    246     m_imageList.Remove(nItem);
    247 }
    248 
    249 int CToolBoxCtr::RunToolFile(int index)
    250 {
    251     if (0 > index || index > m_toolFileInfoArray.GetCount()-1)
    252     {
    253         return RetCode_ExeRunFailed;
    254     }
    255 
    256     CToolFileInfo *pFileInfo = m_toolFileInfoArray[index];
    257 
    258     if (NULL == pFileInfo)
    259     {
    260         return RetCode_ExeRunFailed;
    261     }
    262 
    263     CString strCMD;
    264     strCMD.Format(_T("\"%s\""), pFileInfo->GetToolPath());
    265 
    266     if (TToolType_Exe_Yes == pFileInfo->getToolType())
    267     {
    268         STARTUPINFO si;
    269         PROCESS_INFORMATION pi;
    270 
    271         ZeroMemory( &si, sizeof(si) );
    272         si.cb = sizeof(si);
    273         ZeroMemory( &pi, sizeof(pi) );
    274 
    275         if (!CreateProcess(NULL, (LPSTR)(LPCTSTR)strCMD, NULL, NULL, FALSE, 0, NULL,
    276             NULL, &si, &pi )) 
    277         {
    278             return RetCode_ExeRunFailed;
    279         }
    280     }
    281     else
    282     {
    283         if (32 >= (int)ShellExecute(NULL, _T("open"), strCMD, NULL,
    284                                     NULL, SW_SHOWNORMAL))
    285         {
    286             return RetCode_FileOpenFailed;
    287         }
    288     }
    289 
    290     return RetCode_OK;
    291 
    292 }
    高山流水,海纳百川!
  • 相关阅读:
    跨域现象及原理分析
    git的commit撤销
    什么是幂等,什么情况下需要幂等,如何实现幂等
    flowable表简要说明
    关于SpringCloud、SpringBoot简单讲解
    常用的maven仓库地址
    Python安装第三方库常用方法
    反编译pyinstaller打包的exe安装包
    测试用例-需要添加@Transactional 这样 就不会再数据库里面留下痕迹了
    断点 太多了 调试运行特别慢-把所有的历史断点都去掉就快了
  • 原文地址:https://www.cnblogs.com/ahcc08/p/4033579.html
Copyright © 2020-2023  润新知