作者:Walkdan(walkdan<at>gmail.com), http://www.cnblogs.com/walkdan
虽然白底具有最丰富的视觉效果,不过我并不喜欢用白底的编辑器,原因只有一个,保护视力。由于黑色没有光线,而白色包含全部光线,所以黑色对眼睛没有刺激,而白色却具有最强的刺激。对于需要长时间盯住屏幕的程序员来说,白色无疑是对眼睛的最大伤害。以前用Delphi编程一直不觉得眼睛累,因为Delphi提供了蓝底的Classic配色方案和黑底的Twilight方案,我更喜欢用Twilight,曾经有段时间看不到黑底,便没有编程的感觉,真是很怀念Delphi。
Visual Studio编辑器很不错,也可以修改配色,到了2005版,更是提供了一百多配色项,但问题是为什么就不弄几个配色方案,这一百多项得你自己慢慢改。
这天眼睛总于受不了,于是用VB.NET写了个简单的宏VS StyleManager,装进Visual Studio中,可以方便的保存和恢复配色方案。同时参考Delphi,写了相应的Classic和Twilight配色方案,现在我又可以用Twilight了,终于能够拯救眼睛了!
现在把宏分享出来,希望能对各位同行的眼睛有所帮助。
Classic方案
Twilight方案
Presentation方案。如果经常做演示,大字体是需要的。
thinkinnight同学提供的vi darkblue方案。
'' Save & Load ColorAndFont Style for Visual Studio 2003, 2005
''
'' Copyright 2006 Qiu Dan (walkdan(at)gmail.com)
'' http://www.cnblogs.com/walkdan
''
'' This program is free software; you can redistribute it and/or
'' modify it under the terms of the GNU General Public License
'' as published by the Free Software Foundation; either version 2
'' of the License, or (at your option) any later version.
Option Strict Off
Option Explicit Off
Imports System
Imports System.IO
Imports System.Text
Imports System.Collections
Imports System.Xml
Imports System.Drawing
Imports System.Windows
Imports System.Windows.Forms
Imports EnvDTE
'8 for Visual Studio 2005, 7 for Visual Studio 2003
#Const VS_VERSION = 8
Public Module StyleManagerModule StyleManager
''--------------------------------------------------------------
''StyleItem
Public Class StyleItemClass StyleItem
Public Sub New()Sub New()
'Me.New("", 1, 1, False)
End Sub
Public Sub New()Sub New(ByVal name As String, ByVal foreground As UInt32, ByVal background As UInt32, ByVal bold As Boolean)
Me.Name = name
Me.Foreground = ColorTranslator.FromOle(Convert.ToInt32(foreground))
Me.Background = ColorTranslator.FromOle(Convert.ToInt32(background))
Me.Bold = bold
End Sub
Public Shared Function DeSerializeFromXml()Function DeSerializeFromXml(ByVal node As XmlNode) As StyleItem
Dim item As New StyleItem
item.Name = node.Attributes.ItemOf("name").Value
Try
item.Foreground = ColorTranslator.FromHtml(node.Attributes.ItemOf("foreground").Value)
item.Background = ColorTranslator.FromHtml(node.Attributes.ItemOf("background").Value)
item.Bold = Boolean.Parse(node.Attributes.ItemOf("bold").Value)
Catch
End Try
Return item
End Function
Public Sub SerializeToXml()Sub SerializeToXml(ByVal writer As XmlTextWriter)
writer.WriteStartElement("FontColorItem")
writer.WriteAttributeString("name", Me.Name)
writer.WriteAttributeString("foreground", ColorTranslator.ToHtml(Me.Foreground))
writer.WriteAttributeString("background", ColorTranslator.ToHtml(Me.Background))
writer.WriteAttributeString("bold", Me.Bold.ToString)
writer.WriteEndElement()
End Sub
' Properties
Public Property Background()Property Background() As Color
Get
Return Me._background
End Get
Set(ByVal value As Color)
Me._background = value
End Set
End Property
Public Property Bold()Property Bold() As Boolean
Get
Return Me._bold
End Get
Set(ByVal value As Boolean)
Me._bold = value
End Set
End Property
Public Property Foreground()Property Foreground() As Color
Get
Return Me._foreground
End Get
Set(ByVal value As Color)
Me._foreground = value
End Set
End Property
Public Property Name()Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
' Fields
Private _foreground As Color
Private _background As Color
Private _bold As Boolean
Private _name As String
End Class
''--------------------------------------------------------------
''StyleItemCollection
Public Class StyleItemCollectionClass StyleItemCollection
Inherits CollectionBase
' Methods
Public Overridable Sub Add()Sub Add(ByVal obj As StyleItem)
MyBase.List.Add(obj)
End Sub
Public Overridable Sub Remove()Sub Remove(ByVal obj As StyleItem)
MyBase.List.Remove(obj)
End Sub
' Properties
Public Property Item()Property Item(ByVal i As Integer) As StyleItem
Get
Return CType(MyBase.List.Item(i), StyleItem)
End Get
Set(ByVal value As StyleItem)
MyBase.List.Item(i) = value
End Set
End Property
End Class
''--------------------------------------------------------------
''StyleSchema
Public Class StyleSchemaClass StyleSchema
' Methods
Public Sub New()Sub New(ByVal name As String)
Me.StyleItems = New StyleItemCollection
Me.Name = name
End Sub
Public Shared Function DeSerializeFromXml()Function DeSerializeFromXml(ByVal node As XmlNode) As StyleSchema
Dim style As New StyleSchema(node.Attributes.ItemOf("name").Value)
Try
Dim fontNode As XmlNode = node.SelectSingleNode("Font")
style.Font = New Font(fontNode.Attributes.ItemOf("family").Value, Single.Parse(fontNode.Attributes.ItemOf("size").Value))
Dim nodes As XmlNodeList = node.SelectNodes("FontColorItems/FontColorItem")
For Each itemNode As XmlNode In nodes
style.StyleItems.Add(StyleItem.DeSerializeFromXml(itemNode))
Next
Catch
End Try
Return style
End Function
Public Sub SerializeToXml()Sub SerializeToXml(ByVal writer As XmlTextWriter)
writer.WriteStartElement("Style")
writer.WriteAttributeString("name", Me.Name)
writer.WriteStartElement("Font")
writer.WriteAttributeString("family", Me.Font.FontFamily.Name)
writer.WriteAttributeString("characterSet", "0")
writer.WriteAttributeString("size", Me.Font.Size.ToString)
writer.WriteEndElement()
writer.WriteStartElement("FontColorItems")
Dim item1 As StyleItem
For Each item1 In Me.StyleItems
item1.SerializeToXml(writer)
Next
writer.WriteEndElement()
writer.WriteEndElement()
End Sub
Public Shared Function DeSerializeFromFile()Function DeSerializeFromFile(ByVal filename As String) As StyleSchema
If File.Exists(filename) Then
Dim doc As New XmlDocument
doc.Load(filename)
Dim node As XmlNode = doc.SelectSingleNode("Styles/Style")
If Not node Is Nothing Then
Return StyleSchema.DeSerializeFromXml(node)
End If
End If
Return Nothing
End Function
Public Sub SaveToFile()Sub SaveToFile(ByVal filename As String)
If (Not filename Is "") Then
Dim writer As New XmlTextWriter(filename, Encoding.UTF8)
writer.WriteStartDocument(True)
writer.WriteStartElement("Styles")
SerializeToXml(writer)
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Close()
End If
End Sub
Public Sub ApplyToApplication()Sub ApplyToApplication()
Dim dteProperty As [Property ]()Property] = DTE.Properties("FontsAndColors", "TextEditor").Item("FontFamily")
dteProperty.Value = Font.FontFamily.Name
dteProperty = DTE.Properties("FontsAndColors", "TextEditor").Item("FontSize")
dteProperty.Value = Font.Size
Dim fontColorProperty As [Property ]()Property] = DTE.Properties("FontsAndColors", "TextEditor").Item("FontsAndColorsItems")
For Each item As StyleItem In _styleItems
Dim ci As ColorableItems = fontColorProperty.Object(item.Name)
If Not ci Is Nothing Then
ci.Foreground = Convert.ToUInt32(ColorTranslator.ToOle(item.Foreground))
ci.Background = Convert.ToUInt32(ColorTranslator.ToOle(item.Background))
ci.Bold = item.Bold
End If
Next
End Sub
Public Shared Function DeSerializeFromApplication()Function DeSerializeFromApplication() As StyleSchema
Dim style As New StyleSchema("default")
Dim fontFamity As String = DTE.Properties("FontsAndColors", "TextEditor").Item("FontFamily").Value.ToString
Dim fontSize As Single = Single.Parse(DTE.Properties("FontsAndColors", "TextEditor").Item("FontSize").Value.ToString)
style.Font = New Font(fontFamity, fontSize)
Dim fontColorProperty As [Property ]()Property] = DTE.Properties("FontsAndColors", "TextEditor").Item("FontsAndColorsItems")
Dim ci As ColorableItems
For Each ci In CType(fontColorProperty.Object, FontsAndColorsItems)
style.StyleItems.Add(New StyleItem(ci.Name, ci.Foreground, ci.Background, ci.Bold))
Next
Return style
End Function
' Properties
Public Property Font()Property Font() As Font
Get
Return Me._font
End Get
Set(ByVal value As Font)
Me._font = value
End Set
End Property
Public Property StyleItems()Property StyleItems() As StyleItemCollection
Get
Return Me._styleItems
End Get
Set(ByVal value As StyleItemCollection)
Me._styleItems = value
End Set
End Property
Public Property Name()Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = value
End Set
End Property
' Fields
Private _font As Font
Private _styleItems As StyleItemCollection
Private _name As String
End Class
''--------------------------------------------------------------
Public Class WinWrapperClass WinWrapper
Implements System.Windows.Forms.IWin32Window
Overridable ReadOnly Property Handle()Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
Get
Dim iptr As New System.IntPtr(DTE.MainWindow.HWnd)
Return iptr
End Get
End Property
End Class
''--------------------------------------------------------------
Private Sub InitFileDialog()Sub InitFileDialog(ByRef fileDialog As FileDialog)
#If VS_VERSION = 7 Then
fileDialog.Filter = "Visual Studio 2003 style(*.vss7)|*.vss7|All files (*.*)|*.*"
#Else
fileDialog.Filter = "Visual Studio 2005 style(*.vss8)|*.vss8|All files (*.*)|*.*"
#End If
End Sub
Public Sub SaveSettings()Sub SaveSettings()
Try
Dim winptr As New WinWrapper
Dim SaveFileDialog As New Forms.SaveFileDialog
InitFileDialog(SaveFileDialog)
SaveFileDialog.Title = "Save Style Settings"
If SaveFileDialog.ShowDialog(winptr) = Forms.DialogResult.OK Then
Dim style As StyleSchema
style = StyleSchema.DeSerializeFromApplication()
Dim filename As String = SaveFileDialog.FileName
style.SaveToFile(filename)
MsgBox("Save Finish")
End If
Catch err As System.Exception
MsgBox(err.Message)
End Try
End Sub
''--------------------------------------------------------------
Public Sub LoadSettings()Sub LoadSettings()
Try
Dim winptr As New WinWrapper
Dim openFileDialog As New Forms.OpenFileDialog
InitFileDialog(openFileDialog)
openFileDialog.Title = "Open Style Settings"
If openFileDialog.ShowDialog(winptr) = Forms.DialogResult.OK Then
Dim filename As String = openFileDialog.FileName
Dim style As StyleSchema = StyleSchema.DeSerializeFromFile(filename)
style.ApplyToApplication()
MsgBox("Load Finish")
End If
Catch err As System.Exception
MsgBox(err.Message)
End Try
End Sub
End Module
------------
附件提供可以直接使用的StyleManager宏和几个的配色方案。注意这个宏需要处理一百多配色项,运行较慢。
1.安装StyleManager宏:
a) 打开Visual Studio菜单工具|宏|加载宏项目,打开文件StyleMacros.vsmacros即可。注意不同版本的VS选择不同的宏。
2.使用StyleManager宏:
a) Alt+F8打开宏资源管理器
b) 调入配色方案: 运行StyleManager.LoadSettings宏 --> 调入附件中vss8\目录下的配色方案(VS2003的方案文件是.vss7, VS2005是.vss8)
c) 修改配色: 打开菜单工具|选项 --> 环境|字体和颜色
d) 保存配色方案:运行StyleManager.SaveSettings宏
3. 如果大家能够做出其他不错的配色方案,请发邮件到(walkdan<at>gmail.com),我加入到附件中,让大家分享更多的方案。
转载请注明出处
Download: VS StyleManager 1.0.6 for Visual Studio 2003, 2005
Reversion:
2007-03-26 1.0.6 新增加了vi darkblue(VS2005)方案,感谢thinkinnight提供
2006-11-03 1.0.5 新增加了torte(VS2003)方案,感谢大橘子提供
2006-08-06 1.0.4 修改了Code Definition Window配色, 感谢 Seuler.Shi
2006-04-13 1.0.3 修改了Twilight的XML配色方案,value型规范为Yellow
2006-04-12 1.0.2 对于VS2005, 可以直接导入.vssettings文件
2006-04-11 1.0.1 新增加VS2003的蓝色底Classic方案