本文共 5461 字,大约阅读时间需要 18 分钟。
先上效果图:
第一步:
下载VS对应版本的SDK,这里用2012举例:
第二步:
创建时语言选择C#,最后Finish界面的两个复选框把勾勾去掉
第三步:
添加4个引用:“PresentationCore”、“PresentationFramework”、“System.Xaml”、“WindowsBase”
第四步:
打开对应位置的程序GuidList,记住红圈中的两段代码,之后选中箭头指向的"XXXPackage.cs"文件
将代码改成如下(注意看注释!):
using Microsoft.VisualStudio.Shell;using Microsoft.VisualStudio.Shell.Interop;using System;using System.Runtime.InteropServices;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.Windows.Media.Imaging;namespace Microsoft.VSPackage1 //命名空间改成上面第一个红圈里的内容{ [PackageRegistration(UseManagedResourcesOnly = true)] [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)] [Guid(GuidList.guidVSPackage1PkgString)] //这里改成上面第二个第二个红圈里的内容 [ProvideAutoLoad(UIContextGuids.NoSolution)] [ProvideAutoLoad(UIContextGuids.SolutionExists)] public sealed class IDEBackgroundPackage : Package { protected override void Initialize() { base.Initialize(); Application.Current.MainWindow.Loaded += MainWindow_Loaded; } void MainWindow_Loaded(object sender, RoutedEventArgs e) { var rWindow = (Window)sender; var rImageSource = BitmapFrame.Create(new Uri(@"D:\程序设计\bj01.jp1"/*图片路径,记得修改!!*/), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); rImageSource.Freeze(); var rImageControl = new Image() { Source = rImageSource, Stretch = Stretch.UniformToFill, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, }; Grid.SetRowSpan(rImageControl, 4); var rRootGrid = (Grid)rWindow.Template.FindName("RootGrid", rWindow); rRootGrid.Children.Insert(0, rImageControl); } }}
到这里就已经完成1/3了,进行调试
第五步:
这时启动的是实验用VS,并且可以看出已经有外框了
之后在实验用VS处进入“工具->扩展功能和更新程序”如下图:
下载并安装箭头的那个插件(需要重启实验VS),如果VS库连接不上,那就等一等可能那边服务器关了
第六步:
下载已经配置好的模板:密码: ty8b
之后在实验用VS处进入“工具->Custumize Colors”会弹出一个页面,选择页面右边的"Import Theme"
打开刚刚下载的主题,到这里你就会发现好像已经成功了,但其实编辑器背景还是黑的,要把这层去掉
第七步:
关闭实验用VS,回到一开始的VSpackage,打开"source.extension.vsixmanifest"文件(应该就是开着的)
之后如下
点击OK之后再添加3个引用(和第二步一样):
System.ComponentModel.Composition、Microsoft.VisualStudio.CoreUtility、
Microsoft.VisualStudio.Text.UI、Microsoft.VisualStudio.Text.UI.Wpf(后三个在“扩展”里)
第八步:
新建一个.cs文件,命名"EditorBackground.cs",并输入以下代码
using Microsoft.VisualStudio.Text.Classification;using Microsoft.VisualStudio.Text.Editor;using Microsoft.VisualStudio.Utilities;using System;using System.ComponentModel.Composition;using System.Windows;using System.Windows.Controls;using System.Windows.Media;using System.Windows.Threading;namespace Moen.IDEBackground{ [Export(typeof(IWpfTextViewCreationListener))] [ContentType("Text")] [ContentType("BuildOutput")] [TextViewRole(PredefinedTextViewRoles.Document)] class Listener : IWpfTextViewCreationListener { [Import] IEditorFormatMapService EditorFormatMapService = null; public void TextViewCreated(IWpfTextView rpTextView) { new EditorBackground(rpTextView); var rProperties = EditorFormatMapService.GetEditorFormatMap(rpTextView).GetProperties("Indicator Margin"); rProperties["BackgroundColor"] = Colors.Transparent; rProperties["Background"] = Brushes.Transparent; } } class EditorBackground { IWpfTextView r_TextView; ContentControl r_Control; Grid r_ParentGrid; Canvas r_ViewStack; public EditorBackground(IWpfTextView rpTextView) { r_TextView = rpTextView; r_Control = (ContentControl)r_TextView; r_TextView.Background = Brushes.Transparent; r_TextView.BackgroundBrushChanged += TextView_BackgroundBrushChanged; r_TextView.Closed += TextView_Closed; r_Control.Loaded += TextView_Loaded; } void MakeBackgroundTransparent() { r_TextView.Background = Brushes.Transparent; r_ViewStack.Background = Brushes.Transparent; r_ParentGrid.ClearValue(Grid.BackgroundProperty); } void TextView_Loaded(object sender, RoutedEventArgs e) { if (r_ParentGrid == null) r_ParentGrid = (Grid)r_Control.Parent; if (r_ViewStack == null) r_ViewStack = (Canvas)r_Control.Content; MakeBackgroundTransparent(); } void TextView_BackgroundBrushChanged(object sender, BackgroundBrushChangedEventArgs e) { r_Control.Dispatcher.BeginInvoke(new Action(() => { while (r_ParentGrid.Background != null) MakeBackgroundTransparent(); }), DispatcherPriority.Render); } void TextView_Closed(object sender, EventArgs e) { r_TextView.Closed -= TextView_Closed; r_TextView.BackgroundBrushChanged -= TextView_BackgroundBrushChanged; } }}
最后一步:
调试并进入实验用VS,就可以看到成果,不过当然这只在实验实例里有效,所以你每次打开VS都要编译一下VSpackage比较麻烦,这里其实可以直接进,我的路径如下:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Visual Studio 2012\Microsoft Visual Studio SDK\Tools\Start Experimental Instance of Visual Studio 2012
启动这个"Start Experimental Instance of Visual Studio 2012"就直接是实验实例了
备注:这个其实我也是学习网上教程的,不过它们因为没有现成的背景透明度设置文件,再加上其实很多地方都不清楚或者有问题和错误,所以最后想弄好还是比较麻烦的,需要查阅很多地方的资料,并且花的时间也不少
在这里就是整理一下,希望一样想设置背景的同学可以省一点时间,应该很快就可以搞定了,中间如果有问题可以回复问我