Guest User

InfoBar

a guest
Jul 6th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5.  
  6. namespace Chrome1
  7. {
  8.     internal class InfoBar : Control
  9.     {
  10.         public InfoBar()
  11.         {
  12.             Dock = DockStyle.Top;
  13.             AutoSize = true;
  14.             DoubleBuffered = true;
  15.  
  16.  
  17.             SetStyle(ControlStyles.AllPaintingInWmPaint, true);// True is better
  18.             SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // True is better
  19.             // Disable the on built PAINT event. We dont need it with a renderloop.
  20.             // The form will no longer refresh itself
  21.             // we will raise the paint event ourselves from our renderloop.
  22.             SetStyle(ControlStyles.UserPaint, true); // False is better
  23.         }
  24.  
  25.         Image _image;
  26.         [Description("Image shown on the left side of the control"),
  27.         Category("Appearance")]
  28.         public Image Image
  29.         {
  30.             get
  31.             {
  32.                 return _image;
  33.             }
  34.             set
  35.             {
  36.                 if (_image == value) return;
  37.                 _image = value;
  38.                 Invalidate();
  39.             }
  40.         }
  41.  
  42.         #region overrides
  43.  
  44.         [DefaultValue(DockStyle.Top), Description("Defines which borders of control are cound to container"), Category("Appearance"), Browsable(true)]
  45.         public override DockStyle Dock
  46.         {
  47.             get { return base.Dock; }
  48.             set
  49.             {
  50.                 if (base.Dock == value) return;
  51.                 base.Dock = value;
  52.                 Invalidate();
  53.             }
  54.         }
  55.  
  56.         [DefaultValue("Importane message"), Description("Text of control"), Category("Appearance"), Browsable(true)]
  57.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  58.         public override string Text
  59.         {
  60.             get
  61.             {
  62.                 return base.Text;
  63.             }
  64.             set
  65.             {
  66.                 if (base.Text == value) return;
  67.                 base.Text = value;
  68.                 Invalidate();
  69.             }
  70.         }
  71.  
  72.         protected override void OnTextChanged(EventArgs e)
  73.         {
  74.             base.OnTextChanged(e);
  75.             SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size);
  76.             Invalidate();
  77.         }
  78.  
  79.         protected override void OnResize(EventArgs e)
  80.         {
  81.             base.OnResize(e);
  82.             Invalidate();
  83.         }
  84.  
  85.         protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
  86.         {
  87.             var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter;
  88.             var proposedSize = new Size(width - _paddingLeft - _paddingRight, int.MaxValue);
  89.             var size = TextRenderer.MeasureText(Text, Font, proposedSize, flags);
  90.             height = size.Height + _paddingTopBottom * 2;
  91.             base.SetBoundsCore(x, y, width, height, specified);
  92.         }
  93.  
  94.         private int _paddingTopBottom = 10;
  95.         private int _paddingLeft = 20;
  96.         private int _paddingRight = 10;
  97.  
  98.         protected override void OnPaint(PaintEventArgs e)
  99.         {
  100.             base.OnPaint(e);
  101.             e.Graphics.Clear(SystemColors.Info);
  102.  
  103.             Rectangle textRect = new Rectangle
  104.             {
  105.                 Width = Width - _paddingRight - _paddingLeft / 2,
  106.                 Height = Height - _paddingTopBottom * 2,
  107.                 X = Padding.Left + _paddingLeft / 2,
  108.                 Y = Padding.Top + _paddingTopBottom
  109.             };
  110.  
  111.             if (_image != null)
  112.             {
  113.                 e.Graphics.DrawImage(_image, new Point(0, 10));
  114.  
  115.                 textRect.Width -= _paddingLeft/2;
  116.                 textRect.X += _paddingLeft/2;
  117.             }
  118.  
  119.  
  120.  
  121.             var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak | TextFormatFlags.Top;
  122.             TextRenderer.DrawText(e.Graphics, Text, Font, textRect, ForeColor, flags);
  123.  
  124.             if (Dock == DockStyle.Top)
  125.             {
  126.                 e.Graphics.DrawLine(Pens.Black, 0, Height - 1, Width, Height - 1);
  127.             }
  128.  
  129.             //for test only
  130.             e.Graphics.DrawRectangle(Pens.Red, textRect);
  131.         }
  132.  
  133.         #endregion
  134.     }
  135. }
Add Comment
Please, Sign In to add comment