回 帖 发 新 帖 刷新版面

主题:[原创]吐血奉献:CVF注释多行的宏命令

不止一位问道可不可以在CVF中为多行代码加注释。
CVF本身无这样的功能,但是可以用macro功能自己添!而且是添加工具钮!
俺初步试验成功,虽然在容错性方面还可以改进,但已经基本够用,特奉献给大家。

[color=FF0000]做法:[/color]

(1) 在..\Microsoft Visual Studio\Common\MSDEV98\MACROS文件夹下生成文件GrpComment.dsm
(2) 用文本编辑器打开该文件,将以下所附的代码贴在其中,保存(注意保留.dsm后缀)
(3) 启动CVF,选Tools=>Customize=>Add-ins and Macro Files
(4) 在GrpComment前打勾,去掉其他的勾
(5) 在同一对话框中选Commands=>Macros,此时在右边可以看见CommentDel和CommentOut
(6) 选中CommentOut,拖到CVF的工具栏上去(添加工具钮),会弹出Button Appearance对话框
(7) 选Image and text,在下边Button text框中输入名称(默认是CommentOut),如“加注释”
(8) 类似的方法再将CommentDel命令以工具钮的形式添加到工具栏上,名称可取为“去注释”

这时,工具栏上应该多了两个工具钮:“加注释”和“去注释”。

[color=FF0000]用法:[/color]

加注释:选择要加注释的多行代码,点击“加注释”按钮即可;
去注释:选择已经注释的多行代码,点击“去注释”按钮即可。

[color=FF0000]适用:[/color]后缀为f90或f77的代码文件。

[color=0000FF]Enjoy!!![/color]


[color=FF0000]VBscript代码:[/color]

Function FileType (ByVal doc)
    ext = doc.Name
    FileType = 0
    pos = Instr(ext, ".")
    if pos > 0 then
        Do While pos <> 1
            ext = Mid(ext, pos, Len(ext) - pos + 1)
            pos = Instr(ext, ".")
        Loop
        ext = LCase(ext)
    end if
    If ext = ".f90" Then
        FileType = 8
    ElseIf ext = ".for" Then
        FileType = 9
    Else
        FileType = 0
    End If
End Function


Sub CommentOut ()
'DESCRIPTION: 为所选的多行代码加注释
    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 = 8 Or TypeOfFile = 9 Then

            If TypeOfFile = 8 Then
                CommentType = "! "  ' Fortran 90 file
            Else
                CommentType = "C "  ' Fortran 77 file
            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.SelectLine
                ActiveDocument.Selection = CommentType + ActiveDocument.Selection

            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


Sub CommentDel ()
'DESCRIPTION: 去除所选的多行代码的注释
    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 = 8 Or TypeOfFile = 9 Then

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

回复列表 (共45个回复)

21 楼

万分感谢

22 楼

感谢lz
试过了,好用,解决了大问题。

23 楼

刚才试了一下,很好使,太感谢mltx老师了,真是解决了个大问题。
以前都是一行一行的键入注释,要是行数多的话,实在是很麻烦,这下方便多了。
再次感谢!!

24 楼

真的很感谢楼主!!!!!

25 楼

的的确确是高手!佩服的五体投地!这都能搞定。

26 楼

是啊
太强了!!

有什么办法可以选中多行?

27 楼

[quote]是啊
太强了!!

有什么办法可以选中多行?[/quote]

Shift + 箭头键
或者:
shift + 鼠标划选

28 楼

非常感谢啊!

29 楼

mltx一直是我崇拜的偶像!

对这个方法,我使用过程中发现,如果原来有顶格的注释,在用这个方法去除注释时会多去掉一个字符,如
!endif
去掉注释后会变成'ndif',少了一个字符'e',所以麻烦mltx再完善下.

30 楼

呵呵,那个注释一定是你手工加的。

俺很不喜欢“!”后直接跟文字,所以“加注释”时就多加了一个空格,“去注释”时也多删一个空格。

所以,你的注释要是用“加注释”加的,则用“去注释”去除,就没有问题。

我来回复

您尚未登录,请登录后再回复。点此登录或注册