Click here to Skip to main content
15,891,907 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 139.1K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;

namespace ScintillaNet
{
	public static class Utilities
	{
		public static int ColorToRgb(Color c)
		{
			return c.R + (c.G << 8) + (c.B << 16);
		}

		public static Color RgbToColor(int color)
		{
			return Color.FromArgb(color & 0x0000ff, (color & 0x00ff00) >> 8, (color & 0xff0000) >> 16);
		}

		/// <summary>
		/// Returns an HTML #XXXXXX format for a color. Unlike the ColorTranslator class it
		/// never returns named colors.
		/// </summary>
		/// <param name="c"></param>
		/// <returns></returns>
		public static string ColorToHtml(Color c)
		{
			return "#" + c.R.ToString("X2", null) + c.G.ToString("X2", null) + c.B.ToString("X2", null);
		}

		
		public static int SignedLoWord(IntPtr loWord)
		{
			return (short)((int)(long)loWord & 0xffff);
		}

		public static int SignedHiWord(IntPtr hiWord)
		{
			return (short)(((int)(long)hiWord >> 0x10) & 0xffff);
		}

		public unsafe static string PtrToStringUtf8(IntPtr ptr, int length)
		{
			if(ptr == IntPtr.Zero)
				return null;

			byte[] buff = new byte[length];
			Marshal.Copy(ptr, buff, 0, length);
			return System.Text.UTF8Encoding.UTF8.GetString(buff);
		}

		public unsafe static string MarshalStr(IntPtr p)
		{
			// instead of 
			// System.Runtime.InteropServices.Marshal.PtrToStringAuto(p)
			sbyte* ps = (sbyte*)p;
			int size = 0;
			for (; ps[size] != 0; ++size)
				;
			return new String(ps, 0, size);
		}

		public static uint GetMarkerMask(IEnumerable<int> markers)
		{
			uint mask = 0;
			foreach (int i in markers)
				mask |= (uint)i;
			return mask;
		}


		public static uint GetMarkerMask(IEnumerable<Marker> markers)
		{
			uint mask = 0;
			foreach (Marker m in markers)
				mask |= (uint)m.Number;
			return mask;
		}

		public static Keys GetKeys(char c)
		{
			switch (c)
			{
				case '/':
					return Keys.Oem2;
				case '`':
					return Keys.Oem3;
				case '[':
					return Keys.Oem4;
				case '\\':
					return Keys.Oem5;
				case ']':
					return Keys.Oem6;
				case '-':
					return (Keys)189;

			}

			return (Keys)Enum.Parse(typeof(Keys), c.ToString(), true);
		}

		public static Keys GetKeys(string s)
		{
			switch (s)
			{
				case "/":
					return Keys.Oem2;
				case "`":
					return Keys.Oem3;
				case "[":
					return Keys.Oem4;
				case "\\":
					return Keys.Oem5;
				case "]":
					return Keys.Oem6;
				case "-":
					return (Keys)189;
			}

			return (Keys)Enum.Parse(typeof(Keys), s, true);
		}

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions