1. 程式人生 > >VC6.0 中 新增/取消 塊註釋的Macro程式碼

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

2013-07-13 17:08