a) 自動添加文件註釋 效果如下:
- /*
- * Copyright(C) 2012,Company 保留所有權利。( All rights reserved. )
- *
- * 文件名稱:TEDlg.cpp
- * 摘 要:
- * 當前版本:1.0
- * 作 者:YuJian
- * 創建日期:2012年3月18日
- */
b) 自動添加函數註釋 效果如下:
- //-----------------------------------------------------------
- //
- // 函數名稱:CTEDlg::OnInitDialog
- //
- // 參 數: -無
- //
- // 返 回:BOOL
- //
- // 函數作用:
- //
- // 修改日期:2012年3月18日 By YuJian
- //
- //-----------------------------------------------------------
所使用的途徑 是:編寫宏
而在,編寫宏之前,要給VS2005安裝補丁文件 ,否則 vs2005無法執行宏
英文補丁 431M
安裝好了以後,就可以編寫自動註釋函數的宏了。。。
由於是VB,以前沒用過,因此借鑑網上的程序,自己根據需要稍微修改了下
1 建立宏項目
工具→宏→新建宏項目,輸入文件名稱,並選擇要保存的位置
將下面的代碼複製到Module1模塊,保存。
- Imports System
- Imports EnvDTE
- Imports EnvDTE80
- Imports System.Diagnostics
- Public Module Module1
- ' --------------------------------------------------
- ' 生成文件說明註釋
- ' --------------------------------------------------
- Sub FileDescription()
- Dim gAuthor As String = "YuJian"
- Dim gCompany As String = "Company"
- Dim outTextDoc As TextDocument
- Dim outText As EditPoint
- DTE.ActiveDocument.Selection.GotoLine(1)
- outTextDoc = DTE.ActiveDocument.Object("TextDocument")
- outText = outTextDoc.StartPoint.CreateEditPoint()
- outText.Insert("/*" + vbCrLf)
- outText.Insert(" * Copyright(C) " + Date.Today.Year.ToString() + "," + gCompany + " 保留所有權利。( All rights reserved. )" + vbCrLf)
- outText.Insert(" * " + vbCrLf)
- outText.Insert(" * 文件名稱:" + DTE.ActiveDocument.Name + vbCrLf)
- outText.Insert(" * 摘 要:" + vbCrLf)
- outText.Insert(" * 當前版本:1.0" + vbCrLf)
- outText.Insert(" * 作 者:" + gAuthor + vbCrLf)
- outText.Insert(" * 創建日期:" + Date.Today.ToLongDateString() + vbCrLf)
- outText.Insert(" */" + vbCrLf)
- DTE.ActiveDocument.Selection.GotoLine(10)
- End Sub
- ' --------------------------------------------------
- ' 生成函數說明註釋
- ' --------------------------------------------------
- Sub FunctionRemark()
- Dim preSpaceCount As Integer = 0 ' 註釋前面的空格數, 縮進(單位:字符)
- Dim outTextDoc As TextDocument
- Dim outText As EditPoint
- Dim iCurrentLineNumber As Integer
- iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine '.ToString()
- outTextDoc = DTE.ActiveDocument.Object("TextDocument")
- outText = outTextDoc.StartPoint.CreateEditPoint()
- ' 移動文本輸入點到指定行上
- outText.MoveToLineAndOffset(iCurrentLineNumber, 1)
- Dim strSpace As String = ""
- Dim iSpaceIndex As Integer
- For iSpaceIndex = 1 To preSpaceCount
- strSpace = strSpace + " "
- Next
- outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)
- outText.Insert(strSpace + "// 函數名稱:" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "// 參數:" + vbCrLf)
- outText.Insert(strSpace + "// - " + vbCrLf)
- outText.Insert(strSpace + "// 返回:" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "// 說明:" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)
- End Sub
- ' --------------------------------------------------
- '
- ' 函數註釋解析部分
- '
- ' --------------------------------------------------
- Public Structure ITEMDATA
- Public itemType As Integer
- Public itemText As String
- End Structure
- Public lItemList As New System.Collections.Generic.List(Of ITEMDATA)(4)
- Private Function ParseFunctionDescription(ByVal funText As String) As Boolean
- Dim strItem As String
- Dim idata As ITEMDATA
- Dim strSplit As String() = funText.Split("(")
- If strSplit.Length = 1 Then
- Return False
- End If
- '解析函數名稱部分
- If strSplit.Length > 2 Then
- strItem = strSplit(strSplit.Length - 2).Trim()
- Else
- strItem = strSplit(0).Trim()
- End If
- Dim strHeadSplit As String() = strItem.Split(" ")
- strItem = strHeadSplit(strHeadSplit.Length - 1).Trim()
- idata.itemType = 1
- idata.itemText = strItem.Trim()
- lItemList.Add(idata)
- '解析參數部分
- strItem = strSplit(strSplit.Length - 1).Trim()
- If strItem.Substring(0, 1) <> ")" Then
- Dim iend As Integer = strItem.IndexOf(")", 0)
- Dim strParams As String = strItem.Substring(0, iend).Trim()
- Dim strParamSplit As String() = strParams.Split(",")
- For Each strItem In strParamSplit
- idata.itemType = 2
- idata.itemText = strItem.Trim()
- lItemList.Add(idata)
- Next strItem
- Else
- idata.itemType = 2
- idata.itemText = "無參數"
- lItemList.Add(idata)
- End If
- '解析返回值類型
- Dim iIndex As Integer
- For iIndex = 0 To strHeadSplit.Length - 2
- idata.itemType = 3
- idata.itemText = strHeadSplit(iIndex).Trim()
- lItemList.Add(idata)
- Next iIndex
- Return True
- End Function
- ' --------------------------------------------------
- '
- ' 根據函數聲明生成註釋
- '
- ' --------------------------------------------------
- Sub MakeFunctionRemark()
- Dim preSpaceCount As Integer = 0 ' 註釋前面的空格數, 縮進(單位:字符)
- Dim outTextDoc As TextDocument
- Dim outText As EditPoint
- Dim iCurrentLineNumber As Integer
- Dim iLineLength As Integer
- Dim strFunText As String
- Dim iItemIndex As Integer
- Dim idata As ITEMDATA
- lItemList.Clear()
- iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine.ToString()
- outTextDoc = DTE.ActiveDocument.Object("TextDocument")
- outText = outTextDoc.StartPoint.CreateEditPoint()
- ' 移動文本輸入點到指定行上
- outText.MoveToLineAndOffset(iCurrentLineNumber, 1)
- iLineLength = outText.LineLength
- strFunText = outText.GetText(iLineLength)
- iLineLength = strFunText.Trim().Length
- '但前行沒有內容直接返回
- If iLineLength = 0 Then
- Return
- End If
- ' 解析函數名稱
- Dim bResult As Boolean = ParseFunctionDescription(strFunText.Trim())
- If bResult = False Then
- lItemList.Clear()
- Return
- End If
- Dim pcount As Integer = 0
- Dim rcount As Integer = 0
- Dim strSpace As String = ""
- Dim iSpaceIndex As Integer
- For iSpaceIndex = 1 To preSpaceCount
- strSpace = strSpace + " "
- Next
- outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)
- For iItemIndex = 0 To lItemList.Count - 1
- idata = lItemList.Item(iItemIndex)
- Select Case idata.itemType
- Case 1
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "// 函數名稱:" + idata.itemText + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- Case 2
- If idata.itemText = "無參數" Then
- outText.Insert(strSpace + "// 參 數: -無" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- Else
- If pcount = 0 Then
- outText.Insert(strSpace + "// 參 數:" + vbCrLf)
- End If
- pcount = pcount + 1
- outText.Insert(strSpace + "// - " + idata.itemText + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- End If
- Case 3
- If rcount = 0 Then
- outText.Insert(strSpace + "// 返 回:" + idata.itemText + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- End If
- rcount = rcount + 1
- Case 4
- Case 5
- End Select
- Next
- outText.Insert(strSpace + "// 函數作用:" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "// 修改日期:" + Date.Today.ToLongDateString() + " By YuJian" + vbCrLf)
- outText.Insert(strSpace + "// " + vbCrLf)
- outText.Insert(strSpace + "//-----------------------------------------------------------" + vbCrLf)
- lItemList.Clear() '清楚所有元素
- End Sub
- End Module
將宏項目加載後, 在宏資源器中,便會發現該項目有三個宏方法:
FileDescription : 是爲整個文件添加註釋
FunctionRemark: 爲函數添加註釋------(很粗糙的 信息需要自己輸入,這是中間函數 )
MakeFunctionRemark: 自動爲函數添加註釋
我們需要的是 FileDescription 和MarkFunctionRemark這兩個宏函數。
分別雙擊它們,便可建立文件註釋和函數註釋。 不過,建立函數註釋前, 光標必須位於函數定義處。
c) 對語句進行說明
- /****************************** add by yujian 2012年5月8日 10:19 *************************************/
- if (FontType&RASTER_FONTTYPE)
- {
- return TRUE;
- }
- /****************************** add by yujian 2012年5月8日 10:19 *************************************/
代碼:
- ' --------------------------------------------------
- ' 生成函數語句說明註釋
- ' --------------------------------------------------
- Sub FunctionRemark()
- Dim preSpaceCount As Integer = 0 ' 註釋前面的空格數, 縮進(單位:字符)
- Dim outTextDoc As TextDocument
- Dim outText As EditPoint
- Dim iCurrentLineNumber As Integer
- ' iCurrentLineNumber = DTE.ActiveDocument.Selection.CurrentLine '.ToString()
- iCurrentLineNumber = DTE.ActiveDocument.Selection.TopLine
- outTextDoc = DTE.ActiveDocument.Object("TextDocument")
- outText = outTextDoc.StartPoint.CreateEditPoint()
- ' 移動文本輸入點到指定行上
- outText.MoveToLineAndOffset(iCurrentLineNumber - 1, 1)
- Dim strSpace As String = ""
- Dim iSpaceIndex As Integer
- For iSpaceIndex = 1 To preSpaceCount
- strSpace = strSpace + " "
- Next
- outText.Insert(strSpace + "/****************************** " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + " *************************************/" + vbCrLf)
- ' outText.Insert(strSpace + vbCrLf)
- ' outText.Insert(strSpace + "/****************************** " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + " *************************************/" + vbCrLf)
- iCurrentLineNumber = DTE.ActiveDocument.Selection.BottomLine + 1
- ' 移動文本輸入點到指定行上
- outText.MoveToLineAndOffset(iCurrentLineNumber + 1, 1)
- outText.Insert(strSpace + "/****************************** " + " add by yujian " + Date.Today.ToLongDateString() + " " + Date.Now.Hour.ToString + ":" + Date.Now.Minute.ToString + " *************************************/" + vbCrLf)
- End Sub
2 建立快捷鍵
在源項目工程中【不是在宏項目中】 依次選擇
工具→選項→環境→鍵盤
設置用來運行宏的快捷鍵,注意不要和已有的快捷鍵相沖突。
比如: 建立文件註釋 快捷鍵爲: ALT+A
建立函數註釋 快捷鍵爲: ALT+R
參考資料:
http://z.chao.yue.blog.163.com/blog/static/13086262020114210373211/
http://www.cppblog.com/eday/archive/2007/07/15/28079.html
http://pppboy.blog.163.com/blog/static/3020379620091030111611858/