Click here to Skip to main content
15,886,744 members
Articles / Programming Languages / C# 4.0

Writing a P2P Snippet sharing Extension for Visual Studio 2010 (VSX 2010)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
31 Mar 2010GPL311 min read 36.4K   332   21  
CodeXchange is a simple Visual Studio extension which allows you to create, edit and share snippets with your peers without leaving the Visual Studio 2010 IDE.
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace Sand.Services.CodeXchange.Client.Controls
{
	/// <summary>
	/// OptionSectionPanel mimics the option panels of Outlook 2003
	/// (Caption text, a line on the right and optional an image left below).
	/// </summary>
	[DefaultProperty("Text")]
	public class OptionSectionPanel : System.Windows.Forms.Panel
	{
		private Color	_highlight		= Color.FromKnownColor( KnownColor.ControlLightLight );
		private Color	_shadow			= Color.FromKnownColor( KnownColor.ControlDark );
		private Image	_image			= null;
		private Point	_imageLocation;
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Initializer.
		/// </summary>
		public OptionSectionPanel()
		{
			_imageLocation = new Point(0, 20);

			SetStyle(ControlStyles.DoubleBuffer, true);
			
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		protected override void OnPaintBackground(PaintEventArgs pe) 
		{

			base.OnPaintBackground (pe);
			
			StringFormat sf = new StringFormat (StringFormatFlags.MeasureTrailingSpaces|
				StringFormatFlags.NoWrap);

			Graphics g	= pe.Graphics;
			SizeF lt = g.MeasureString(this.Text + " ", this.Font, this.Width, sf);
			float lth = lt.Height / 2;

			Brush h	= new SolidBrush( _highlight );
			Pen	ph	= new Pen(h, 1.0f);
			Brush s	= new SolidBrush( _shadow );
			Pen	ps	= new Pen(s, 1.0f);

			g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;

			g.DrawString(this.Text, this.Font, SystemBrushes.FromSystemColor(this.ForeColor) , new PointF(0,0), sf);
			
			g.DrawLine( ps, lt.Width, lth, Width,  lth );
			
			g.DrawLine( ph, lt.Width, lth + 1.0f, Width,  lth + 1.0f);
			
			if (this._image != null)
				g.DrawImage(this._image,  _imageLocation.X, _imageLocation.Y, this._image.Width, this._image.Height);
			
			sf.Dispose();
			ph.Dispose();
			ps.Dispose();
			h.Dispose();
			s.Dispose();

		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// OptionSectionPanel
			// 
			this.Size = new System.Drawing.Size(220, 150);

		}
		#endregion

		/// <summary>
		/// Set/Get the image to be displayed for this option
		/// </summary>
		[
		Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
		DefaultValue(null),
		Category("Appearance")
		]
		public Image Image 
		{
			get { return this._image;	}
			set 
			{ 
				this._image = value;	
				this.Invalidate();
			}
		}

		/// <summary>
		/// Set/Get the section caption text
		/// </summary>
		[
		Browsable(true), Localizable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
		DefaultValue(null),
		Category("Appearance")
		]
		public override string Text 
		{
			get 
			{
				return base.Text;
			}
			set 
			{
				base.Text = value;
				this.Invalidate();
			}
		}


		/// <summary>
		/// Set/Get the Image Location.
		/// </summary>
		[
		Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
		Category("Layout")
		]
		public Point ImageLocation 
		{
			get 
			{
				return _imageLocation;
			}
			set 
			{
				_imageLocation = value;
				this.Invalidate();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Spain Spain
Hi! I'm 22 years old. I live in a sunny mediterranian city called Barcelona.

I am a big fan of .NET and have been working with c# for a few years now.

Comments and Discussions