vc++ 6.0 实现代码的多行注释与取消注释功能(实测有效)

人生如逆旅,我亦是行人 / 2024-09-14 / 原文

方式一

1. 新建Macro File宏文件

点击菜单栏“文件→新建(File→New)”选项,在“新建(New)”弹框中,顶部区域选项“文件(File)”下找到Macro File,点击,并在右边输入“文件名”,点击“确定(OK)”按钮。

img

此时弹出“新建宏文件”窗口,在“描述(Descripion)”输入框中输入信息,点击“确定(OK)”按钮。

此时会生成一个后缀名为.dsm格式的文件。

img

2. 添加代码多行注释/取消注释宏文件

'------------------------------------------------------------------------------
'FILE DESCRIPTION: 添加多行注释代码功能
'------------------------------------------------------------------------------
Sub SetSelNote()'Sun DESCRIPTION: 过程SetSelNote用于将选中的文本成为注释

    dim CurWin '当前获得的窗口

    set CurWin = ActiveWindow

    if CurWin.type<>"Text" Then '判断当前窗口是否是文本窗口

        MsgBox "当前窗口不是代码窗口"

    else

        NoteType = "//"

        BeginLine = ActiveDocument.Selection.TopLine

        EndLine = ActiveDocument.Selection.BottomLine

        if EndLine < BeginLine then

            Line = BeginLine
            BeginLine = EndLine

            EndLine = Line

        else

            for  row = BeginLine To EndLine

                ActiveDocument.Selection.GoToLine row

                ActiveDocument.Selection.SelectLine '选中当前行

                ActiveDocument.Selection = NoteType + ActiveDocument.Selection

            Next

        End if

    End if

End Sub

'------------------------------------------------------------------------------
'FILE DESCRIPTION: 取消代码注释
'------------------------------------------------------------------------------
Sub CancelSelNote()

    dim CurWin '当前获得的窗口

    set CurWin = ActiveWindow

    if CurWin.type<>"Text" Then '判断当前窗口是否是文本窗口

        MsgBox "当前窗口不是代码窗口"

    else

        BeginLine = ActiveDocument.Selection.TopLine

        EndLine   = ActiveDocument.Selection.BottomLine

        if EndLine < BeginLine then 

            Line = BeginLine

            BeginLine = EndLine

            EndLine = Line

        else


            for  row = BeginLine To EndLine 

                ActiveDocument.Selection.GoToLine row

                ActiveDocument.Selection.SelectLine '选中当前行

                SelBlock =  ActiveDocument.Selection

                Trim(SelBock)

                pos =  instr(SelBlock,"//")

                if pos <>0 then

                    RightBlock = Right(SelBlock,Len(SelBlock)-2)

                    ActiveDocument.Selection = RightBlock

                End If
            Next

        End if

    End if

End Sub

3. 将注释宏命令添加到工具栏中

打开菜单栏“工具→定制(Tools→Customize)”,打开定制(Customize)对话框,点击“附加项和宏文件(Add-ins and Macro Files)”选项卡,并勾选“SAMPLE”前面的复选框。

img

点击“浏览(Browse)”按钮,选择之前创建的宏文件,此时它将会出现在“附加项和宏文件(Add-ins and Macro Files)”窗口中。

img

切换到“命令(Commands)”选项卡中,在“类别(Category)”组合框中选择“Macros”选项,在右侧的列表中会显示当前宏文件中定义的命令,在命令列表中选中“SetSelNode”,将其拖动到工具栏中,此时将弹出“按钮外观(Button Appearance)”窗口。

img

在“按钮外观(Button Appearance)”窗口中选中“仅图像(Image only)”单选按钮,在“图像(Images)”群组框中为按钮选择一个图标,单击“确定(OK)”按钮完成工具栏设置。

img

关闭“定制”弹框,此时工具栏显示图标。

img

4. 重复第三步添加取消注释宏命令

打开菜单栏“工具→定制(Tools→Customize)”,打开定制(Customize)对话框,切换到“命令(Commands)”选项卡中,在“类别(Category)”组合框中选择“Macros”选项,在右侧的列表中会显示当前宏文件中定义的命令,在命令列表中选中“CancelSelNote”,将其拖动到工具栏中,此时将弹出“按钮外观(Button Appearance)”窗口。

img

在“按钮外观(Button Appearance)”窗口中选中“仅图像(Image only)”单选按钮,在“图像(Images)”群组框中为按钮选择一个图标,单击“确定(OK)”按钮完成工具栏设置。

img

关闭“定制”弹框,此时工具栏显示图标。

img

5. 测试

选中需要注释的代码块,点击“黄色笑脸”图标,发现代码块自动实现了注释。

img

再次选中需要取消注释的代码块,点击“绿色哭脸”图标,发现代码块自动取消了注释

img

方式二

1. 新建Macro File宏文件

点击菜单栏“文件→新建(File→New)”选项,在“新建(New)”弹框中,顶部区域选项“文件(File)”下找到Macro File,点击,并在右边输入“文件名”,点击“确定(OK)”按钮。

img

此时弹出“新建宏文件”窗口,在“描述(Descripion)”输入框中输入信息,点击“确定(OK)”按钮。

此时会生成一个后缀名为.dsm格式的文件。

img

2. 添加代码多行注释/取消注释宏文件

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 dsFirstColumn
                        ActiveDocument.Selection = CommentType + _
                                        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

3. 将注释宏命令添加到工具栏中

打开菜单栏“工具→定制(Tools→Customize)”,打开定制(Customize)对话框,点击“附加项和宏文件(Add-ins and Macro Files)”选项卡,并勾选“SAMPLE”前面的复选框。

img

点击“浏览(Browse)”按钮,选择之前创建的宏文件,此时它将会出现在“附加项和宏文件(Add-ins and Macro Files)”窗口中。

img

切换到“命令(Commands)”选项卡中,在“类别(Category)”组合框中选择“Macros”选项,在右侧的列表中会显示当前宏文件中定义的命令,在命令列表中选中自定义宏命令(CustomCommentOut),将其拖动到工具栏中,此时将弹出“按钮外观(Button Appearance)”窗口。

img

在“按钮外观(Button Appearance)”窗口中选中“仅图像(Image only)”单选按钮,在“图像(Images)”群组框中为按钮选择一个图标,单击“确定(OK)”按钮完成工具栏设置。

img

工具栏显示图标。通过一个图标实现代码块的注释与取消注释功能。

img

4. 测试

选中需要注释的代码块,点击“橡皮擦”图标,发现代码块自动实现了注释。

img

再次选中需要取消注释的代码块,点击“橡皮擦”图标,发现代码块自动取消了注释

img