Advertisement
Guest User

Untitled

a guest
Dec 30th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.94 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. using MaterialSkin.Animations;
  6.  
  7. namespace MaterialSkin.Controls
  8. {
  9.     public class MaterialSingleLineTextField : Control, IMaterialControl
  10.     {
  11.         //Properties for managing the material design properties
  12.         public int Depth { get; set; }
  13.         public MaterialSkinManager SkinManager { get { return MaterialSkinManager.Instance; } }
  14.         public MouseState MouseState { get; set; }
  15.  
  16.         public override string Text { get { return baseTextBox.Text; } set { baseTextBox.Text = value; } }
  17.         public string Hint { get { return baseTextBox.Hint; } set { baseTextBox.Hint = value; } }
  18.  
  19.         private readonly AnimationManager animationManager;
  20.  
  21.         private readonly BaseTextBox baseTextBox;
  22.         public MaterialSingleLineTextField()
  23.         {
  24.             SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.DoubleBuffer, true);
  25.  
  26.             animationManager = new AnimationManager()
  27.             {
  28.                 Increment = 0.06,
  29.                 AnimationType = AnimationType.EaseInOut,
  30.                 InterruptAnimation = false
  31.             };
  32.             animationManager.OnAnimationProgress += sender => Invalidate();
  33.  
  34.             baseTextBox = new BaseTextBox
  35.             {
  36.                 BorderStyle = BorderStyle.None,
  37.                 Font = SkinManager.ROBOTO_REGULAR_11,
  38.                 ForeColor = SkinManager.GetMainTextColor(),
  39.                 Location = new Point(0, 0),
  40.                 Width = Width,
  41.                 Height = Height - 5,
  42.             };
  43.  
  44.             if (!Controls.Contains(baseTextBox) && !DesignMode)
  45.             {
  46.                 Controls.Add(baseTextBox);
  47.             }
  48.  
  49.             baseTextBox.GotFocus += (sender, args) => animationManager.StartNewAnimation(AnimationDirection.In);
  50.             baseTextBox.LostFocus += (sender, args) => animationManager.StartNewAnimation(AnimationDirection.Out);
  51.             BackColorChanged += (sender, args) =>
  52.             {
  53.                 baseTextBox.BackColor = BackColor;
  54.                 baseTextBox.ForeColor = SkinManager.GetMainTextColor();
  55.             };
  56.         }
  57.  
  58.         protected override void OnPaint(PaintEventArgs pevent)
  59.         {
  60.             var g = pevent.Graphics;
  61.             g.Clear(Parent.BackColor);
  62.  
  63.             int lineY = baseTextBox.Bottom + 3;
  64.  
  65.             if (!animationManager.IsAnimating())
  66.             {
  67.                 //No animation
  68.                 g.FillRectangle(baseTextBox.Focused ? SkinManager.PrimaryColorBrush : SkinManager.GetDividersBrush(), baseTextBox.Location.X, lineY, baseTextBox.Width, baseTextBox.Focused ? 2 : 1);
  69.             }
  70.             else
  71.             {
  72.                 //Animate
  73.                 int animationWidth = (int)(baseTextBox.Width * animationManager.GetProgress());
  74.                 int halfAnimationWidth = animationWidth / 2;
  75.                 int animationStart = baseTextBox.Location.X + baseTextBox.Width / 2;
  76.  
  77.                 //Unfocused background
  78.                 g.FillRectangle(SkinManager.GetDividersBrush(), baseTextBox.Location.X, lineY, baseTextBox.Width, 1);
  79.  
  80.                 //Animated focus transition
  81.                 g.FillRectangle(SkinManager.PrimaryColorBrush, animationStart - halfAnimationWidth, lineY, animationWidth, 2);
  82.             }
  83.         }
  84.  
  85.         protected override void OnResize(EventArgs e)
  86.         {
  87.             base.OnResize(e);
  88.  
  89.             baseTextBox.Location = new Point(0, 0);
  90.             baseTextBox.Width = Width;
  91.  
  92.             Height = baseTextBox.Height + 5;
  93.         }
  94.  
  95.         protected override void OnCreateControl()
  96.         {
  97.             base.OnCreateControl();
  98.  
  99.             baseTextBox.BackColor = Parent.BackColor;
  100.             baseTextBox.ForeColor = SkinManager.GetMainTextColor();
  101.         }
  102.  
  103.         private class BaseTextBox : TextBox, IMaterialControl
  104.         {
  105.             //Properties for managing the material design properties
  106.             public int Depth { get; set; }
  107.             public MaterialSkinManager SkinManager { get { return MaterialSkinManager.Instance; } }
  108.             public MouseState MouseState { get; set; }
  109.  
  110.             [DllImport("user32.dll", CharSet = CharSet.Auto)]
  111.             private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, string lParam);
  112.  
  113.             private const int EM_SETCUEBANNER = 0x1501;
  114.  
  115.             private string _hint = string.Empty;
  116.  
  117.             public string Hint
  118.             {
  119.                 get { return _hint; }
  120.                 set
  121.                 {
  122.                     _hint = value;
  123.                     SendMessage(this.Handle, EM_SETCUEBANNER, (int)IntPtr.Zero, this.Hint);
  124.                 }
  125.             }
  126.  
  127.             public BaseTextBox()
  128.             {
  129.                 SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer, true);
  130.                 DoubleBuffered = true;
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement