VC6.0 中 添加/取消 块注释的Macro代码
SAMPLE.DSM是微软提供的样例,使用的是vb语言。其中的 CommentOut 函数,是支持块注释的,可是这种/**/的注释方式,有时候用起来不是很方便,因为两个/会因为一个/而终止。对于大块代码,使用//注释,仅需修改原样例函数中的少部分代码。
取消注释的实现,可以在注释的基础上进行修改。两个函数的源码如下:
Sub CommentOut ()
'DESCRIPTION: Comments out a selected block of text.
Dim win
set win = ActiveWindow
if win.type <> "Text" Then
MsgBox "This macro can only be run when a text editor window is active."
else
TypeOfFile = FileType(ActiveDocument)
If TypeOfFile > 0 And TypeOfFile < 5 Then 'C & Java use the same
'style of comments.
'在这里修改
'ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
CommentType = "//"
StartLine = ActiveDocument.Selection.TopLine
EndLine = ActiveDocument.Selection.BottomLine
For i = StartLine To EndLine
ActiveDocument.Selection.GoToLine i
ActiveDocument.Selection.SelectLine
ActiveDocument.Selection = CommentType + ActiveDocument.Selection
Next
ElseIf TypeOfFile = 5 Then
ActiveDocument.Selection = "<!-- " + ActiveDocument.Selection + " -->"
ElseIf TypeOfFile = 6 Or TypeOfFile = 7 Then
'There is no group comment like there is in the other file types,
'so we need to iterate through each line, and prepend a ' to the line.
'Also, because VBS/DEF does not have a 'end the comment at this
'particular column' delimiter, entire lines of code must be
'commented out, not sections.
If TypeOfFile = 6 Then
CommentType = " ' "
Else
CommentType = " ; "
End If
StartLine = ActiveDocument.Selection.TopLine
EndLine = ActiveDocument.Selection.BottomLine
If EndLine < StartLine Then
Temp = StartLine
StartLine = EndLine
EndLine = Temp
End If
If EndLine = StartLine Then
ActiveDocument.Selection = CommentType + ActiveDocument.Selection
Else
For i = StartLine To EndLine
ActiveDocument.Selection.GoToLine i
ActiveDocument.Selection.S1electLine
ActiveDocument.Selection = CommentType + _
ActiveDocument.Selection
Next
End If
Else
MsgBox("Unable to comment out the highlighted text" + vbLf + _
"because the file type was unrecognized." + vbLf + _
"If the file has not yet been saved, " + vbLf + _
"please save it and try again.")
End If
End If
End Sub
Sub UndoCommentOut ()
'DESCRIPTION: Comments out a selected block of text.
Dim win
set win = ActiveWindow
if win.type <> "Text" Then
MsgBox "This macro can only be run when a text editor window is active."
else
TypeOfFile = FileType(ActiveDocument)
If TypeOfFile > 0 And TypeOfFile < 5 Then 'C & Java use the same
'style of comments.
'ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
StartLine = ActiveDocument.Selection.TopLine
EndLine = ActiveDocument.Selection.BottomLine
For i = StartLine To EndLine
ActiveDocument.Selection.GoToLine i
ActiveDocument.Selection.SelectLine
'在这里修改
If(Left(ActiveDocument.Selection,2)="//") Then
ActiveDocument.Selection = Mid(ActiveDocument.Selection,3)
End If
Next
Else
MsgBox("Unable to undo comment out the highlighted text" + vbLf + _
"because the file type was unrecognized." + vbLf + _
"If the file has not yet been saved, " + vbLf + _
"please save it and try again.")
End If
End If
End Sub
最近又写了这样一个注释的代码,根据回车+//作为分隔符,直接改写选中块,添加删除都在一个函数中。只是选中时,第一行若是未从行首开始,注释//未必会在行首,难免有点美中不足
Sub Comment ( )
TmpBlock = ""
CmtBlock = ActiveDocument.Selection
TypeOfFile = FileType(ActiveDocument)
If TypeOfFile > 0 And TypeOfFile < 5 Then 'C/C++ style comment.
'Get the first two characters of the comment block.
Trim(CmtBlock)
str = vbLf + "//"
If (Left(CmtBlock,2) = "//") Then
CmtBlock = Mid(CmtBlock,3)
pos = Instr (CmtBlock,str)
If pos <> 0 Then
Do While pos <> 0
TmpBlock = TmpBlock + Left (CmtBlock, pos)
CmtBlock = Mid(CmtBlock, pos + Len(str))
pos = Instr (CmtBlock,str)
Loop
CmtBlock = TmpBlock + CmtBlock
End If
Else
CmtBlock = "//" + CmtBlock
pos = Instr (CmtBlock, vbLf)
Do While pos <> 0
TmpBlock = TmpBlock + Left (CmtBlock, pos)_
+ "//"
CmtBlock = Mid(CmtBlock, pos + 1)
pos = Instr (CmtBlock, vbLf)
Loop
CmtBlock = TmpBlock + Trim(CmtBlock)
If(Right(CmtBlock,3) = str) Then
CmtBlock = Left(CmtBlock,Len(CmtBlock) - 3)
CmtBlock = CmtBlock + vblf
End If
End If
ActiveDocument.Selection = CmtBlock
Else
MsgBox "This macro does not work on this type of file."
End If
End Sub
本文采用 知识共享署名 4.0 国际许可协议 进行许可