首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

VC6 代碼格式化巨集:CodeFormat 自動排版腳本

     編輯:VC 教學

緣起與目的

作者未學過 VBScript,但為了格式化自用代碼,參考現有資源勉強撰寫了此巨集。其核心價值在於:在 VC6 這個受信任的開發環境中進行格式化,絕不會導致代碼無法編譯。

使用方法

  1. 將下列代碼儲存為 .DSM 副檔名的檔案。
  2. 在 VC6 中載入該巨集檔。
  3. 開啟任一 C/C++ 原始碼檔案。
  4. 執行巨集,代碼將依系統設定(空白鍵或 Tab)自動格式化。

已知限制

  • 必須在 VC 環境中手動開啟檔案,不支援目錄遞迴批次處理。
  • 若 { 後同行有字元,大括號內部程式碼會遺失縮排。
  • 檔案必須可寫入。

巨集原始碼

Sub FormatByCode()
    Dim myDocument
    For Each myDocument in Application.Documents
        myDocument.Active = True
        myDocument.Selection.SelectAll
        Dim EndLine, CurrLine
        EndLine = myDocument.Selection.BottomLine
        myDocument.Selection.StartOfLine
        CurrLine = myDocument.Selection.CurrentLine
        While (CurrLine <= EndLine)
            myDocument.Selection.SelectLine
            myDocument.Selection = LTrim(myDocument.Selection)
            CurrLine = myDocument.Selection.CurrentLine
        Wend
        myDocument.Selection.SelectAll
        myDocument.Selection.Unindent
        myDocument.Selection.Unindent
        myDocument.Selection.Unindent
        myDocument.Selection.Unindent
        myDocument.Selection.Unindent
        myDocument.Selection.Unindent
        myDocument.Selection.SmartFormat
        myDocument.Save
        myDocument.Close dsSaveChangesPrompt
    Next
End Sub

期望改進

作者誠摯希望有技術高手能完善此巨集(支援批次處理、修正縮排邏輯等),若有新版本請寄送至 Yanggr@cbcyber.com。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved