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

VC6.0 中 添加/取消 块注释的Macro代码的更多相关文章

  1. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  2. 在VC6.0中能不能使用Duilib界面库呢?

    Duilib库的源代码是在vs2010下编译的,一般适用于vs2008及以上的版本开发使用,那么duilib能不能在vc6.0的工程中使用呢?如何在vc6.0中使用duilib库呢? 今天,由于工作要 ...

  3. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  4. VC++ 6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在& ...

  5. Notepad++怎么增加整行删除快捷键?添加/取消区块注释?

    1.有网友说 Notepad++ 自带删除行功能,用快捷键 Ctrl+L 就可以删除一行.的确,这个快捷键是可以删除一行文本,但确切的说,Ctrl+L 并不是仅仅删除了一行文本,而是 剪切 了一行文本 ...

  6. VC 6.0中添加库文件和头文件 【转】

    本文转载自:http://blog.sina.com.cn/s/blog_9d3971af0102wxjq.html 加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools&g ...

  7. VC6.0中友元函数无法访问类私有成员的解决办法

    举个例子: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #inclu ...

  8. [置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

    c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date' ...

  9. Service 中添加同步块防止并发 重复

    Service 中添加同步块防止并发 重复. synchronized(this){}

随机推荐

  1. (转)Inno Setup入门(八)——有选择性的安装文件

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250827 这主要使用[Components]段实现,一个演示的代 ...

  2. HttpURLConnection的使用步骤

    创建一个URL对象: URL url = new URL(http://www.baidu.com); 调用URL对象的openConnection( )来获取HttpURLConnection对象实 ...

  3. apktool重新打包错误

    E:\apktool-install-windows-r05-ibot\apktool-install-windows-r05-ibot\.\test_apk_name\res\layout-larg ...

  4. eclipse安装freemarker插件【转】

    今天在Eclipse上安装Freemarker的插件,一开始装官方网站上的推荐插件,装上后发现除了Freemarker代码高亮显示其他什么效果都没有,郁闷.在javaeye论坛上请教了下,据说官网上的 ...

  5. postman 设置代理

    点击右上角 图标(亮着的为录制中) 设置端口 和存放位置 把浏览器设置代理 localhost 8080 即可 filter中可以通过正则表达式来匹配自己关心的url     2018.9 后记: 今 ...

  6. 如何扩大重做日志(redolog)文件的大小

    假设现有三个日志组,每个组内有一个成员,每个成员的大小为1MB,现在想把此三个日志组的成员大小都改为10MB 1.创建2个新的日志组alter database add logfile group 4 ...

  7. 十年Java架构师分享

    看到一篇十年java架构师分享需要掌握的技术点,有时间对照一下,好好学习一下. ------------------------------------------------------------ ...

  8. HTML 空白

    HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  9. spring-boot-actuator健康监控

    #健康监控 management.security.enabled=false health.mail.enabled =false http://localhost:54001/autoconfig ...

  10. Console2支援中文顯示的正式設定法

    1.用regedit找到HKEY_CURRENT_USER\Console,把底下的Console2 command window機碼給砍了.2.Console2的View功能表中,有個Console ...