这个宏的原作者不得知,转载留作备份。
- Sub SyntaxHighlight()
- '
- ' SyntaxHighlight Macro
- ' 宏在 2003-4-24 由 yu 创建
- ' 修改 2008-12-08 由 wt.yu
- '
- 'Customization goes here
- Dim keywords, sysclasses
- keywords = Array("abstract", "asm", "auto", "bool", "boolean", "break", "byte", "case", "cast", "catch", "char", _
- "class", "const", "continue", "default", "delete", "do", "double", "dynamic_case", "else", "enum", "explicit", "export", "extern", "extends", "false", "final", _
- "finally", "friend", "float", "for", "goto", "if", "inline", "implements", "import", "instanceof", "inner", "int", _
- "interface", "long", "native", "new", "null", "operator", "package", "private", "protected", "public", "return", _
- "short", "sigend", "static", "static_cast", "struct", "super", "switch", "synchronized", "template", "this", "throw", "throws", "transient", "true", _
- "try", "typedef", "unsigned", "union", "using", "virtual", "void", "volatile", "while", "include", "std")
- sysclasses = Array("System", "String", "StringBuffer", "Runnable", "Thread", "Exception", "IOException", "cout", "cin", "std", "endl", "vector")
- MFCclasses = Array("CWnd", "HWND", "CRect", "HDC", "CDialog", "BOOL", "TRUE")
- b = Selection.Start
- E = Selection.End
- tstr = Selection.Text
- ll = Len(tstr)
- pos = 1
- c = ""
- sb = 0
- se = 0
- While (pos < ll)
- c = Mid(tstr, pos, 1)
- Select Case c
- Case "/"
- If Mid(tstr, pos + 1, 1) = "/" Then 'Comments
- sb = pos
- se = InStr(pos, tstr, vbCr)
- Selection.Start = b + sb - 1
- Selection.End = b + se - 1
- Selection.Font.Italic = True
- Selection.Font.Color = wdColorTeal
- pos = se + 1
- Else
- pos = pos + 1
- End If
- Case """"
- sb = pos
- se = InStr(pos + 1, tstr, """")
- Selection.Start = b + sb - 1
- Selection.End = b + se
- Selection.Font.Color = wdColorGray45
- pos = se + 1
- Case "a" To "z", "A" To "Z"
- sb = pos
- While (c <> " ") And (c <> "(") And (c <> ")") And (c <> "{") And (c <> "}") And (c <> vbCr) _
- And (c <> ".") And (c <> ";")
- pos = pos + 1
- c = Mid(tstr, pos, 1)
- Wend
- w = Mid(tstr, sb, pos - sb)
- For i = 0 To UBound(keywords)
- If w = keywords(i) Then
- Selection.Start = b + sb - 1
- Selection.End = b + sb + Len(w) - 1
- Selection.Font.Bold = True
- Selection.Font.Color = wdColorBlue
- End If
- Next
- For i = 0 To UBound(sysclasses)
- If w = sysclasses(i) Then
- Selection.Start = b + sb - 1
- Selection.End = b + sb + Len(w) - 1
- Selection.Font.Color = wdColorRed
- End If
- Next
- For i = 0 To UBound(MFCclasses)
- If w = MFCclasses(i) Then
- Selection.Start = b + sb - 1
- Selection.End = b + sb + Len(w) - 1
- Selection.Font.Color = wdColorGreen
- End If
- Next
- pos = pos + 1
- Case Else
- pos = pos + 1
- End Select
- Wend
- End Sub