最近在工作中,需要在详细设计文档中插入伪代码。尝试编辑了不同的格式,总是觉得代码显示的不美观,因此尝试在网上搜索一些方法,还真有这方面的资料。如下
- 一、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
有不同看法和好的建议的,可以留言沟通。