定製筆記本小工具

打開小工具時,自動粘貼剪貼板數據,文件名設置爲當前日期,保存後自動打開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();

歡迎交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章