详解VC6.0中自己手动实现代码注释和取消注释的工具栏按钮功能

VC6.0的工具栏中没有注释代码和取消注释的按钮,下面详细介绍如何自己手动实现这个两个功能,并将该两个功能放在工具条上,方便使用。

一、注释代码

打开VC6.0,选择菜单Tools-->Macro在出现的对话框中点击:Options,选择Load files

在出现的窗口中勾选Sample,关闭。

再次打开Tools-->Macro在出现的对话框

选中SAMPLE,以及列表框中的CommentOut,点击Options,点击Toolbars,出现如下对话框

左键按住对话框中的CommentOut,并拖放到工具栏上,然后松开鼠标左键,出现如下对话框,

选中 Image and text,在下面图标中选择一个图标,点击 OK。可以看到工具栏上出现如下按钮:

这样就实现了注释的工具条按钮,选中相应代码,点击此注释按钮即可注释代码内容。

二、取消注释

打开Tools-->Macro在出现的对话框

在Macro File:列表框中选择中SAMPLE,点击Record,在弹出的新对话框中填写两个CommentIn,点击OK。

出现下面的小窗口,

点击小窗口上的Stop按钮,转到宏代码编辑窗口,如下:

将该宏代码改成如下内容:

Sub CommentIn()
'DESCRIPTION: CommentIn

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.
   CommentType = "//"
    
   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.GoToLine StartLine
    ActiveDocument.Selection.SelectLine
ActiveDocument.Selection = Replace(ActiveDocument.Selection,"//","",1,1)
    ActiveDocument.Selection.GoToLine StartLine
    'ActiveDocument.Selection.SelectLine
    'If Left(ActiveDocument.Selection,2) = "//" Then
    ' ActiveDocument.Selection = Mid(ActiveDocument.Selection,3)'CommentType + ActiveDocument.Selection
    ' ActiveDocument.Selection.GoToLine StartLine
    ' ActiveDocument.Selection.SelectLine
    'End If
   Else
    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
     ActiveDocument.Selection.GoToLine EndLine
     ActiveDocument.Selection.SelectLine
    Next
   End If
  End If
End If

'Begin Recording
'End Recording
End Sub

然后按CTRL + S保存。

查找Sub 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.
   CommentType = "//"
    
   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
    ActiveDocument.Selection.GoToLine StartLine
    'ActiveDocument.Selection.SelectLine

   Else
    For i = StartLine To EndLine
     ActiveDocument.Selection.GoToLine i
     ActiveDocument.Selection.SelectLine
     ActiveDocument.Selection = CommentType + _
      ActiveDocument.Selection
    Next
   End If
   'ActiveDocument.Selection = "/*" + ActiveDocument.Selection + "*/"
  ElseIf TypeOfFile = 5 Then
   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
    ActiveDocument.Selection.GoToLine StartLine
    ActiveDocument.Selection.SelectLine

   Else
    For i = StartLine To EndLine
     ActiveDocument.Selection.GoToLine i
     ActiveDocument.Selection.SelectLine
     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

按CTRL + S保存。

再次打开Tools-->Macro在出现的对话框

选中SAMPLE,选中CommentIn,点击Options,点击Toolbars,出现如下对话框:

按住CommentIn拖放到工具栏上,于是即实现工具栏的注释与取消注释按钮。

你可能感兴趣的:(IDE,工具,file,comments,java,types,image)