Visual Studio 自动添加函数注释宏

本文介绍了如何在C++环境下通过宏实现自动为函数添加注释的功能,包括函数名、参数列表、返回类型等信息,并展示了使用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在.net环境下,按下三个/// 就可以自动添加函数注释,包括函数名、参数列表、返回值等信息。

在C++环境下通过MACRO宏也可以实现这个功能,宏代码如下:


Option Strict Off
Option Explicit Off
Imports EnvDTE
Imports System.Diagnostics

Public Module Comment

    Sub FILE()
        DTE.ActiveDocument.Selection.Text = "/******************************************************************************"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Indent()
        DTE.ActiveDocument.Selection.Text = "文件名称 : " + DTE.ActiveDocument.Name.ToString()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "作    者 : "
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "创建时间 : " + System.DateTime.Now.ToLocalTime()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "文件描述 :"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "版权声明 : Copyright(C) 2008-2012"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "修改历史 : N/A"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.DeleteLeft()
        DTE.ActiveDocument.Selection.Text = "*******************************************************************************/"
    End Sub

    Sub FUN()
        Dim projectItem As ProjectItem = DTE.ActiveDocument.ProjectItem
        Dim fileCodeMode As FileCodeModel = projectItem.FileCodeModel
        Dim selectItem As TextSelection = DTE.ActiveDocument.Selection      '当前选定内容
        Dim point As TextPoint = selectItem.ActivePoint                     '读取当前位置
        Dim codeElement As CodeElement = fileCodeMode.CodeElementFromPoint(point, vsCMElement.vsCMElementFunction)

        Dim nCount As Integer = 0

        If (codeElement Is Nothing) Then
            Return                                  '若不是函数名,直接返回
        Else
            DTE.ActiveDocument.Selection.MoveToPoint(codeElement.StartPoint)
            DTE.ActiveDocument.Selection.Text = "/******************************************************************************"
            nCount = codeElement.Children.Count     '参数数目
        End If


        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.DeleteWhitespace()
        DTE.ActiveDocument.Selection.Text = "描述 : "
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "参数 : "

        For i = 1 To nCount
            DTE.ActiveDocument.Selection.Indent()
            DTE.ActiveDocument.Selection.Text = codeElement.Children.Item(i).Name
            DTE.ActiveDocument.Selection.Text = ("  --- 说明")

            If (i <> nCount) Then
                DTE.ActiveDocument.Selection.NewLine()
                DTE.ActiveDocument.Selection.DeleteWhitespace()
                DTE.ActiveDocument.Selection.Indent(1)
            End If
        Next

        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.DeleteWhitespace()
        DTE.ActiveDocument.Selection.Text = "返回 : "
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.DeleteWhitespace()
        DTE.ActiveDocument.Selection.Text = "*******************************************************************************/"
        DTE.ActiveDocument.Selection.NewLine()
    End Sub
End Module

使用效果如下:

/******************************************************************************
描述 : 
参数 :	filename  --- 说明
	data  --- 说明
	dataSize  --- 说明
返回 : 
*******************************************************************************/
bool ReadTGA(const char *filename, byte* data, int dataSize)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值