在word中插入格式漂亮的代碼

最近在工作中,需要在詳細設計文檔中插入僞代碼。嘗試編輯了不同的格式,總是覺得代碼顯示的不美觀,因此嘗試在網上搜索一些方法,還真有這方面的資料。如下

  • 一、CSDN的博文《如何優雅的在 Microsoft word中插入代碼》提到使用一個在線的網站PlanetB對代碼進行格式化,該網站確實效果不錯。但因爲是在線的方式進行格式化,因此可能存在代碼泄露的問題。
  • 二、在百度經驗中有一個方法是使用word 的腳步實現格式化代碼,鏈接地址是:word中插入漂亮的代碼。該方法親測是可用的,就是需要自己先創建一個“ccode”的樣式,在執行其中的腳步程序,並且格式化後顯示的效果不如第一個方法明顯。

基於以上兩種情況,決定自己設計一個格式化腳步的。本格式分爲兩部分進行:

  • 在Notepad++中編輯好需要顯示的程序或者僞代碼,注意文件的格式最好設置成.c 或者.h等,便於Notepad識別出關鍵詞,進行關鍵詞的顯示(關鍵詞自動有各種不同顏色進行顯示)。選中所需要的內容,依次完成“插件”->“NppExport”->“Copy HTML to clipboard”.
Uint8_t GetChar(UART_PACKGE *x,uint8_t *pData)
	if pData 爲空 or x 爲空 or x->u8EmptyFlag 爲 true then
		return false
	else
		*pData ← x->pDataBuffer[x->u8HeadIndex++]
		if x->u8HeadIndex >= TX_BUFFER_LEN then
			x->u8HeadIndex ← 0
		if x->u8HeadIndex == x->u8TailIndex
			x->u8EmptyFlag ← true
			x->u8FullFlag ← false
		return true
  • 在word中粘貼上述步驟拷貝的內容,並選中所有內容,並執行如下步驟的宏腳本(文末列出),即可以得到最終的效果的代碼。

在實際格式化的時候,也可以直接進行第2步,但這會導致無關鍵詞的高亮顯示。

上述步驟中描述的VB腳步如下:

Sub formatCode()
'
' formatCode 宏
'
'

    With Selection.ParagraphFormat
        With .Borders(wdBorderLeft)
            .LineStyle = wdLineStyleSingle
            .LineWidth = wdLineWidth300pt
            .Color = 4185357
        End With
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
        With .Borders
            .DistanceFromTop = 1
            .DistanceFromLeft = 0
            .DistanceFromBottom = 1
            .DistanceFromRight = 4
            .Shadow = False
        End With
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth300pt
        .DefaultBorderColor = 4185357
    End With
    With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingSpace
        
        'wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = CentimetersToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(0.74)
        .TabPosition = wdUndefined
        .ResetOnHigher = 0
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = ""
    End With
    ListGalleries(wdNumberGallery).ListTemplates(1).Name = ""
    Selection.Range.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
        False, ApplyTo:=wdListApplyToSelection, DefaultListBehavior:= _
        wdWord10ListBehavior
    LineNum = Selection.Range.Sentences.Count
    For i = 0 To LineNum - 1 Step 1
        Selection.HomeKey Unit:=wdLine, Extend:=None
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        Call changeListLevel
        Selection.MoveDown Unit:=wdLine
    Next i    
End Sub




Sub changeListLevel()
'
' change List level
'
'
'    Selection.Range.SetListLevel Level:=1

    CurList = Selection.Range.ListFormat.ListLevelNumber
    If CurList > 1 Then
       Selection.Range.SetListLevel Level:=1
       Selection.HomeKey Unit:=wdLine
       For i = 0 To 2 * (CurList - 2) - 1 Step 1
           Selection.InsertSymbol CharacterNumber:=160, Unicode:=True, Bias:=0
       Next i
    End If
    
End Sub

有不同看法和好的建議的,可以留言溝通。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章