打开小工具时,自动粘贴剪贴板数据,文件名设置为当前日期,保存后自动打开txt。
先看界面:
核心代码:
// TODO: 在此添加命令处理程序代码
CString strDir, strFile;
GetDlgItemText(IDC_EDIT_DIR, strDir);
GetDlgItemText(IDC_EDIT_FILE, strFile);
if (!strDir.GetLength())
{
strDir = CMfcStrFile::BrowseDir();
if (strDir.GetLength() > 0)
{
SetDlgItemText(IDC_EDIT_DIR, strDir);
}
else
{
return;
}
}
if (!strFile.GetLength())
{
//请先决定文件名
CString strTmp[2];
strTmp[0].LoadString(IDS_NAME_INVALID);
strTmp[1].LoadString(IDS_TIPS);
MessageBox(strTmp[0], strTmp[1], MB_ICONINFORMATION);
return;
}
_tstring sDir = CMfcStrFile::CString2string(strDir);
m_cfg.vDirPaths.clear();
m_cfg.vDirPaths.push_back(sDir);
_tstring sFile = CMfcStrFile::CString2string(strFile);
m_cfg.vFilePaths.clear();
m_cfg.vFilePaths.push_back(sFile);
//测试时间
clock_t start, finish;
double duration;
start = clock();
//开始显示进度
if (nullptr == m_pTaskbarList)
{
CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_ALL, IID_ITaskbarList3, (void**)&m_pTaskbarList);
}
if (m_pTaskbarList && m_hWnd)
{
m_pTaskbarList->SetProgressState(m_hWnd, TBPF_INDETERMINATE);
}
//======================分割线======================
long nLendth = m_RichEdit2Note.GetTextLength();
nLendth += 1; //'\0'结尾
TCHAR* pContent = nullptr;
CStdTpl::NewSafely(pContent, nLendth, true);
m_RichEdit2Note.GetWindowText(pContent, nLendth);
CString strContent(pContent);
DelAllBlankLine(strContent, ((CButton*)GetDlgItem(IDC_CHECK_DELBLANK))->GetCheck() == TRUE);
_tstring sFilePath = CStdStr::AddSlashIfNeeded(sDir) + sFile;
CStdFile::SaveTXTFile(sFilePath, CMfcStrFile::CString2string(strContent));
//======================分割线======================
if (m_hWnd)
{
m_pTaskbarList->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
FlashWindow(TRUE);
}
finish = clock();
duration = double(finish - start) / CLOCKS_PER_SEC;
int nMinutes = int(duration / 60);
int nSeconds = (int)duration % 60;
CString strTips = CString(ToString(nMinutes, 0).c_str()) + CString("分") + ToString(nSeconds, 0).c_str() + _T("秒");
//保存log,当前时间以及耗时
CString strTime, strDate; //获取系统时间
CTime tm;
tm = CTime::GetCurrentTime();
strTime = tm.Format("%Y年%m月%d日 %X");
strDate = tm.Format("%Y年%m月%d日");
CString strSaveLine = strTips + "\t\t" + strTime + "\t\t" + CString(sDir.c_str()) + '\n';
TCHAR exeFullPath[MAX_PATH];
CString strPath;
GetModuleFileName(NULL, exeFullPath, MAX_PATH);
strPath = (CString)exeFullPath;
int position = strPath.ReverseFind('\\');
strPath = strPath.Left(position);
CStdioFile sStdFile(strPath + "\\" + strDate + ".log", CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);
sStdFile.SeekToEnd();
setlocale(LC_CTYPE, "chs");//设定f
sStdFile.WriteString(strSaveLine);
setlocale(LC_ALL, "C"); //还原区域设定
sStdFile.Close();
WriteIniFile(GetIniPath(), m_cfg);
if (m_cfg.bRemPath)
{
PROCESS_INFORMATION pi;
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
STARTUPINFO si;
ZeroMemory (&si, sizeof (STARTUPINFO)); //初始化
si.cb = sizeof (STARTUPINFO);
si.wShowWindow=SW_SHOW;
si.dwFlags=STARTF_USESHOWWINDOW;
//注意
_tstring sCmd = _T("notepad.exe ") + sFilePath;
wchar_t* szCmdline = (wchar_t*)sCmd.c_str();
if (::CreateProcess (NULL, szCmdline, NULL,
NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))
{
}
}
else
{
AfxMessageBox(strTips);
}
CDialogEx::OnOK();
欢迎交流。