vc6中进行多行注释和反注释的方法

来源:互联网 发布:人力软件 编辑:程序博客网 时间:2024/04/30 07:17

来自:http://blog.csdn.net/fengbingchun/article/details/6366609

 

1、利用工具中自带的按钮实现:

Tools-->Customize-->Add-ins and Macro Files-->将SAMPLE项选中-->Commands-->Category中选择Macros-->在Commands中将CommentOut拖到工具栏,再选中一个Images,点击OK即可。

这种方法是使用/*  */进行多行注释的,而且不能全部一次取消,使用起来并不方便。

 

2、利用VBA 宏实现:

Tools-->Macro-->输入Macro Name-->Edit-->Description-->点击OK,删除刚才生成的代码,贴代码:
'多行注释
Sub Comment()
 if Documents.Count = 0 then
  exit sub
 end if 
     lTopLine = ActiveDocument.Selection.TopLine
 lBottomLine = ActiveDocument.Selection.BottomLine
 lInsertPoint = ActiveDocument.Selection.CurrentLine
For I = lTopLine To lBottomLine
  ActiveDocument.Selection.MoveTo I, 1
  ActiveDocument.Selection.SelectLine
  s = ActiveDocument.Selection.Text
  if s <> vbCrLf then
   s = "//" + vbTab + s
  end if
  ActiveDocument.Selection.Text = s
 Next
if lTopLine = lInsertPoint then
  ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine
  ActiveDocument.Selection.MoveTo lTopLine, 1, dsExtend
 else
  ActiveDocument.Selection.MoveTo lTopLine, 1
  ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine, dsExtend
 end if

End Sub
'多行反注释
Sub Uncomment()
 if Documents.Count = 0 then
  exit sub
 end if 

 lTopLine = ActiveDocument.Selection.TopLine
 lBottomLine =ActiveDocument.Selection.BottomLine
 lInsertPoint = ActiveDocument.Selection.CurrentLine
For I = lTopLine To lBottomLine
  ActiveDocument.Selection.MoveTo I, 1
  ActiveDocument.Selection.SelectLine
  s = ActiveDocument.Selection.Text
  while left(s, 1) = " " OR left(s, 1) = vbTab
   s = right(s, len(s) - 1)
  Wend
if left(s, 3) = "//" + vbTab then
   s = right(s, len(s) - 3)
  elseif left(s, 2) = "//" then
   s = right(s, len(s) - 2)
  end if

  ActiveDocument.Selection.Text = s
 Next
if lTopLine = lInsertPoint then
  ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine
  ActiveDocument.Selection.MoveTo lTopLine, 1, dsExtend
 else
  ActiveDocument.Selection.MoveTo lTopLine, 1
  ActiveDocument.Selection.MoveTo lBottomLine, dsEndOfLine, dsExtend
 end if

 ActiveDocument.Selection.SmartFormat
End Sub
关闭并保存。
Tools-->Customize-->Commands-->在Category中选择Marcos-->在右侧Commands中拖出刚才生成的两个宏名到工具条上,-->分别选择Images图标。
设置快捷键:Keyboard-->Category中选择Macros,在Commands中选择一个然后设置快捷键即可。

 

3、转自:http://blog.csdn.net/jiaolongdy/archive/2010/12/30/6106782.aspx

取消/注释的宏,可以用于C++,java,VB
VC内使用方法:
将文件格式更改为dsm,放置于Program Files/Microsoft Visual Studio/COMMON/MSDev98/Macros目录下
打开VC——Customize——add-ins and Macro files
勾选上Comment,然后选择键盘,类别里选择Macros,CustomCommentOut设置好快捷键就可以了

Sub CustomCommentOut()
'DESCRIPTION: 注释/取消注释宏,可处理VB和C++、Java注释
 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 = 3
  If TypeOfFile > 0 And TypeOfFile < 6 Then
   If TypeOfFile > 3 Then
    CommentType = "'" ' VB注释
    CommentWidth = 1
   Else
    CommentType = "//" ' C++、java 注释
    CommentWidth = 2
   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.StartOfLine dsFirstColumn
    ActiveDocument.Selection.CharRight dsExtend, CommentWidth
    If ActiveDocument.Selection = CommentType Then
     ActiveDocument.Selection.Delete
    Else
     ActiveDocument.Selection.StartOfLine dsFirstText
     ActiveDocument.Selection.CharRight dsExtend, CommentWidth
     If ActiveDocument.Selection = CommentType Then
      ActiveDocument.Selection.CharRight dsExtend
      ActiveDocument.Selection.Delete
     Else
      ActiveDocument.Selection.StartOfLine dsFirstText
      ActiveDocument.Selection = CommentType + vbTab + _
                                        ActiveDocument.Selection
     End If
    End If
   ' 多行
   Else
    For i = StartLine To EndLine
     ActiveDocument.Selection.GoToLine i
     CommentLoc = dsFirstColumn
     ActiveDocument.Selection.StartOfLine CommentLoc
     ActiveDocument.Selection.CharRight dsExtend, CommentWidth
     If ActiveDocument.Selection = CommentType Then
      ActiveDocument.Selection.Delete
     Else
      ActiveDocument.Selection.StartOfLine CommentLoc
      ActiveDocument.Selection = CommentType + _
                                                  ActiveDocument.Selection
     End If
    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

 

4、转自:http://hi.baidu.com/starloooooove/blog/item/9324343ba0364ee014cecb2a.html

打开记事本,输入如下代码:

Sub CommentBlock()
With ActiveDocument.Selection
'对于当前窗口打开的文档中选中的文本
    .ReplaceText "%", "//", dsMatchRegExpB
'在开始位置增加 // 注释
End With
End Sub

Sub UncommentBlock()
With ActiveDocument.Selection
   .ReplaceText "%//", "", dsMatchRegExpB
   '同样的,去掉在选择文本中开头找到的注释符号
End With
End Sub

安装使用这个宏文件:

1、把这个文件命名为COMMENTMAC.dsm,保存到你的VS安装目录下的O:/VS6/Common/MSDev98/Macros中;

2、在VS6的工具条上按下右键,点击Customize下

1)、ADD-INS and Macro Files属性页,选择COMMENTMAC”;

2)、选择keyboard属性页,category下选Macros,为Commands选择快捷键