Visual Studio 2012个性化 代码
在开始之前,先准备Visual Studio 2012 SDK
安装好SDK后,进入VS。先新建一个Project,在“其它项目类型”那里找到“Visual Studio Package”
接下来的对话框里,选“C#”,然后基本是下一步。在最后一步把那两个复选框取消,因为那个在这里没什么用处。最后就成功新建了个VS扩展的Project
三、初步改造
第一步我们给VS加上背景图。首先对Project添加WPF的程序集为引用,有四个,分别为“PresentationCore”、“PresentationFramework”、“System.Xaml”、“WindowsBase”。然后打开“XXXPackage.cs”(XXX一般为这个Project的名字)文件,代码如下:
1 usingMicrosoft.VisualStudio.Shell; usingMicrosoft.VisualStudio.Shell.Interop; 2 3 using System; using System.Runtime.InteropServices; using System.Windows; 4 5 using System.Windows.Controls; using System.Windows.Media; 6 7 using System.Windows.Media.Imaging; 8 9 namespace Moen.IDEBackground //命名空间自己修改回自己用的 { 10 11 [PackageRegistration(UseManagedResourcesOnly = true)] 12 13 [InstalledProductRegistration("#110", "#112","1.0", IconResourceID = 400)] 14 15 [Guid(GuidList.guidIDE_BackgroundPkgString)] 16 17 [ProvideAutoLoad(UIContextGuids.NoSolution)] 18 19 [ProvideAutoLoad(UIContextGuids.SolutionExists)] 20 21 public sealed class IDEBackgroundPackage :Package 22 23 { 24 25 protected override void Initialize() 26 27 { 28 29 base.Initialize(); 30 31 Application.Current.MainWindow.Loaded += MainWindow_Loaded; 32 33 } 34 35 void MainWindow_Loaded(object sender, RoutedEventArgs e) 36 37 { 38 39 var rWindow = (Window)sender; 40 41 //加载图片 42 43 var rImageSource =BitmapFrame.Create(new Uri(@"G:\Picture\Pool\絵师100人展02_p109.png"/*图片路径*/),BitmapCreateOptions.None,BitmapCacheOption.OnLoad); 44 45 rImageSource.Freeze(); 46 47 var rImageControl = new Image() 48 49 { 50 51 Source = rImageSource, 52 53 Stretch =Stretch.UniformToFill, //按比例填充 54 55 HorizontalAlignment =HorizontalAlignment.Center, //水平方向中心对齐 56 57 VerticalAlignment =VerticalAlignment.Center, //垂直方向中心对齐 58 59 }; 60 61 Grid.SetRowSpan(rImageControl, 4); 62 63 var rRootGrid =(Grid)rWindow.Template.FindName("RootGrid", rWindow); 64 65 rRootGrid.Children.Insert(0, rImageControl); 66 67 } 68 69 } 70 71 }
代码修改一下后,调试,这时就会编译扩展,然后启动实验用VS。(如果这是第一次启动实验用VS,可能要像刚安装完VS那样设置一下)接着你会看到角落处显现出那张背景图 (免调试进入实验用VS方法:开始菜单->Microsoft Visual Studio 2012->Microsoft Visual Studio SDK->Tools->Start Experimental Instance of Visual Studio 2012)
四、修改皮肤配色
为了方便,在实验用VS处进入“工具->扩展功能和更新程序”,选“在线”部分,然后在中间找到“Visual Studio 2012 Color ThemeEditor”并安装,重启实验用VS 重启后,进入“工具->CustomizeColors”。本例子已深色为基础,于是在左边“New Theme”处,直接在文本框输入一个皮肤名,然后点“Create”。这样就进入了皮肤配色表
首先把主界面那一大块灰色给除掉。找到“Environment→EnvironmentBackgroundGradient”为开头的,统统都把不透明度设为0。然后点表左上角的“Save andApply Theme”,关掉所有页面。然后你就会看到背景啦
再继续,找到“Environment→ MainWindowActiveCaption”、“Environment→ MainWindowInactiveCaption”、“Environment→ CommandShelfBackgroundGradientXXX”、“Environment→ CommandShelfHighlightGradientXXX”、“Environment→ CommandBarGradientXXX”、“Environment→ CommandBarToolBarBorder”,都把不透明度设为0,然后应用。
上面那部分灰色的也没啦 至于这些是对应哪里的呢,可以通过那名字来确定,不过不准。要详细弄清楚很麻烦,要用反编译软件反要修改的控件的xaml文档,找到对应的画刷名。非常复杂,所以我这里提供我自己用的。在“Customize Colors”那里点“Import Theme”即可
五、编辑器
到目前为止,打开文件后,编辑器的背景还是黑的。接下来就是把这层黑的去掉 先打开“source.extension.vsixmanifest”文件,进入“Assets”选项卡,单击“New”按钮。在弹出的对话框里,“Type”选“Microsoft.VisualStudio.MefComponent”,“Source”选“Aproject in current solution”,“Project”选当前的Project,目前应该就一个选项的。最后OK 接下来新建一个文件,这里就叫“EditorBackground.cs” 在输入代码前添加几个引用——System.ComponentModel.Composition、
1 Microsoft.VisualStudio.CoreUtility、 2 3 Microsoft.VisualStudio.Text.UI、 4 5 Microsoft.VisualStudio.Text.UI.Wpf(后三个在“扩展”处找) 搞定后文件代码如下: 6 7 usingMicrosoft.VisualStudio.Text.Classification; 8 9 usingMicrosoft.VisualStudio.Text.Editor; 10 11 usingMicrosoft.VisualStudio.Utilities; usingSystem; 12 13 usingSystem.ComponentModel.Composition; usingSystem.Windows; 14 15 usingSystem.Windows.Controls; usingSystem.Windows.Media; 16 17 using System.Windows.Threading; 18 19 namespaceMoen.IDEBackground 20 21 { 22 23 [Export(typeof(IWpfTextViewCreationListener))] 24 25 [ContentType("Text")] 26 27 [ContentType("BuildOutput")] 28 29 [TextViewRole(PredefinedTextViewRoles.Document)] 30 31 class Listener : IWpfTextViewCreationListener 32 33 { 34 35 [Import] 36 37 IEditorFormatMapServiceEditorFormatMapService = null; 38 39 public voidTextViewCreated(IWpfTextView rpTextView) 40 41 { 42 43 new EditorBackground(rpTextView); 44 45 //去掉断点边栏的背景 46 47 var rProperties =EditorFormatMapService.GetEditorFormatMap(rpTextView).GetProperties("IndicatorMargin"); 48 49 rProperties["BackgroundColor"] = Colors.Transparent; 50 51 rProperties["Background"]= Brushes.Transparent; 52 53 } 54 55 } 56 57 class EditorBackground 58 59 { 60 61 IWpfTextView r_TextView; 62 63 ContentControl r_Control; 64 65 Grid r_ParentGrid; 66 67 Canvas r_ViewStack; 68 69 public EditorBackground(IWpfTextViewrpTextView) 70 71 { 72 73 r_TextView = rpTextView; 74 75 r_Control = (ContentControl)r_TextView; 76 77 r_TextView.Background =Brushes.Transparent; 78 79 r_TextView.BackgroundBrushChanged+= TextView_BackgroundBrushChanged; 80 81 r_TextView.Closed +=TextView_Closed; 82 83 r_Control.Loaded +=TextView_Loaded; 84 85 } 86 87 void MakeBackgroundTransparent() 88 89 { 90 91 r_TextView.Background =Brushes.Transparent; 92 93 r_ViewStack.Background =Brushes.Transparent; 94 95 r_ParentGrid.ClearValue(Grid.BackgroundProperty); 96 97 } 98 99 void TextView_Loaded(object sender,RoutedEventArgs e) 100 101 { 102 103 if (r_ParentGrid == null) 104 105 r_ParentGrid =(Grid)r_Control.Parent; 106 107 if (r_ViewStack == null) 108 109 r_ViewStack =(Canvas)r_Control.Content; 110 111 MakeBackgroundTransparent(); 112 113 } 114 115 voidTextView_BackgroundBrushChanged(object sender, BackgroundBrushChangedEventArgse) 116 117 { 118 119 r_Control.Dispatcher.BeginInvoke(new Action(() => 120 121 { 122 123 while (r_ParentGrid.Background!= null) 124 125 MakeBackgroundTransparent(); 126 127 }), DispatcherPriority.Render); 128 129 } 130 131 void TextView_Closed(object sender,EventArgs e) 132 133 { 134 135 //清除委托,以防内存泄露 136 137 r_TextView.Closed -=TextView_Closed; 138 139 r_TextView.BackgroundBrushChanged-= TextView_BackgroundBrushChanged; 140 141 } 142 143 } }
调试进入实验用VS,进入配色表,找到“Environment →EnvironmentBackground”,设置一个颜色值(我这里是#A0000000),作为编辑器的背景色。再找到“Environment → Window”设置为透明
打开vs2012-工具--扩展和更新 在联机中搜索 Theme Editer 自定义颜色插件:Visual Studio 2012 Color Theme Editor
六、结尾
基本的VS界面改造就是这么多了。不过有个棘手的问题——xaml编辑器和个别的编辑器(如HTML的)因为是承载在VS的一个子窗口上,而这个窗口的背景是黑色的。目前仍在研究中……