ImaFoSho

[C#] Hero Theme

Nov 17th, 2012
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.54 KB | None | 0 0
  1. //Hero Theme
  2. //By: FoSho
  3. //Theme Base: AeonHack
  4. //Version: 1.0.0.0
  5.  
  6. using System.Drawing.Drawing2D;
  7. using System.ComponentModel;
  8. using System.Runtime.InteropServices;
  9. abstract class Theme : ContainerControl
  10. {
  11.     protected Graphics G;
  12.     Theme()
  13.     {
  14.         SetStyle((ControlStyles)139270, true);
  15.     }
  16.     private bool ParentIsForm;
  17.     protected override void OnHandleCreated(EventArgs e)
  18.     {
  19.         Dock = DockStyle.Fill;
  20.         ParentIsForm = Parent is Form;
  21.         if (ParentIsForm) {
  22.             if (!_TransparencyKey == Color.Empty) ParentForm.TransparencyKey = _TransparencyKey;
  23.             ParentForm.FormBorderStyle = FormBorderStyle.None;
  24.         }
  25.         base.OnHandleCreated(e);
  26.     }
  27.     override string Text {
  28.         get { return base.Text; }
  29.         set {
  30.             base.Text = v;
  31.             Invalidate();
  32.         }
  33.     }
  34.     private bool _Resizable = true;
  35.     bool Resizable {
  36.         get { return _Resizable; }
  37.         set { _Resizable = value; }
  38.     }
  39.     private int _MoveHeight = 24;
  40.     int MoveHeight {
  41.         get { return _MoveHeight; }
  42.         set {
  43.             _MoveHeight = v;
  44.             Header = new Rectangle(7, 7, Width - 14, _MoveHeight - 7);
  45.         }
  46.     }
  47.     private IntPtr Flag;
  48.     protected override void OnMouseDown(MouseEventArgs e)
  49.     {
  50.         if (!e.Button == MouseButtons.Left) return;
  51.         if (ParentIsForm) if (ParentForm.WindowState == FormWindowState.Maximized) return;  
  52.         if (Header.Contains(e.Location)) {
  53.             Flag = new IntPtr(2);
  54.         }
  55.         else if (Current.Position == 0 | !_Resizable) {
  56.             return;
  57.         }
  58.         else {
  59.             Flag = new IntPtr(Current.Position);
  60.         }
  61.         Capture = false;
  62.         DefWndProc(Message.Create(Parent.Handle, 161, Flag, null));
  63.         base.OnMouseDown(e);
  64.     }
  65.     private struct Pointer
  66.     {
  67.         readonly Cursor Cursor;
  68.         readonly byte Position;
  69.         Pointer(Cursor c, byte p)
  70.         {
  71.             Cursor = c;
  72.             Position = p;
  73.         }
  74.     }
  75.     private bool F1;
  76.     private bool F2;
  77.     private bool F3;
  78.     private bool F4;
  79.     private Point PTC;
  80.     private Pointer GetPointer()
  81.     {
  82.         PTC = PointToClient(MousePosition);
  83.         F1 = PTC.X < 7;
  84.         F2 = PTC.X > Width - 7;
  85.         F3 = PTC.Y < 7;
  86.         F4 = PTC.Y > Height - 7;
  87.         if (F1 & F3) return new Pointer(Cursors.SizeNWSE, 13);
  88.         if (F1 & F4) return new Pointer(Cursors.SizeNESW, 16);
  89.         if (F2 & F3) return new Pointer(Cursors.SizeNESW, 14);
  90.         if (F2 & F4) return new Pointer(Cursors.SizeNWSE, 17);
  91.         if (F1) return new Pointer(Cursors.SizeWE, 10);
  92.         if (F2) return new Pointer(Cursors.SizeWE, 11);
  93.         if (F3) return new Pointer(Cursors.SizeNS, 12);
  94.         if (F4) return new Pointer(Cursors.SizeNS, 15);
  95.         return new Pointer(Cursors.Default, 0);
  96.     }
  97.     private Pointer Current;
  98.     private Pointer Pending;
  99.     private void SetCurrent()
  100.     {
  101.         Pending = GetPointer();
  102.         if (Current.Position == Pending.Position) return;
  103.         Current = GetPointer();
  104.         Cursor = Current.Cursor;
  105.     }
  106.     protected override void OnMouseMove(MouseEventArgs e)
  107.     {
  108.         if (_Resizable) SetCurrent();
  109.         base.OnMouseMove(e);
  110.     }
  111.     protected Rectangle Header;
  112.     protected override void OnSizeChanged(EventArgs e)
  113.     {
  114.         if (Width == 0 || Height == 0) return;
  115.         Header = new Rectangle(7, 7, Width - 14, _MoveHeight - 7);
  116.         Invalidate();
  117.         base.OnSizeChanged(e);
  118.     }
  119.     abstract void PaintHook();
  120.     protected override sealed void OnPaint(PaintEventArgs e)
  121.     {
  122.         if (Width == 0 || Height == 0) return;
  123.         G = e.Graphics;
  124.         PaintHook();
  125.     }
  126.     private Color _TransparencyKey;
  127.     Color TransparencyKey {
  128.         get { return _TransparencyKey; }
  129.         set {
  130.             _TransparencyKey = v;
  131.             Invalidate();
  132.         }
  133.     }
  134.     private Image _Image;
  135.     Image Image {
  136.         get { return _Image; }
  137.         set {
  138.             _Image = value;
  139.             Invalidate();
  140.         }
  141.     }
  142.     int ImageWidth {
  143.         get {
  144.             if (_Image == null) return 0;
  145.             return _Image.Width;
  146.         }
  147.     }
  148.     private Size _Size;
  149.     private Rectangle _Rectangle;
  150.     private LinearGradientBrush _Gradient;
  151.     private SolidBrush _Brush;
  152.     protected void DrawCorners(Color c, Rectangle rect)
  153.     {
  154.         _Brush = new SolidBrush(c);
  155.         G.FillRectangle(_Brush, rect.X, rect.Y, 1, 1);
  156.         G.FillRectangle(_Brush, rect.X + (rect.Width - 1), rect.Y, 1, 1);
  157.         G.FillRectangle(_Brush, rect.X, rect.Y + (rect.Height - 1), 1, 1);
  158.         G.FillRectangle(_Brush, rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), 1, 1);
  159.     }
  160.     protected void DrawBorders(Pen p1, Pen p2, Rectangle rect)
  161.     {
  162.         G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
  163.         G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3);
  164.     }
  165.     protected void DrawText(HorizontalAlignment a, Color c, int x)
  166.     {
  167.         DrawText(a, c, x, 0);
  168.     }
  169.     protected void DrawText(HorizontalAlignment a, Color c, int x, int y)
  170.     {
  171.         if (string.IsNullOrEmpty(Text)) return;
  172.         _Size = G.MeasureString(Text, Font).ToSize;
  173.         _Brush = new SolidBrush(c);
  174.         switch (a) {
  175.             case HorizontalAlignment.Left:
  176.                 G.DrawString(Text, Font, _Brush, x, _MoveHeight / 2 - _Size.Height / 2 + y);
  177.             case HorizontalAlignment.Right:
  178.                 G.DrawString(Text, Font, _Brush, Width - _Size.Width - x, _MoveHeight / 2 - _Size.Height / 2 + y);
  179.             case HorizontalAlignment.Center:
  180.                 G.DrawString(Text, Font, _Brush, Width / 2 - _Size.Width / 2 + x, _MoveHeight / 2 - _Size.Height / 2 + y);
  181.         }
  182.     }
  183.     protected void DrawIcon(HorizontalAlignment a, int x)
  184.     {
  185.         DrawIcon(a, x, 0);
  186.     }
  187.     protected void DrawIcon(HorizontalAlignment a, int x, int y)
  188.     {
  189.         if (_Image == null) return;
  190.         switch (a) {
  191.             case HorizontalAlignment.Left:
  192.                 G.DrawImage(_Image, x, _MoveHeight / 2 - _Image.Height / 2 + y);
  193.             case HorizontalAlignment.Right:
  194.                 G.DrawImage(_Image, Width - _Image.Width - x, _MoveHeight / 2 - _Image.Height / 2 + y);
  195.             case HorizontalAlignment.Center:
  196.                 G.DrawImage(_Image, Width / 2 - _Image.Width / 2, _MoveHeight / 2 - _Image.Height / 2);
  197.         }
  198.     }
  199.     protected void DrawGradient(Color c1, Color c2, int x, int y, int width, int height, float angle)
  200.     {
  201.         _Rectangle = new Rectangle(x, y, width, height);
  202.         _Gradient = new LinearGradientBrush(_Rectangle, c1, c2, angle);
  203.         G.FillRectangle(_Gradient, _Rectangle);
  204.     }
  205. }
  206. abstract class ThemeControl : Control
  207. {
  208.     protected Graphics G;
  209.     protected Bitmap B;
  210.     ThemeControl()
  211.     {
  212.         SetStyle((ControlStyles)139270, true);
  213.         B = new Bitmap(1, 1);
  214.         G = Graphics.FromImage(B);
  215.     }
  216.     void AllowTransparent()
  217.     {
  218.         SetStyle(ControlStyles.Opaque, false);
  219.         SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  220.     }
  221.     override string Text {
  222.         get { return base.Text; }
  223.         set {
  224.             base.Text = v;
  225.             Invalidate();
  226.         }
  227.     }
  228.     protected enum State : byte
  229.     {
  230.         MouseNone = 0,
  231.         MouseOver = 1,
  232.         MouseDown = 2
  233.     }
  234.     protected State MouseState;
  235.     protected override void OnMouseLeave(EventArgs e)
  236.     {
  237.         ChangeMouseState(State.MouseNone);
  238.         base.OnMouseLeave(e);
  239.     }
  240.     protected override void OnMouseEnter(EventArgs e)
  241.     {
  242.         ChangeMouseState(State.MouseOver);
  243.         base.OnMouseEnter(e);
  244.     }
  245.     protected override void OnMouseUp(MouseEventArgs e)
  246.     {
  247.         ChangeMouseState(State.MouseOver);
  248.         base.OnMouseUp(e);
  249.     }
  250.     protected override void OnMouseDown(MouseEventArgs e)
  251.     {
  252.         if (e.Button == MouseButtons.Left) ChangeMouseState(State.MouseDown);
  253.         base.OnMouseDown(e);
  254.     }
  255.     private void ChangeMouseState(State e)
  256.     {
  257.         MouseState = e;
  258.         Invalidate();
  259.     }
  260.     abstract void PaintHook();
  261.     protected override sealed void OnPaint(PaintEventArgs e)
  262.     {
  263.         if (Width == 0 || Height == 0) return;
  264.         PaintHook();
  265.         e.Graphics.DrawImage(B, 0, 0);
  266.     }
  267.     protected override void OnSizeChanged(EventArgs e)
  268.     {
  269.         if (!Width == 0 && !Height == 0) {
  270.             B = new Bitmap(Width, Height);
  271.             G = Graphics.FromImage(B);
  272.             Invalidate();
  273.         }
  274.         base.OnSizeChanged(e);
  275.     }
  276.     private bool _NoRounding;
  277.     bool NoRounding {
  278.         get { return _NoRounding; }
  279.         set {
  280.             _NoRounding = v;
  281.             Invalidate();
  282.         }
  283.     }
  284.     private Image _Image;
  285.     Image Image {
  286.         get { return _Image; }
  287.         set {
  288.             _Image = value;
  289.             Invalidate();
  290.         }
  291.     }
  292.     int ImageWidth {
  293.         get {
  294.             if (_Image == null) return 0;
  295.             return _Image.Width;
  296.         }
  297.     }
  298.     int ImageTop {
  299.         get {
  300.             if (_Image == null) return 0;
  301.             return Height / 2 - _Image.Height / 2;
  302.         }
  303.     }
  304.     private Size _Size;
  305.     private Rectangle _Rectangle;
  306.     private LinearGradientBrush _Gradient;
  307.     private SolidBrush _Brush;
  308.     protected void DrawCorners(Color c, Rectangle rect)
  309.     {
  310.         if (_NoRounding) return;
  311.         B.SetPixel(rect.X, rect.Y, c);
  312.         B.SetPixel(rect.X + (rect.Width - 1), rect.Y, c);
  313.         B.SetPixel(rect.X, rect.Y + (rect.Height - 1), c);
  314.         B.SetPixel(rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), c);
  315.     }
  316.     protected void DrawBorders(Pen p1, Pen p2, Rectangle rect)
  317.     {
  318.         G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
  319.         G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3);
  320.     }
  321.     protected void DrawText(HorizontalAlignment a, Color c, int x)
  322.     {
  323.         DrawText(a, c, x, 0);
  324.     }
  325.     protected void DrawText(HorizontalAlignment a, Color c, int x, int y)
  326.     {
  327.         if (string.IsNullOrEmpty(Text)) return;
  328.         _Size = G.MeasureString(Text, Font).ToSize;
  329.         _Brush = new SolidBrush(c);
  330.         switch (a) {
  331.             case HorizontalAlignment.Left:
  332.                 G.DrawString(Text, Font, _Brush, x, Height / 2 - _Size.Height / 2 + y);
  333.             case HorizontalAlignment.Right:
  334.                 G.DrawString(Text, Font, _Brush, Width - _Size.Width - x, Height / 2 - _Size.Height / 2 + y);
  335.             case HorizontalAlignment.Center:
  336.                 G.DrawString(Text, Font, _Brush, Width / 2 - _Size.Width / 2 + x, Height / 2 - _Size.Height / 2 + y);
  337.         }
  338.     }
  339.     protected void DrawIcon(HorizontalAlignment a, int x)
  340.     {
  341.         DrawIcon(a, x, 0);
  342.     }
  343.     protected void DrawIcon(HorizontalAlignment a, int x, int y)
  344.     {
  345.         if (_Image == null) return;
  346.         switch (a) {
  347.             case HorizontalAlignment.Left:
  348.                 G.DrawImage(_Image, x, Height / 2 - _Image.Height / 2 + y);
  349.             case HorizontalAlignment.Right:
  350.                 G.DrawImage(_Image, Width - _Image.Width - x, Height / 2 - _Image.Height / 2 + y);
  351.             case HorizontalAlignment.Center:
  352.                 G.DrawImage(_Image, Width / 2 - _Image.Width / 2, Height / 2 - _Image.Height / 2);
  353.         }
  354.     }
  355.     protected void DrawGradient(Color c1, Color c2, int x, int y, int width, int height, float angle)
  356.     {
  357.         _Rectangle = new Rectangle(x, y, width, height);
  358.         _Gradient = new LinearGradientBrush(_Rectangle, c1, c2, angle);
  359.         G.FillRectangle(_Gradient, _Rectangle);
  360.     }
  361. }
  362. abstract class ThemeContainerControl : ContainerControl
  363. {
  364.     protected Graphics G;
  365.     protected Bitmap B;
  366.     ThemeContainerControl()
  367.     {
  368.         SetStyle((ControlStyles)139270, true);
  369.         B = new Bitmap(1, 1);
  370.         G = Graphics.FromImage(B);
  371.     }
  372.     void AllowTransparent()
  373.     {
  374.         SetStyle(ControlStyles.Opaque, false);
  375.         SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  376.     }
  377.     abstract void PaintHook();
  378.     protected override sealed void OnPaint(PaintEventArgs e)
  379.     {
  380.         if (Width == 0 || Height == 0) return;
  381.         PaintHook();
  382.         e.Graphics.DrawImage(B, 0, 0);
  383.     }
  384.     protected override void OnSizeChanged(EventArgs e)
  385.     {
  386.         if (!Width == 0 && !Height == 0) {
  387.             B = new Bitmap(Width, Height);
  388.             G = Graphics.FromImage(B);
  389.             Invalidate();
  390.         }
  391.         base.OnSizeChanged(e);
  392.     }
  393.     private bool _NoRounding;
  394.     bool NoRounding {
  395.         get { return _NoRounding; }
  396.         set {
  397.             _NoRounding = v;
  398.             Invalidate();
  399.         }
  400.     }
  401.     private Rectangle _Rectangle;
  402.     private LinearGradientBrush _Gradient;
  403.     protected void DrawCorners(Color c, Rectangle rect)
  404.     {
  405.         if (_NoRounding) return;
  406.         B.SetPixel(rect.X, rect.Y, c);
  407.         B.SetPixel(rect.X + (rect.Width - 1), rect.Y, c);
  408.         B.SetPixel(rect.X, rect.Y + (rect.Height - 1), c);
  409.         B.SetPixel(rect.X + (rect.Width - 1), rect.Y + (rect.Height - 1), c);
  410.     }
  411.     protected void DrawBorders(Pen p1, Pen p2, Rectangle rect)
  412.     {
  413.         G.DrawRectangle(p1, rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
  414.         G.DrawRectangle(p2, rect.X + 1, rect.Y + 1, rect.Width - 3, rect.Height - 3);
  415.     }
  416.     protected void DrawGradient(Color c1, Color c2, int x, int y, int width, int height, float angle)
  417.     {
  418.         _Rectangle = new Rectangle(x, y, width, height);
  419.         _Gradient = new LinearGradientBrush(_Rectangle, c1, c2, angle);
  420.         G.FillRectangle(_Gradient, _Rectangle);
  421.     }
  422. }
  423. class HeroTheme : Theme
  424. {
  425.     HeroTheme()
  426.     {
  427.         Resizable = false;
  428.         BackColor = Color.FromArgb(211, 211, 211);
  429.         MoveHeight = 25;
  430.         TransparencyKey = Color.Fuchsia;
  431.     }
  432.     public override void PaintHook()
  433.     {
  434.         G.Clear(BackColor);
  435.         G.Clear(Color.FromArgb(211, 211, 211));
  436.         DrawGradient(Color.FromArgb(62, 62, 62), Color.FromArgb(40, 40, 40), 0, 0, Width, 25, 90);
  437.         G.DrawLine(Pens.Black, 0, 25, Width, 25);
  438.         DrawCorners(Color.Fuchsia, ClientRectangle);
  439.         DrawBorders(Pens.Black, Pens.DimGray, ClientRectangle);
  440.         DrawText(HorizontalAlignment.Left, Color.FromArgb(211, 211, 211), 7, 1);
  441.     }
  442. }
  443. class HeroButton : ThemeControl
  444. {
  445.     override void PaintHook()
  446.     {
  447.         switch (MouseState) {
  448.             case State.MouseNone:
  449.                 G.Clear(Color.FromArgb(50, 50, 50));
  450.                 DrawGradient(Color.FromArgb(62, 62, 62), Color.FromArgb(40, 40, 40), 0, 0, Width, Height, 90);
  451.             case State.MouseOver:
  452.                 G.Clear(Color.Gray);
  453.                 DrawGradient(Color.FromArgb(62, 62, 62), Color.FromArgb(40, 40, 40), 0, 0, Width, Height, 90);
  454.             case State.MouseDown:
  455.                 G.Clear(Color.FromArgb(211, 211, 211));
  456.                 DrawGradient(Color.FromArgb(38, 38, 38), Color.FromArgb(40, 40, 40), 0, 0, Width, Height, 90);
  457.         }
  458.         DrawCorners(Color.Black, ClientRectangle);
  459.         DrawText(HorizontalAlignment.Center, Color.FromArgb(211, 211, 211), 0);
  460.     }
  461. }
  462. class HeroLabel : ThemeControl
  463. {
  464.     override void PaintHook()
  465.     {
  466.         G.Clear(BackColor);
  467.         DrawText(HorizontalAlignment.Center, Color.FromArgb(40, 40, 40), 0);
  468.     }
  469. }
  470. class HeroSeperater : ThemeControl
  471. {
  472.     override void PaintHook()
  473.     {
  474.         G.Clear(BackColor);
  475.         G.DrawLine(Pens.Gray, 0, (int)ClientRectangle.Y / 2, Width, 0);
  476.     }
  477. }
  478. class HeroGroupBox : ThemeContainerControl
  479. {
  480.     HeroGroupBox()
  481.     {
  482.         AllowTransparent();
  483.     }
  484.     override void PaintHook()
  485.     {
  486.         G.Clear(BackColor);
  487.         DrawBorders(Pens.Black, Pens.DimGray, ClientRectangle);
  488.         DrawCorners(Color.Fuchsia, ClientRectangle);
  489.     }
  490. }
  491. class HeroProgressBar : ThemeControl
  492. {
  493.     private int _Maximum = 100;
  494.     public int Maximum {
  495.         get { return _Maximum; }
  496.         set {
  497.             if (value < 1) value = 1;
  498.             if (value < _Value) _Value = value;
  499.             _Maximum = value;
  500.             Invalidate();
  501.         }
  502.     }
  503.     private int _Value = 50;
  504.     public int Value {
  505.         get { return _Value; }
  506.         set {
  507.             if (value > _Maximum) value = _Maximum;
  508.             if (value < 1) value = 1;
  509.             _Value = value;
  510.             Invalidate();
  511.         }
  512.     }
  513.     override void PaintHook()
  514.     {
  515.         G.Clear(Color.FromArgb(50, 50, 50));
  516.         if ((_Value > 0)) {
  517.             DrawGradient(Color.FromArgb(62, 62, 62), Color.FromArgb(40, 40, 40), 0, 0, (int)(_Value / _Maximum) * Width, Height, 90);
  518.         }
  519.         G.DrawRectangle(Pens.LightGray, 0, 0, Width - 1, Height - 1);
  520.         DrawBorders(Pens.Black, Pens.DimGray, ClientRectangle);
  521.     }
  522. }
  523. class HeroCheckBox : ThemeControl
  524. {
  525.     private bool _CheckedState;
  526.     public bool CheckedState {
  527.         get { return _CheckedState; }
  528.         set {
  529.             _CheckedState = v;
  530.             Invalidate();
  531.         }
  532.     }
  533.     HeroCheckBox()
  534.     {
  535.         Size = new Size(90, 15);
  536.         MinimumSize = new Size(16, 16);
  537.         MaximumSize = new Size(600, 16);
  538.         CheckedState = false;
  539.     }
  540.     public override void PaintHook()
  541.     {
  542.         G.Clear(BackColor);
  543.         switch (CheckedState) {
  544.             case true:
  545.                 DrawGradient(Color.FromArgb(62, 62, 62), Color.FromArgb(38, 38, 38), 0, 0, 15, 15, 90);
  546.                 DrawGradient(Color.FromArgb(80, 80, 80), Color.FromArgb(60, 60, 60), 3, 3, 9, 9, 90);
  547.                 DrawGradient(Color.FromArgb(80, 80, 80), Color.FromArgb(60, 60, 60), 4, 4, 7, 7, 90);
  548.             case false:
  549.                 DrawGradient(Color.FromArgb(211, 211, 211), Color.FromArgb(200, 200, 200), 0, 0, 15, 15, 90);
  550.         }
  551.         DrawBorders(Pens.Black, Pens.DimGray, new Rectangle(0, 0, 15, 15));
  552.         DrawText(HorizontalAlignment.Left, Color.FromArgb(30, 30, 30), 17, 0);
  553.     }
  554.     void  // ERROR: Handles clauses are not supported in C#
  555. changeCheck()
  556.     {
  557.         switch (CheckedState) {
  558.             case true:
  559.                 CheckedState = false;
  560.             case false:
  561.                 CheckedState = true;
  562.         }
  563.     }
  564. }
Advertisement
Add Comment
Please, Sign In to add comment