📄 commands.cpp
字号:
strText = bstr;
long lStart = 0,lEnd = 0,iColEnd = 0,lCurrent;
pSel->get_TopLine(&lStart);
pSel->get_BottomLine(&lEnd);
pSel->get_CurrentLine(&lCurrent);
pSel->get_CurrentColumn(&iColEnd);
//Select a complete line
if(lStart == lEnd)
{
if((lCurrent == lEnd) && (iColEnd > 1))//not a complete line
{
strTmp.Format(_T("/*%s*/"),strText);
bstr = strTmp;
pSel->put_Text(bstr);
}
else if((lCurrent == (lEnd + 1)) && (iColEnd == 1))//a full line
{
strTmp.Format(_T("//%s"),strText);
bstr = strTmp;
pSel->put_Text(bstr);
}
}
else//multi-line selected
{
int idx = 0;
int totalline = lEnd - lStart + 1;
strTmp = _T("/*");
if(g_bAddTime && g_bAddUser)
{
CString strtt;
strTmp += _T("@**#---");
if(g_bAddTime)
{
SYSTEMTIME st;
::GetLocalTime(&st);
strtt.Format(_T("%04d-%02d-%02d %02d:%02d:%02d "),st.wYear,st.wMonth,
st.wDay,st.wHour,st.wMinute,st.wSecond);
strTmp += strtt;
}
if(g_bAddUser)
{
strtt.Format(_T("(%s)"),g_szUser);
strTmp += strtt;
}
strTmp += _T("---#**@");
}
strTmp += _T("\r\n");
strTmp += strText;
strTmp += _T("*/");
bstr = strTmp;
pSel->put_Text(bstr);
}
}
}
}
return S_OK;
}
STDMETHODIMP CCommands::OSRemark()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
CComPtr<IDispatch> pDispDoc;
CComQIPtr<ITextDocument, &IID_ITextDocument> pDoc;
m_pApplication->get_ActiveDocument(&pDispDoc);
pDoc = pDispDoc;
pDispDoc = NULL;
if(pDoc)
{
CComPtr<IDispatch> pDispSel;
CComQIPtr<ITextSelection, &IID_ITextSelection> pSel;
CComBSTR bstr;
CString strText,strTmp;
int nSelectLength = 0;
HRESULT hr;
pDoc->get_Selection(&pDispSel);
pSel = pDispSel;
pDispSel = NULL;
hr = pSel->get_Text(&bstr);
if(SUCCEEDED(hr))
{
nSelectLength = bstr.Length();
if(nSelectLength > 0)
{
strText = bstr;
long lStart = 0,lEnd = 0,iColEnd = 0,lCurrent;
pSel->get_TopLine(&lStart);
pSel->get_BottomLine(&lEnd);
pSel->get_CurrentLine(&lCurrent);
pSel->get_CurrentColumn(&iColEnd);
//select a single line
if(lStart == lEnd)
{
if((lCurrent == lEnd) && (iColEnd > 1))//not a complete line
{
strTmp.Format(_T("/*%s*/"),strText);
bstr = strTmp;
pSel->put_Text(bstr);
}
else if((lCurrent == (lEnd + 1)) && (iColEnd == 1))//yes,it is a full line
{
strTmp.Format(_T("//%s"),strText);
bstr = strTmp;
pSel->put_Text(bstr);
}
}
else//multi-line selected
{
int idx = 0;
int totalline = lEnd - lStart + 1;
strTmp = _T("//");
if(g_bAddTime && g_bAddUser)
{
CString strtt;
strTmp += _T("@**#---");
if(g_bAddTime)
{
SYSTEMTIME st;
::GetLocalTime(&st);
strtt.Format(_T("%04d-%02d-%02d %02d:%02d:%02d "),st.wYear,st.wMonth,
st.wDay,st.wHour,st.wMinute,st.wSecond);
strTmp += strtt;
}
if(g_bAddUser)
{
strtt.Format(_T("(%s)"),g_szUser);
strTmp += strtt;
}
strTmp += _T("---#**@\r\n");
}
for(int curline = 0; curline <= totalline; curline++)
{
if(curline == 0)
strText.Insert(idx,strTmp);
else
strText.Insert(idx,_T("//"));
idx = strText.Find(_T("\r\n"),idx);
idx += 2;
}
bstr = strText;
pSel->put_Text(bstr);
}
}
}
}
return S_OK;
}
STDMETHODIMP CCommands::OSUnremark()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
CComPtr<IDispatch> pDispDoc;
CComQIPtr<ITextDocument, &IID_ITextDocument> pDoc;
m_pApplication->get_ActiveDocument(&pDispDoc);
pDoc = pDispDoc;
pDispDoc = NULL;
if(pDoc)
{
CComPtr<IDispatch> pDispSel;
CComQIPtr<ITextSelection, &IID_ITextSelection> pSel;
CComBSTR bstr;
CString strText,strTmp;
int nSelectLength = 0;
HRESULT hr;
pDoc->get_Selection(&pDispSel);
pSel = pDispSel;
pDispSel = NULL;
hr = pSel->get_Text(&bstr);
if(SUCCEEDED(hr))
{
nSelectLength = bstr.Length();
if(nSelectLength > 0)
{
strText = bstr;
long lStart = 0,lEnd = 0,iColEnd = 0,lCurrent;
pSel->get_TopLine(&lStart);
pSel->get_BottomLine(&lEnd);
pSel->get_CurrentLine(&lCurrent);
pSel->get_CurrentColumn(&iColEnd);
//select a line
if(lStart == lEnd)
{
if((lCurrent == lEnd) && (iColEnd > 1))//not a completed line
{
//only deal with matched /* and */
int idxLeft = strText.Find(_T("/*"),0);
int idxRight = strText.Find(_T("*/"),0);
if(idxLeft != -1 && idxRight != -1)
{
strTmp = strText.Left(idxLeft);
strTmp += strText.Mid(idxLeft + 2,idxRight - idxLeft - 2);
strTmp += strText.Mid(idxRight + 2);
bstr = strTmp;
pSel->put_Text(bstr);
}
}
else if((lCurrent == (lEnd + 1)) && (iColEnd == 1))//not a completed line
{
int idx = strText.Find(_T("//"),0);
if(idx != -1)
{
strTmp = strText.Left(idx);
strTmp += strText.Mid(idx + 2);
bstr = strTmp;
pSel->put_Text(bstr);
}
}
}
else//multi-line selected
{
BOOL bChange = FALSE;
BOOL bBlockMark = FALSE;
int idxFirstMline = strText.Find(_T("//"),0);
int idxFirstBlk = strText.Find(_T("/*"),0);
if(idxFirstMline >= 0 && idxFirstBlk < 0)
bBlockMark = FALSE;
else if(idxFirstMline < 0 && idxFirstBlk >= 0)
bBlockMark = TRUE;
else if(idxFirstMline < idxFirstBlk)
bBlockMark = FALSE;
else if(idxFirstMline > idxFirstBlk)
bBlockMark = TRUE;
int idxLeft = strText.Find(_T("@**#---"),0);
int idxRight = strText.Find(_T("---#**@"),0);
if(idxLeft != -1 && idxRight != -1)
{
bChange = TRUE;
strTmp = strText.Left(bBlockMark ? idxLeft : idxLeft - 2);
strTmp += strText.Mid(idxRight + 7 + 2);
strText = strTmp;
}
if(bBlockMark)
{
int idx = strText.Find(_T("/*"),0);
int idxSecondBlk = strText.Find(_T("*/"),0);
if(idxSecondBlk >= 0)
{
strTmp = strText.Left(idx);
strText = strText.Mid(idx + 2);
idxSecondBlk = strText.Find(_T("*/"),0);
strTmp += strText.Left(idxSecondBlk);
strTmp += strText.Mid(idxSecondBlk + 2);
}
else
strTmp = strText;
}
else
{
strTmp = lpszNull;
int idx = 0;
while((idx = strText.Find(_T("//"),idx)) != -1)
{
bChange = TRUE;
strTmp += strText.Left(idx);
strText = strText.Mid(idx + 2);
idx = strText.Find(_T("\r\n"),0);//Only skip the first _T("//") of a line
strTmp += strText.Left(idx + 2);//Include _T("\r\n")
strText = strText.Mid(idx + 2);//Start from new line
idx = 0;
}
strTmp += strText;//It is not a good idea, but can void many bugs
}
if(bChange)
{
bstr = strTmp;
pSel->put_Text(bstr);
}
}
}
}
}
return S_OK;
}
#define DIAGRAMSTART "{{DIAGRAM_START"
#define DIAGRAMEND "}}DIAGRAM_END"
#define BDIAGRAMSTART L"//{{DIAGRAM_START"
#define BDIAGRAMEND L"//}}DIAGRAM_END"
STDMETHODIMP CCommands::OSInsertDiagram()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
DoDiagram();
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
STDMETHODIMP CCommands::OSZipWorkspace()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
ASSERT(g_pDevStudioWnd != NULL);
//Get the DevStudio frame window's title to prase workspace name
//if the DevStudio does not open any file or project,the title of frame window is "Microsoft Visual C++"
//if it open some file,but not a project,the title is "Microsoft Visual C++ - [ssss.cpp]"
//if it open a project,the title is "WorkspaceName - Microsoft Visual C++ - [CurrentOpenFile.cpp]"
CString strFrameTitle = g_pDevStudioWnd->m_cTitle;
CString strDswName,strDswPath;
int idx = strFrameTitle.Find(_T(" - "),0);
if(idx != -1)//the DevStudio just has open some file,may be not a project
{
strFrameTitle = strFrameTitle.Left(idx);
if(strFrameTitle.Compare(lpszDevStudioDefaultTitle) != 0)//it is a project name
{
strDswName = strFrameTitle;
}
}
if(!strDswName.IsEmpty())//dsw is open,prepare full dsw path
{
// we suppose that all projects of this workspace are in the workspace folder
CComBSTR bstrPrjName;
IGenericProject *pProject = NULL;
if(SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
{
pProject->get_FullName(&bstrPrjName);
pProject->Release();
CString strTmp = bstrPrjName;
int index = strTmp.Find(strDswName,0);
if(index != -1)//it is in the workspace folder
{
strDswPath = strTmp.Left(index + strDswName.GetLength());//not include the last '\'
}
}
}
CIni ini(g_szIniPathName);
BOOL bZipall;
ini.GetBoolValue(lpszZip,_T("bZipAllFile"),bZipall);
CString strZipFileType;
if(!bZipall)//need exclude some type files,so get the exclude type list
ini.GetValue(lpszZip,_T("sZipFileType"),strZipFileType);
CString strLastZipPath;
ini.GetValue(lpszZip,_T("sLastZipFolder"),strLastZipPath);
CString strLastZipOutputPath;
ini.GetValue(lpszZip,_T("sLastZipOutputFolder"),strLastZipOutputPath);
BOOL bCheckValidate;
ini.GetBoolValue(lpszZip,_T("bOpenCheck"),bCheckValidate);
CZipPrjFolderDlg dlg;
dlg.m_strFileType = strZipFileType;
if(strDswPath.IsEmpty())//still empty? some error occurred,get last zip source path form ini file
{
dlg.m_nzipSrc = 1;//zip other folder
dlg.m_strSrcFolder = strLastZipPath;
dlg.m_strOutputZipFile = strLastZipOutputPath + _T("untitled.zip");
}
else
{
dlg.m_nzipSrc = 0;//zip workspace
dlg.m_strSrcFolder = strDswPath;
dlg.m_strOutputZipFile = strLastZipOutputPath + strDswName + _T(".zip");
}
if(dlg.DoModal() == IDOK)
{
strLastZipPath = dlg.m_strSrcFolder;
ini.SetValue(lpszZip,_T("sLastZipFolder"),strLastZipPath);
strLastZipOutputPath = dlg.m_strOutputZipFile;
int idx = strLastZipOutputPath.ReverseFind(_T('\\'));
strLastZipOutputPath = strLastZipOutputPath.Left(idx + 1);
ini.SetValue(lpszZip,_T("sLastZipOutputFolder"),strLastZipOutputPath);
ini.Write(g_szIniPathName);
if(dlg.m_nzipSrc == 0)//zip workspace,so save it first
{
m_pApplication->ExecuteCommand(L"FileSaveAll");
m_pApplication->ExecuteCommand(L"WorkspaceSave");
}
CWaitCursor cursor;
BOOL bRtn = ZipFolderToFile(dlg.m_strSrcFolder,dlg.m_strOutputZipFile,strZipFileType);
cursor.Restore();
if(bCheckValidate && bRtn)
{
ShellExecute(NULL, NULL, dlg.m_strOutputZipFile, NULL, NULL, SW_SHOW);
}
}
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
STDMETHODIMP CCommands::OSOpenDswInZip()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_FALSE));
IGenericProject* pProject = NULL;
//close current workspace
if(SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
{
m_pApplication->ExecuteCommand(L"WorkspaceClose");
pProject->Release();
}
// see if the user cancelled
if(SUCCEEDED(m_pApplication->get_ActiveProject((IDispatch**)&pProject)))
{
pProject->Release();
return S_OK;
}
OpenZippedWorkspace();
VERIFY_OK(m_pApplication->EnableModeless(VARIANT_TRUE));
return S_OK;
}
BOOL CCommands::ZipFolderToFile(LPCTSTR lpszFolder,LPCTSTR lpszOutputFile,LPCTSTR lpszFileTypes)
{
BOOL bRtn = FALSE;
if((GetFileAttributes(lpszOutputFile) != 0xffffffff))
{
CString sMessage;
#ifdef _VER_CHINESE
sMessage.Format(_T("文件 '%s' 已经存在.\n\n是否覆盖此文件?"), lpszOutputFile);
#else
sMessage.Format(_T("The file '%s' already exists.\n\nAre you sure that you want to overwrite it?"), lpszOutputFile);
#endif
if(MessageBox(NULL, sMessage, "ProjectZip", MB_YESNO) == IDNO)
return bRtn;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -