Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Copyright © 2015 Adonis S. Deliannis (Blizzardo1)
- */
- #region Include Directives
- using System;
- using System.Diagnostics;
- using System.Drawing;
- using System.Runtime.InteropServices;
- #endregion Include Directives
- using static NativeMethods;
- /// <summary>
- /// </summary>
- public enum Colors : int
- {
- /// <summary>
- /// The color white
- /// </summary>
- White = 0,
- /// <summary>
- /// The color black
- /// </summary>
- Black = 1,
- /// <summary>
- /// The color blue
- /// </summary>
- Blue = 2,
- /// <summary>
- /// The color green
- /// </summary>
- Green = 3,
- /// <summary>
- /// The color red
- /// </summary>
- Red = 4,
- /// <summary>
- /// The color maroon
- /// </summary>
- Maroon = 5,
- /// <summary>
- /// The color purple
- /// </summary>
- Purple = 6,
- /// <summary>
- /// The color orange
- /// </summary>
- Orange = 7,
- /// <summary>
- /// The color yellow
- /// </summary>
- Yellow = 8,
- /// <summary>
- /// The color light green
- /// </summary>
- LightGreen = 9,
- /// <summary>
- /// The color dark cyan
- /// </summary>
- DarkCyan = 10,
- /// <summary>
- /// The color cyan
- /// </summary>
- Cyan = 11,
- /// <summary>
- /// The color royale blue
- /// </summary>
- Royale = 12,
- /// <summary>
- /// The color magenta
- /// </summary>
- Magenta = 13,
- /// <summary>
- /// The color gray
- /// </summary>
- Gray = 14,
- /// <summary>
- /// The color light gray
- /// </summary>
- LightGray = 15,
- /// <summary>
- /// Color is overridden
- /// </summary>
- Overridden = -1
- };
- [JsonObject ( Title = "Theme", Description = "Global Theme for the Client" )]
- public struct Theme
- {
- private IrcPalette palette;
- private IrcColor action;
- private IrcColor ctcp;
- private IrcColor error;
- private IrcColor highlight;
- private IrcColor info;
- private IrcColor info2;
- private IrcColor invite;
- private IrcColor join;
- private IrcColor kick;
- private IrcColor mode;
- private IrcColor nick;
- private IrcColor normal;
- private IrcColor notice;
- private IrcColor notify;
- private IrcColor other;
- private IrcColor own;
- private IrcColor part;
- private IrcColor quit;
- private IrcColor topic;
- private IrcColor wallops;
- private IrcColor whois;
- private IrcColor fore;
- private IrcColor back;
- private string name;
- [JsonProperty ( PropertyName = "Palette" )]
- public IrcPalette Palette => palette;
- [JsonProperty ( PropertyName = "Action" )]
- public IrcColor Action => action;
- [JsonProperty ( PropertyName = "Back" )]
- public IrcColor Background => back;
- [JsonProperty ( PropertyName = "Ctcp" )]
- public IrcColor Ctcp => ctcp;
- [JsonProperty ( PropertyName = "Error" )]
- public IrcColor Error => error;
- [JsonProperty ( PropertyName = "Foreground" )]
- public IrcColor Foreground => fore;
- [JsonProperty ( PropertyName = "Highlight" )]
- public IrcColor Highlight => highlight;
- [JsonProperty ( PropertyName = "Info" )]
- public IrcColor Info => info;
- [JsonProperty ( PropertyName = "Info2" )]
- public IrcColor Info2 => info2;
- [JsonProperty ( PropertyName = "Invite" )]
- public IrcColor Invite => invite;
- [JsonProperty ( PropertyName = "Join" )]
- public IrcColor Join => join;
- [JsonProperty ( PropertyName = "Kick" )]
- public IrcColor Kick => kick;
- [JsonProperty ( PropertyName = "Mode" )]
- public IrcColor Mode => mode;
- [JsonProperty ( PropertyName = "Nick" )]
- public IrcColor Nick => nick;
- [JsonProperty ( PropertyName = "Normal" )]
- public IrcColor Normal => normal;
- [JsonProperty ( PropertyName = "Notice" )]
- public IrcColor Notice => notice;
- [JsonProperty ( PropertyName = "Notify" )]
- public IrcColor Notify => notify;
- [JsonProperty ( PropertyName = "other" )]
- public IrcColor Other => other;
- [JsonProperty ( PropertyName = "Own" )]
- public IrcColor Own => own;
- [JsonProperty ( PropertyName = "Part" )]
- public IrcColor Part => part;
- [JsonProperty ( PropertyName = "Quit" )]
- public IrcColor Quit => quit;
- [JsonProperty ( PropertyName = "Topic" )]
- public IrcColor Topic => topic;
- [JsonProperty ( PropertyName = "Wallops" )]
- public IrcColor Wallops => wallops;
- [JsonProperty ( PropertyName = "Whois" )]
- public IrcColor Whois => whois;
- [JsonProperty ( PropertyName = "Name" )]
- public string Name => name;
- // [JsonConstructor]
- public Theme ( string name,
- IrcPalette palette,
- IrcColor action,
- IrcColor background,
- IrcColor ctcp,
- IrcColor error,
- IrcColor foreground,
- IrcColor highlight,
- IrcColor info,
- IrcColor info2,
- IrcColor invite,
- IrcColor join,
- IrcColor kick,
- IrcColor mode,
- IrcColor nick,
- IrcColor normal,
- IrcColor notice,
- IrcColor notify,
- IrcColor other,
- IrcColor own,
- IrcColor part,
- IrcColor quit,
- IrcColor topic,
- IrcColor wallops,
- IrcColor whois
- )
- {
- this.name = name;
- this.palette = palette;
- this.action = action;
- this.ctcp = ctcp;
- this.error = error;
- this.highlight = highlight;
- this.info = info;
- this.info2 = info2;
- this.invite = invite;
- this.join = join;
- this.kick = kick;
- this.mode = mode;
- this.nick = nick;
- this.normal = normal;
- this.notice = notice;
- this.notify = notify;
- this.other = other;
- this.own = own;
- this.part = part;
- this.quit = quit;
- this.topic = topic;
- this.wallops = wallops;
- this.whois = whois;
- fore = foreground;
- back = background;
- }
- private static Theme _blizzy = new Theme
- {
- name = "Blizzy",
- palette = IrcPalette.Blizzy,
- action = IrcColor.FromCode ( 10 ),
- back = IrcColor.FromCode ( 1 ),
- ctcp = IrcColor.FromCode ( 5 ),
- error = IrcColor.FromCode ( 4 ),
- fore = IrcColor.FromCode ( 0 ),
- highlight = IrcColor.FromCode ( 7 ),
- info = IrcColor.FromCode ( 5 ),
- info2 = IrcColor.FromCode ( 5 ),
- invite = IrcColor.FromCode ( 3 ),
- join = IrcColor.FromCode ( 11 ),
- kick = IrcColor.FromCode ( 4 ),
- mode = IrcColor.FromCode ( 10 ),
- nick = IrcColor.FromCode ( 14 ),
- normal = IrcColor.FromCode ( 0 ),
- notice = IrcColor.FromCode ( 6 ),
- notify = IrcColor.FromCode ( 2 ),
- other = IrcColor.FromCode ( 15 ),
- own = IrcColor.FromCode ( 0 ),
- part = IrcColor.FromCode ( 10 ),
- quit = IrcColor.FromCode ( 13 ),
- topic = IrcColor.FromCode ( 12 ),
- wallops = IrcColor.FromCode ( 12 ),
- whois = IrcColor.FromCode ( 12 ),
- };
- public static Theme Blizzy => _blizzy;
- private static Theme _mircClassic = new Theme
- {
- name = "mIRC Classic",
- palette = IrcPalette.Default,
- action = IrcColor.FromCode ( 6 ),
- back = IrcColor.FromCode ( 0 ),
- ctcp = IrcColor.FromCode ( 4 ),
- error = IrcColor.FromCode ( 9 ),
- fore = IrcColor.FromCode ( 1 ),
- highlight = IrcColor.FromCode ( 5 ),
- info = IrcColor.FromCode ( 2 ),
- info2 = IrcColor.FromCode ( 3 ),
- invite = IrcColor.FromCode ( 3 ),
- join = IrcColor.FromCode ( 3 ),
- kick = IrcColor.FromCode ( 3 ),
- mode = IrcColor.FromCode ( 3 ),
- nick = IrcColor.FromCode ( 3 ),
- normal = IrcColor.FromCode ( 8 ),
- notice = IrcColor.FromCode ( 5 ),
- notify = IrcColor.FromCode ( 7 ),
- other = IrcColor.FromCode ( 6 ),
- own = IrcColor.FromCode ( 8 ),
- part = IrcColor.FromCode ( 3 ),
- quit = IrcColor.FromCode ( 2 ),
- topic = IrcColor.FromCode ( 3 ),
- wallops = IrcColor.FromCode ( 5 ),
- whois = IrcColor.FromCode ( 8 )
- };
- public static Theme mIrcClassic => _mircClassic;
- private static Theme _mircModern = new Theme
- {
- name = "mIRC Modern",
- palette = IrcPalette.Modern,
- action = IrcColor.FromCode ( 6 ),
- back = IrcColor.FromCode ( 0 ),
- ctcp = IrcColor.FromCode ( 4 ),
- error = IrcColor.FromCode ( 8 ),
- fore = IrcColor.FromCode ( 1 ),
- highlight = IrcColor.FromCode ( 7 ),
- info = IrcColor.FromCode ( 3 ),
- info2 = IrcColor.FromCode ( 3 ),
- invite = IrcColor.FromCode ( 3 ),
- join = IrcColor.FromCode ( 3 ),
- kick = IrcColor.FromCode ( 3 ),
- mode = IrcColor.FromCode ( 3 ),
- nick = IrcColor.FromCode ( 3 ),
- normal = IrcColor.FromCode ( 0 ),
- notice = IrcColor.FromCode ( 5 ),
- notify = IrcColor.FromCode ( 7 ),
- other = IrcColor.FromCode ( 6 ),
- own = IrcColor.FromCode ( 0 ),
- part = IrcColor.FromCode ( 3 ),
- quit = IrcColor.FromCode ( 2 ),
- topic = IrcColor.FromCode ( 3 ),
- wallops = IrcColor.FromCode ( 5 ),
- whois = IrcColor.FromCode ( 0 )
- };
- public static Theme mIrcModern => _mircModern;
- }
- public struct IrcPalette
- {
- private Color [ ] pal;
- private bool defaultPalette;
- private const int max = 16;
- [JsonProperty ( PropertyName = "DefaultPalette" )]
- public bool DefaultPalette => defaultPalette;
- private string name;
- [JsonProperty ( PropertyName = "Name" )]
- public string Name => name;
- private bool _isok;
- [JsonIgnore]
- public bool IsOk => _isok;
- private static Dictionary<string, IrcPalette> _cache = new Dictionary<string, IrcPalette> ( );
- public static Dictionary<string, IrcPalette> AvailablePalettes => _cache;
- [JsonConstructor]
- private IrcPalette ( string name, bool defaultPalette = false )
- {
- this.defaultPalette = defaultPalette;
- pal = new Color [ max ];
- _isok = true;
- this.name = name;
- if ( !_cache.ContainsKey ( name ) )
- {
- // Console.WriteLine ( "Adding key {0}", name );
- _cache.Add ( name, this );
- }
- }
- public override bool Equals ( object obj )
- {
- return base.Equals ( obj );
- }
- public override int GetHashCode ( )
- {
- return base.GetHashCode ( );
- }
- public void OverrideConsolePalette ( )
- {
- // Console.WriteLine ( "Overriding Console Palette" );
- ConsoleScreenBufferInfoEx info = new ConsoleScreenBufferInfoEx ( );
- info.cbSize = 96;
- IntPtr hConsole = ConsoleHandle;
- GetConsoleScreenBufferInfoEx ( hConsole, ref info );
- for ( int i = 0; i < pal.Length; i++ )
- {
- info.ColorTable [ i ] = new ColorRef ( pal [ i ] );
- SetConsoleScreenBufferInfoEx ( hConsole, ref info );
- SetConsoleTextAttribute ( hConsole, ( ushort ) i );
- }
- }
- public Color GetColor ( int code )
- {
- if ( code >= 0 && code < max )
- return ( pal != null ) ? pal [ code ] : Color.White;
- throw new IndexOutOfRangeException ( "code" );
- }
- public Color GetColor ( Colors code )
- {
- return GetColor ( ( int ) code );
- }
- public static bool operator == ( IrcPalette left, IrcPalette right )
- {
- return ( left.Equals ( right ) );
- }
- public static bool operator != ( IrcPalette left, IrcPalette right )
- {
- return !( left.Equals ( right ) );
- }
- public static IrcPalette Default => _cache.ContainsKey ( "Default" ) ? _cache [ "Default" ] : _default;
- public static IrcPalette Blizzy => _cache.ContainsKey ( "Blizzy" ) ? _cache [ "Blizzy" ] : _blizzy;
- public static IrcPalette Modern => _cache.ContainsKey ( "Modern" ) ? _cache [ "Modern" ] : _modern;
- public static IrcPalette Monochrome => _cache.ContainsKey ( "Monochrome" ) ? _cache [ "Monochrome" ] : _monochrome;
- public static IrcPalette PlacidHues => _cache.ContainsKey ( "PlacidHues" ) ? _cache [ "PlacidHues" ] : _placidHues;
- public static IrcPalette RainbowSky => _cache.ContainsKey ( "RainbowSky" ) ? _cache [ "RainbowSky" ] : _rainbowSky;
- private static IrcPalette _default => new IrcPalette ( "Default", true )
- {
- pal = new Color [ max ]
- {
- IrcColor.White, // White
- IrcColor.Black, // Black
- IrcColor.Blue,
- IrcColor.Green,
- IrcColor.Red,
- IrcColor.Maroon,
- IrcColor.Purple,
- IrcColor.Orange,
- IrcColor.Yellow,
- IrcColor.LightGreen,
- IrcColor.DarkCyan,
- IrcColor.Cyan,
- IrcColor.Royale,
- IrcColor.Magenta,
- IrcColor.Gray,
- IrcColor.LightGray
- }
- };
- private static IrcPalette _blizzy => new IrcPalette ( "Blizzy" )
- {
- pal = new Color [ max ]
- {
- IrcColor.White, // White
- IrcColor.Black, // Black
- IrcColor.Blue,
- IrcColor.Green,
- IrcColor.Red,
- IrcColor.Maroon,
- IrcColor.Purple,
- IrcColor.Orange,
- IrcColor.Yellow,
- IrcColor.LightGreen,
- IrcColor.DarkCyan,
- IrcColor.Cyan,
- IrcColor.Royale,
- new IrcColor( Rgb ( 128, 255, 255 ), (Colors)13, "#80ffff" ),
- IrcColor.Gray,
- IrcColor.LightGray
- }
- };
- private static IrcPalette _modern => new IrcPalette ( "Modern" )
- {
- pal = new Color [ max ]
- {
- IrcColor.White, // White
- IrcColor.Black, // Black
- new IrcColor(Rgb(0 ,0,167), (Colors)2, "#0000a7"),
- new IrcColor(Rgb(43 ,89,50) ,(Colors)3, "#2b5932"),
- new IrcColor(Rgb(240,0,0), (Colors)4, "#f00000" ),
- new IrcColor(Rgb(136,72,72), (Colors)5, "#884848"),
- new IrcColor(Rgb(112,0,128), (Colors)6, "#700080"),
- new IrcColor(Rgb(208,104,0), (Colors)7, "#d06800"),
- new IrcColor(Rgb(248,232,24), (Colors)8, "#f8e818"),
- new IrcColor(Rgb(0 ,224,0), (Colors)9, "#00e000"),
- new IrcColor(Rgb(20,114,1), (Colors)10, "#147201"),
- IrcColor.Cyan,
- new IrcColor(Rgb(0,0,252), (Colors)12, "#0000fc"),
- IrcColor.Magenta,
- IrcColor.Gray,
- IrcColor.LightGray
- }
- };
- private static IrcPalette _monochrome => new IrcPalette ( "Monochrome" )
- {
- pal = new Color [ max ]
- {
- IrcColor.White, // White
- IrcColor.Black, // Black
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray,
- IrcColor.Gray
- }
- };
- private static IrcPalette _placidHues => new IrcPalette ( "PlacidHues" )
- {
- pal = new Color [ max ]
- {
- IrcColor.White,
- IrcColor.Black,
- new IrcColor(Rgb(108,47,0), Colors.Orange, "#6c2f00"),
- new IrcColor(Rgb(198,2,23), Colors.Red, "#c60217"),
- IrcColor.Red,
- new IrcColor(Rgb(67,78,167), Colors.Blue, "#434ea7"),
- new IrcColor(Rgb(62,100,100), Colors.DarkCyan, "#3e6464"),
- new IrcColor(Rgb(255,128,0), Colors.Orange, "#ff8000"),
- IrcColor.Yellow,
- new IrcColor(Rgb(0,128,64), Colors.Green, "#008040"),
- new IrcColor(Rgb(0,144,144), Colors.DarkCyan, "#009090"),
- new IrcColor(Rgb(25,96,151), Colors.Blue, "#196097"),
- IrcColor.Blue,
- IrcColor.Magenta,
- new IrcColor(Rgb(100,100,100), Colors.Gray, "#646464"),
- new IrcColor(Rgb(130,130,130), Colors.Gray, "#828282")
- }
- };
- private static IrcPalette _rainbowSky => new IrcPalette ( "RainbowSky" )
- {
- pal = new Color [ max ]
- {
- IrcColor.White,
- new IrcColor (Rgb(55,55,55), Colors.Gray, "#373737"),
- new IrcColor(Rgb(112,112,184), Colors.Blue, "#7070b8"),
- new IrcColor(Rgb(0,128,255), Colors.Blue, "#0080ff"),
- IrcColor.Red,
- IrcColor.Orange,
- IrcColor.Yellow,
- new IrcColor(Rgb (0,168,0), Colors.Green, "#00a800"),
- IrcColor.DarkCyan,
- IrcColor.Cyan,
- new IrcColor (Rgb(0,0,252), Colors.Blue, "#0000fc"),
- IrcColor.Magenta,
- IrcColor.Gray,
- IrcColor.Purple,
- IrcColor.Gray,
- IrcColor.LightGray
- }
- };
- }
- public struct IrcColor
- {
- private static IrcPalette iPalette = IrcPalette.Default;
- public static IrcPalette IrcColorPalette => iPalette;
- private Colors clr;
- private Color xlr;
- private string name;
- public Colors IColor => clr;
- public string Name => name;
- [JsonConstructor]
- public IrcColor ( Colors color, string name )
- {
- clr = color;
- xlr = iPalette.IsOk ? iPalette.GetColor ( clr ) : ( IrcPalette.Default.IsOk ? IrcPalette.Default.GetColor ( clr ) : Color.White );
- this.name = name;
- }
- public IrcColor ( Color color, Colors type, string name )
- {
- clr = type;
- xlr = color;
- this.name = name;
- }
- public static void SetPalette ( IrcPalette palette )
- {
- iPalette = palette;
- }
- //public IrcColor (Color color )
- //{
- // clr = Colors.Overridden;
- // xlr = color;
- // iPalette = IrcPalette.Default;
- //}
- public static IrcColor Black = new IrcColor ( Color.Black, Colors.Black, "Black" );
- public static IrcColor Blue = new IrcColor ( Color.Blue, Colors.Blue, "Blue" );
- public static IrcColor Cyan = new IrcColor ( Color.Cyan, Colors.Cyan, "Cyan" );
- public static IrcColor DarkCyan = new IrcColor ( Color.DarkCyan, Colors.DarkCyan, "DarkCyan" );
- public static IrcColor Gray = new IrcColor ( Color.Gray, Colors.Gray, "Gray" );
- public static IrcColor LightGray = new IrcColor ( Color.LightGray, Colors.LightGray, "LightGray" );
- public static IrcColor Green = new IrcColor ( Color.Green, Colors.Green, "Green" );
- public static IrcColor LightGreen = new IrcColor ( Color.LightGreen, Colors.LightGreen, "LightGreen" );
- public static IrcColor Magenta = new IrcColor ( Color.Magenta, Colors.Magenta, "Magenta" );
- public static IrcColor Maroon = new IrcColor ( Color.Maroon, Colors.Maroon, "Maroon" );
- public static IrcColor Orange = new IrcColor ( Color.Orange, Colors.Orange, "Orange" );
- public static IrcColor Purple = new IrcColor ( Color.Purple, Colors.Purple, "Purple" );
- public static IrcColor Red = new IrcColor ( Color.Red, Colors.Red, "Red" );
- public static IrcColor Royale = new IrcColor ( Rgb ( 174, 174, 255 ), Colors.Royale, "Royale" );
- public static IrcColor White = new IrcColor ( Color.White, Colors.White, "White" );
- public static IrcColor Yellow = new IrcColor ( Color.Yellow, Colors.Yellow, "Yellow" );
- public static IrcColor FromCode ( int code )
- {
- // Console.WriteLine ( "Code = {0}", ( Colors ) code );
- if ( code < -1 || code > MAX )
- throw new ArgumentOutOfRangeException ( "code" );
- switch ( ( Colors ) code )
- {
- case Colors.Black:
- return Black;
- case Colors.Blue:
- return Blue;
- case Colors.Cyan:
- return Cyan;
- case Colors.DarkCyan:
- return DarkCyan;
- case Colors.Gray:
- return Gray;
- case Colors.Green:
- return Green;
- case Colors.LightGray:
- return LightGray;
- case Colors.LightGreen:
- return LightGreen;
- case Colors.Magenta:
- return Magenta;
- case Colors.Maroon:
- return Maroon;
- case Colors.Orange:
- return Orange;
- case Colors.Purple:
- return Purple;
- case Colors.Red:
- return Red;
- case Colors.Royale:
- return Royale;
- case Colors.White:
- return White;
- case Colors.Yellow:
- return Yellow;
- case Colors.Overridden:
- default:
- return new IrcColor ( iPalette.IsOk ? iPalette.GetColor ( ( Colors ) code ) : Color.White, Colors.Overridden, "Custom" );
- }
- }
- public ConsoleColor ToConsoleColor ( )
- {
- return ( ConsoleColor ) ( ( int ) clr );
- }
- public IrcColor Override ( string hex )
- {
- if ( !hex.StartsWith ( "#" ) )
- {
- hex = hex.Insert ( 0, "#" );
- }
- return new IrcColor { xlr = Color.FromName ( hex ) };
- }
- public IrcColor Override ( byte r, byte g, byte b )
- {
- return ( Override ( ToHex ( r, g, b, 255 ) ) );
- }
- public static implicit operator Color ( IrcColor iclr )
- {
- //if ( iPalette == IrcPalette.Default )
- //{
- //
- // if(iclr.clr == Colors.Overridden)
- // {
- // return iclr.xlr;
- // }
- // return FromCode ( (int)iclr.clr );
- //
- // #region Old
- switch ( iclr.clr )
- {
- case Colors.Black:
- return Color.Black;
- case Colors.Blue:
- return Color.Blue;
- case Colors.Cyan:
- return Color.Cyan;
- case Colors.DarkCyan:
- return Color.DarkCyan;
- case Colors.Gray:
- return Color.Gray;
- case Colors.LightGray:
- return Color.LightGray;
- case Colors.Green:
- return Color.Green;
- case Colors.LightGreen:
- return Color.LightGreen;
- case Colors.Magenta:
- return Color.Magenta;
- case Colors.Maroon:
- return Color.Maroon;
- case Colors.Orange:
- return Color.Orange;
- case Colors.Purple:
- return Color.Purple;
- case Colors.Red:
- return Color.Red;
- case Colors.Royale:
- return Color.RoyalBlue;
- case Colors.White:
- return Color.White;
- case Colors.Yellow:
- return Color.Yellow;
- case Colors.Overridden:
- return iclr.xlr;
- }
- // #endregion
- //
- //} else
- if ( !iPalette.IsOk )
- SetPalette ( IrcPalette.Default );
- return iPalette.GetColor ( iclr.clr );
- }
- public override string ToString ( )
- {
- return name;
- }
- }
- internal class NativeMethods
- {
- public const int ForegroundBlue = 0x01;
- public const int ForegroundGreen = 0x02;
- public const int ForegroundRed = 0x04;
- public const int ForegroundIntensity = 0x08;
- public const int MAX = ForegroundIntensity | ForegroundRed | ForegroundGreen | ForegroundBlue;
- [StructLayout ( LayoutKind.Sequential )]
- public struct ColorRef
- {
- public uint ColorDWORD;
- public ColorRef ( System.Drawing.Color color )
- {
- ColorDWORD = ( uint ) color.R + ( ( ( uint ) color.G ) << 8 ) + ( ( ( uint ) color.B ) << 16 );
- }
- public System.Drawing.Color GetColor ( )
- {
- return System.Drawing.Color.FromArgb ( ( int ) ( 0x000000FFU & ColorDWORD ),
- ( int ) ( 0x0000FF00U & ColorDWORD ) >> 8, ( int ) ( 0x00FF0000U & ColorDWORD ) >> 16 );
- }
- public void SetColor ( System.Drawing.Color color )
- {
- ColorDWORD = ( uint ) color.R + ( ( ( uint ) color.G ) << 8 ) + ( ( ( uint ) color.B ) << 16 );
- }
- }
- [StructLayout ( LayoutKind.Sequential )]
- public struct Coord
- {
- public short X;
- public short Y;
- }
- public struct SmallRect
- {
- public short Left;
- public short Top;
- public short Right;
- public short Bottom;
- }
- [StructLayout ( LayoutKind.Sequential )]
- public struct ConsoleScreenBufferInfoEx
- {
- public uint cbSize;
- public Coord dwSize;
- public Coord dwCursorPosition;
- public short wAttributes;
- public SmallRect srWindow;
- public Coord dwMaximumWindowSize;
- public ushort wPopupAttributes;
- public bool bFullscreenSupported;
- [MarshalAs ( UnmanagedType.ByValArray, SizeConst = 16 )]
- public ColorRef [ ] ColorTable;
- public static ConsoleScreenBufferInfoEx Create ( )
- {
- return new ConsoleScreenBufferInfoEx { cbSize = 96 };
- }
- }
- [StructLayout ( LayoutKind.Sequential )]
- public struct Rgb
- {
- private byte byRed, byGreen, byBlue, RESERVED;
- public Rgb ( Color colorIn )
- {
- byRed = colorIn.R;
- byGreen = colorIn.G;
- byBlue = colorIn.B;
- RESERVED = 0;
- }
- public Rgb ( byte R, byte G, byte B )
- {
- byRed = R;
- byGreen = G;
- byBlue = B;
- RESERVED = 0;
- }
- public static implicit operator Color ( Rgb rgb )
- {
- return Color.FromArgb ( rgb.byRed, rgb.byGreen, rgb.byBlue );
- }
- public int ToInt32 ( )
- {
- byte [ ] rgbcolors = new byte [ 4 ];
- rgbcolors [ 0 ] = byRed;
- rgbcolors [ 1 ] = byGreen;
- rgbcolors [ 2 ] = byBlue;
- rgbcolors [ 3 ] = RESERVED;
- return BitConverter.ToInt32 ( rgbcolors, 0 );
- }
- }
- public enum ShowWindowCommands
- {
- /// <summary>
- /// Hides the window and activates another window.
- /// </summary>
- Hide = 0,
- /// <summary>
- /// Activates and displays a window. If the window is minimized or
- /// maximized, the system restores it to its original size and position.
- /// An application should specify this flag when displaying the window
- /// for the first time.
- /// </summary>
- Normal = 1,
- /// <summary>
- /// Activates the window and displays it as a minimized window.
- /// </summary>
- ShowMinimized = 2,
- /// <summary>
- /// Maximizes the specified window.
- /// </summary>
- Maximize = 3, // is this the right value?
- /// <summary>
- /// Activates the window and displays it as a maximized window.
- /// </summary>
- ShowMaximized = 3,
- /// <summary>
- /// Displays a window in its most recent size and position. This value
- /// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
- /// the window is not activated.
- /// </summary>
- ShowNoActivate = 4,
- /// <summary>
- /// Activates the window and displays it in its current size and position.
- /// </summary>
- Show = 5,
- /// <summary>
- /// Minimizes the specified window and activates the next top-level
- /// window in the Z order.
- /// </summary>
- Minimize = 6,
- /// <summary>
- /// Displays the window as a minimized window. This value is similar to
- /// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
- /// window is not activated.
- /// </summary>
- ShowMinNoActive = 7,
- /// <summary>
- /// Displays the window in its current size and position. This value is
- /// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
- /// window is not activated.
- /// </summary>
- ShowNA = 8,
- /// <summary>
- /// Activates and displays the window. If the window is minimized or
- /// maximized, the system restores it to its original size and position.
- /// An application should specify this flag when restoring a minimized window.
- /// </summary>
- Restore = 9,
- /// <summary>
- /// Sets the show state based on the SW_* value specified in the
- /// STARTUPINFO structure passed to the CreateProcess function by the
- /// program that started the application.
- /// </summary>
- ShowDefault = 10,
- /// <summary>
- /// <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
- /// that owns the window is not responding. This flag should only be
- /// used when minimizing windows from a different thread.
- /// </summary>
- ForceMinimize = 11
- }
- public static IntPtr ConsoleHandle { get; private set; }
- public const int StdOutputHandle = -11;
- [DllImport ( "kernel32.dll", SetLastError = true )]
- private static extern IntPtr GetStdHandle ( int nStdHandle );
- public static void SetConsoleHandle ( )
- {
- ConsoleHandle = GetStdHandle ( StdOutputHandle );
- }
- [DllImport ( "kernel32.dll", SetLastError = true )]
- public static extern bool GetConsoleScreenBufferInfoEx (
- IntPtr hConsoleOutput,
- ref ConsoleScreenBufferInfoEx consoleScreenBufferInfo
- );
- [DllImport ( "kernel32.dll", SetLastError = true )]
- public static extern bool SetConsoleScreenBufferInfoEx (
- IntPtr consoleOutput,
- ref ConsoleScreenBufferInfoEx consoleScreenBufferInfoEx
- );
- [DllImport ( "kernel32.dll" )]
- public static extern bool SetConsoleTextAttribute ( IntPtr hConsoleOutput,
- ushort wAttributes );
- [Conditional ( "DEBUG" ), DllImport ( "Kernel32.dll" )]
- public static extern void AllocConsole ( );
- [Conditional ( "DEBUG" ), DllImport ( "Kernel32.dll" )]
- public static extern void FreeConsole ( );
- [DllImport ( "user32.dll" )] // import lockwindow to remove flashing
- public static extern bool LockWindowUpdate ( IntPtr hWndLock );
- [DllImport ( "user32.dll", CharSet = CharSet.Unicode )]
- public static extern int MessageBox ( IntPtr hWnd, string caption, string title, int flags );
- [DllImport ( "user32.dll" )]
- public static extern bool ShowWindowAsync ( IntPtr hWnd, ShowWindowCommands nCmdShow );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement