65.9K
CodeProject 正在变化。 阅读更多。
Home

VC6 的自动生成增量器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.77/5 (6投票s)

2000年7月12日

viewsIcon

87226

downloadIcon

445

一篇关于如何为 VC6 添加自动生成增量器的文章

  • 下载源代码文件 - 2 Kb
  • 引言

    几周前,我开始尝试寻找一种方法,以便在每次编译时自动递增构建号。我找到了一些关于这个主题的文章和一些源代码,但没有一个完全符合我的要求,所以我决定编写我自己的递增器。

    将代码添加到现有的宏文件中,或者,如果您没有现有的宏文件,则创建一个并添加一个虚拟宏,以便您可以访问代码片段。Application_BeforeBuildStart() 是 Visual Studio 定义的事件处理程序,虽然代码在宏文件中,但当您选择“工具|宏”菜单项时,您将看不到它,因此您至少需要在文件中添加一个虚拟宏才能访问源代码。

    请注意,这些代码片段经过了有限的测试,您使用它们需要自行承担风险。它们在测试环境中工作正常,但您将承担因其在您的环境中无法正常工作而产生的所有风险。我不会承担任何因其无法正常工作而产生的任何责任。

    请将任何评论、改进建议、问题等通过电子邮件发送给我。

    Sub Application_BeforeBuildStart()
       ' written 7/11/00 by Curt Blackmon
       ' e-mail any comments, corrections, etc. to curtblackmon@home.com
       ' updated versions will be at www.ccbcon.com on the Code Snippets page
       ' invoked automatically before build starts
       ' will open the project's *.rc file as text
       ' will search for the version info
       ' and will increment the build number
    
       dim oDoc
       dim sBuild
       dim sRCFile
       dim sOne
       dim sTwo
       dim iSelect
       dim lLineNbr
       dim bFound
    
       'get the name of the active project's .rc file
       sRCFile = application.activeproject & ".rc"
    
       'open *.rc file as text
       set oDoc = Documents.Open(sRCFile, "Text")
    
       'position to the correct section of the file
       oDoc.Selection.FindText " FILEVERSION", dsMatchCase
       'save the line number for the next search
       lLineNbr = oDoc.Selection.CurrentLine
    
       'use a regular expression search string for the first version search
       sOne = "[0-9]+,[0-9]+,[0-9]+,"
    
       'find the first string
       oDoc.Selection.FindText sOne, dsMatchRegExp
    
       if oDoc.Selection.CurrentLine = lLineNbr then
          'convert the regular expression to an absolute search string
          sOne = oDoc.Selection
          'build an absolute search string for the strings with embedded spaces
          sTwo = Replace(sOne, ",", ", ")
          'move to the build number
          oDoc.Selection.CharRight
          'select the build number
          oDoc.Selection.WordRight dsExtend
          'increment the build number
          sBuild = oDoc.Selection + 1
          'replace the old build number with the new one
          oDoc.Selection = sBuild
       else 'something went wrong
          msgbox "Version number 1 not found. Closing without changes."
          oDoc.Close dsSaveChangesNo
          set oDoc = nothing
          exit sub
       end if
       
       'now change the other 3 occurences of the build number
       for iSelect = 2 to 4
          if iSelect = 2 then
             bFound = oDoc.Selection.FindText(sOne)
          else
             bFound = oDoc.Selection.FindText(sTwo)
          end if
          oDoc.Selection.CharRight
          oDoc.Selection.WordRight dsExtend
          oDoc.Selection = sBuild
          if bFound = False then
             msgbox "Version number " & iSelect & " not found. Closing without changes."
             oDoc.Close dsSaveChangesNo
             set oDoc = nothing
             exit sub
          end if
       next
    
       'close and save *.rc
       oDoc.Close dsSaveChangesYes
       set oDoc = nothing
    End Sub
    


    © . All rights reserved.