using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using Nullstack.Utilities.Guidgen2.Properties; namespace Nullstack.Utilities.Guidgen2 { public partial class GuidInterface : Form { #region Format Strings // format strings! use standard string.format notation // args 0-15 are the 0th to 15th bytes of the guid specified as integers // args 16 and 17 are the open and close brace ('{', '}') because you may need them (odd behaviour of string.format re: {15,X2}}} situations) private static readonly string FORMAT_IMPLEMENTOLECREATE = "// {{{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:x2}{7:x2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}{17}" + Environment.NewLine + "IMPLEMENT_OLECREATE(<>, <>, " + Environment.NewLine + "0x{0:x}{1:x}{2:x}{3:x}, 0x{4:x}{5:x}, 0x{6:x}{7:x}, 0x{8:x}, 0x{9:x}, 0x{10:x}, 0x{11:x}, 0x{12:x}, 0x{13:x}, 0x{14:x}, 0x{15:x});"; private static readonly string FORMAT_DEFINEGUID = "// {{{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:x2}{7:x2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}{17}" + Environment.NewLine + "DEFINE_GUID(<>, " + Environment.NewLine + "0x{0:x}{1:x}{2:x}{3:x}, 0x{4:x}{5:x}, 0x{6:x}{7:x}, 0x{8:x}, 0x{9:x}, 0x{10:x}, 0x{11:x}, 0x{12:x}, 0x{13:x}, 0x{14:x}, 0x{15:x});"; private static readonly string FORMAT_STATICCONST = "// {{{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:x2}{7:x2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}{17}" + Environment.NewLine + "static const GUID <> = " + Environment.NewLine + "{{ 0x{0:x}{1:x}{2:x}{3:x}, 0x{4:x}{5:x}, 0x{6:x}{7:x}, {{ 0x{8:x}, 0x{9:x}, 0x{10:x}, 0x{11:x}, 0x{12:x}, 0x{13:x}, 0x{14:x}, 0x{15:x} }} }};"; private static readonly string FORMAT_REGISTRY = "{{{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:x2}{7:x2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}{17}"; private static readonly string FORMAT_CSASSEMBLYINFO = "[assembly: Guid(\"{0:x2}{1:x2}{2:x2}{3:x2}-{4:x2}{5:x2}-{6:x2}{7:x2}-{8:x2}{9:x2}-{10:x2}{11:x2}{12:x2}{13:x2}{14:x2}{15:x2}\")]"; private static readonly string FORMAT_VBASSEMBLYINFO = ""; private static readonly string FORMAT_PLAIN = "{0:X2}{1:X2}{2:X2}{3:X2}-{4:X2}{5:X2}-{6:x2}{7:x2}-{8:X2}{9:X2}-{10:X2}{11:X2}{12:X2}{13:X2}{14:X2}{15:X2}"; #endregion #region API Imports private const Int32 WM_SYSCOMMAND = 0x112; private const Int32 WS_MAXIMIZEBOX = 0x10000; private const Int32 GWL_STYLE = (-16); private const Int32 MF_REMOVE = 0x1000; private const Int32 MF_SEPARATOR = 0x800; private const Int32 MF_BYPOSITION = 0x400; private const Int32 MF_DISABLED = 0x2; private const Int32 MF_GRAYED = 0x1; private const Int32 MF_STRING = 0x0; private const Int32 IDM_ABOUT = 1000; private const Int32 IDM_MAXIMIZE = 1001; private const Int32 IDM_SIZE = 1002; [DllImport("User32.Dll")] private static extern IntPtr DrawMenuBar(IntPtr hwnd); [DllImport("user32.dll")] private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("User32.Dll")] private static extern IntPtr GetMenuItemCount(IntPtr hMenu); [DllImport("user32.dll")] private static extern IntPtr InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem); [DllImport("user32.dll")] private static extern IntPtr ModifyMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem); [DllImport("user32.dll")] private static extern IntPtr GetWindowLong(IntPtr hWnd, Int32 wIndex); [DllImport("user32.dll")] private static extern IntPtr SetWindowLong(IntPtr hWnd, Int32 wIndex, IntPtr dwNewLong); #endregion #region Variables private GuidFormatStyles _format; private Guid _guid; private string _result; #endregion #region Constructors public GuidInterface() { InitializeComponent(); btnCopy.Text = Resources.btnCopy; btnExit.Text = Resources.btnExit; btnGenerate.Text = Resources.btnGenerate; chkAlwaysOnTop.Text = Resources.chkAlwaysOnTop; grpFormat.Text = Resources.strGUIDFormat; grpResult.Text = Resources.strResult; lblInstructions.Text = Resources.strInstructions; radFormat1.Text = Resources.radFormat1; radFormat2.Text = Resources.radFormat2; radFormat3.Text = Resources.radFormat3; radFormat4.Text = Resources.radFormat4; radFormat5.Text = Resources.radFormat5; radFormat6.Text = Resources.radFormat6; radFormat7.Text = Resources.radFormat7; radFormat1.CheckedChanged += radFormat_CheckedChanged; radFormat2.CheckedChanged += radFormat_CheckedChanged; radFormat3.CheckedChanged += radFormat_CheckedChanged; radFormat4.CheckedChanged += radFormat_CheckedChanged; radFormat5.CheckedChanged += radFormat_CheckedChanged; radFormat6.CheckedChanged += radFormat_CheckedChanged; radFormat7.CheckedChanged += radFormat_CheckedChanged; chkAlwaysOnTop.CheckedChanged += chkAlwaysOnTop_Checked; _format = GuidFormatStyles.IMPLEMENT_OLECREATE; NewGuid(); } #endregion #region Properties public GuidFormatStyles Format { get { return _format; } set { if (_format != value) { _format = value; CalculateResult(); } } } public Guid Guid { get { return _guid; } } public string FormattedGuid { get { return _result; } } #endregion #region Public Methods public void NewGuid() { _guid = Guid.NewGuid(); CalculateResult(); } public void CopyToClipboard() { Clipboard.SetText(_result); } #endregion #region Form Overrides protected override void OnLoad(EventArgs e) { base.OnLoad(e); IntPtr sysMenuHandle = GetSystemMenu(this.Handle, false); IntPtr menuCount = GetMenuItemCount(sysMenuHandle); IntPtr windowStyle = GetWindowLong(this.Handle, GWL_STYLE); // disable Maximize and Size from System Menu ModifyMenu(sysMenuHandle, menuCount.ToInt32() - 3, MF_BYPOSITION | MF_GRAYED, IDM_MAXIMIZE, Resources.mnuMaximize); ModifyMenu(sysMenuHandle, menuCount.ToInt32() - 5, MF_BYPOSITION | MF_GRAYED, IDM_SIZE, Resources.mnuSize); // add About to the system menu InsertMenu(sysMenuHandle, menuCount.ToInt32(), MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty); InsertMenu(sysMenuHandle, menuCount.ToInt32() + 1, MF_BYPOSITION, IDM_ABOUT, Resources.mnuAbout); windowStyle = new IntPtr(windowStyle.ToInt32() & ~WS_MAXIMIZEBOX); SetWindowLong(this.Handle, GWL_STYLE, windowStyle); // redraw the menu bar DrawMenuBar(this.Handle); } protected override void WndProc(ref Message m) { if (m.Msg == WM_SYSCOMMAND) { switch (m.WParam.ToInt32()) { case IDM_ABOUT: (new About()).ShowDialog(this); return; default: break; } } base.WndProc(ref m); } #endregion #region Private Helpers private void CalculateResult() { string format = ""; switch (_format) { case GuidFormatStyles.IMPLEMENT_OLECREATE: format = FORMAT_IMPLEMENTOLECREATE; break; case GuidFormatStyles.DEFINE_GUID: format = FORMAT_DEFINEGUID; break; case GuidFormatStyles.STATIC_CONST_STRUCT: format = FORMAT_STATICCONST; break; case GuidFormatStyles.REGISTRY_FORMAT: format = FORMAT_REGISTRY; break; case GuidFormatStyles.CS_ASSEMBLYINFO: format = FORMAT_CSASSEMBLYINFO; break; case GuidFormatStyles.VB_ASSEMBLYINFO: format = FORMAT_VBASSEMBLYINFO; break; case GuidFormatStyles.PLAIN: format = FORMAT_PLAIN; break; } byte[] b = _guid.ToByteArray(); int[] i = new int[16]; for (int n = 0; n < 16; n++) { i[n] = (int)b[n]; } _result = string.Format(format, i[3], i[2], i[1], i[0], i[5], i[4], i[7], i[6], i[8], i[9], i[10], i[11], i[12], i[13], i[14], i[15], "{", "}"); lblResult.Text = _result; } private void radFormat_CheckedChanged(object sender, EventArgs e) { GuidFormatStyles value = GuidFormatStyles.PLAIN; if (radFormat1.Checked) { value = GuidFormatStyles.IMPLEMENT_OLECREATE; } else if (radFormat2.Checked) { value = GuidFormatStyles.DEFINE_GUID; } else if (radFormat3.Checked) { value = GuidFormatStyles.STATIC_CONST_STRUCT; } else if (radFormat4.Checked) { value = GuidFormatStyles.REGISTRY_FORMAT; } else if (radFormat5.Checked) { value = GuidFormatStyles.CS_ASSEMBLYINFO; } else if (radFormat6.Checked) { value = GuidFormatStyles.VB_ASSEMBLYINFO; } else if (radFormat7.Checked) { value = GuidFormatStyles.PLAIN; } if (_format != value) { _format = value; CalculateResult(); } } #endregion #region GUI Event Handlers private void btnGenerate_Click(object sender, EventArgs e) { NewGuid(); } private void btnExit_Click(object sender, EventArgs e) { Close(); } private void btnCopy_Click(object sender, EventArgs e) { CopyToClipboard(); } private void chkAlwaysOnTop_Checked(object sender, EventArgs e) { this.TopMost = chkAlwaysOnTop.Checked; } #endregion } }