Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- namespace Chrome1
- {
- internal class InfoBar : Control
- {
- public InfoBar()
- {
- Dock = DockStyle.Top;
- AutoSize = true;
- DoubleBuffered = true;
- SetStyle(ControlStyles.AllPaintingInWmPaint, true);// True is better
- SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // True is better
- // Disable the on built PAINT event. We dont need it with a renderloop.
- // The form will no longer refresh itself
- // we will raise the paint event ourselves from our renderloop.
- SetStyle(ControlStyles.UserPaint, true); // False is better
- }
- Image _image;
- [Description("Image shown on the left side of the control"),
- Category("Appearance")]
- public Image Image
- {
- get
- {
- return _image;
- }
- set
- {
- if (_image == value) return;
- _image = value;
- Invalidate();
- }
- }
- #region overrides
- [DefaultValue(DockStyle.Top), Description("Defines which borders of control are cound to container"), Category("Appearance"), Browsable(true)]
- public override DockStyle Dock
- {
- get { return base.Dock; }
- set
- {
- if (base.Dock == value) return;
- base.Dock = value;
- Invalidate();
- }
- }
- [DefaultValue("Importane message"), Description("Text of control"), Category("Appearance"), Browsable(true)]
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
- public override string Text
- {
- get
- {
- return base.Text;
- }
- set
- {
- if (base.Text == value) return;
- base.Text = value;
- Invalidate();
- }
- }
- protected override void OnTextChanged(EventArgs e)
- {
- base.OnTextChanged(e);
- SetBoundsCore(Left, Top, Width, Height, BoundsSpecified.Size);
- Invalidate();
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- Invalidate();
- }
- protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
- {
- var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak | TextFormatFlags.VerticalCenter;
- var proposedSize = new Size(width - _paddingLeft - _paddingRight, int.MaxValue);
- var size = TextRenderer.MeasureText(Text, Font, proposedSize, flags);
- height = size.Height + _paddingTopBottom * 2;
- base.SetBoundsCore(x, y, width, height, specified);
- }
- private int _paddingTopBottom = 10;
- private int _paddingLeft = 20;
- private int _paddingRight = 10;
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- e.Graphics.Clear(SystemColors.Info);
- Rectangle textRect = new Rectangle
- {
- Width = Width - _paddingRight - _paddingLeft / 2,
- Height = Height - _paddingTopBottom * 2,
- X = Padding.Left + _paddingLeft / 2,
- Y = Padding.Top + _paddingTopBottom
- };
- if (_image != null)
- {
- e.Graphics.DrawImage(_image, new Point(0, 10));
- textRect.Width -= _paddingLeft/2;
- textRect.X += _paddingLeft/2;
- }
- var flags = TextFormatFlags.Left | TextFormatFlags.WordBreak | TextFormatFlags.Top;
- TextRenderer.DrawText(e.Graphics, Text, Font, textRect, ForeColor, flags);
- if (Dock == DockStyle.Top)
- {
- e.Graphics.DrawLine(Pens.Black, 0, Height - 1, Width, Height - 1);
- }
- //for test only
- e.Graphics.DrawRectangle(Pens.Red, textRect);
- }
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment