Implementing transparent Label program code in WinForm

Source: Internet
Author: User
  code is as follows copy code

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Drawing;
Using System.Data;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Drawing.Drawing2D;

Namespace Lyjt.Highway.Monitoring.bin
{
public partial class transparent Label:usercontrol
{
#region Local Variables

public enum Shapeborderstyles
{
Shapebsnone,
Shapebsfixedsingle,
};

Private Shapeborderstyles _borderstyle = Shapeborderstyles.shapebsnone;
Private System.Drawing.Color _backcolor = Color.Black;
Private System.Drawing.Color _bordercolor = Color.White;
private int _radius = 20;
private int _opacity = 125;
private string _text = "Doatransparentlabel";

Local Variables for text
public enum TextAlignment
{
Left,
Center,
Right
};

public enum Movetype
{
None,
RightToLeft,
Downtoup,
LeftToRight,
Uptodown
}

protected TextAlignment _textalign = Textalignment.center;
protected Movetype _moving = Movetype.none;
protected bool _isselected = FALSE;
Private System.Drawing.Color _dimmedcolor = Color.lightgray;

Work Variables
protected int pointx = 0;
protected int pointy = 0;
protected Rectangle irect = new Rectangle ();
protected Rectangle Txtrect = new Rectangle ();

#endregion

#region Constructor
Public Transparent label ()
{
InitializeComponent ();
Base. BackColor = color.transparent;
SetStyle (Controlstyles.allpaintinginwmpaint | Controlstyles.optimizeddoublebuffer | Controlstyles.resizeredraw | Controlstyles.supportstransparentbackcolor | Controlstyles.userpaint, True);
SetStyle (Controlstyles.opaque, false);
UpdateStyles ();
this.components = new System.ComponentModel.Container ();
This.timer1 = new System.Windows.Forms.Timer (this.components);
//
Timer1
//
this.timer1.Enabled = false;
This.timer1.Tick + = new System.EventHandler (This.timer1_tick);
This.timer1.Interval = 100;
}
#endregion

#region Properties

[DefaultValue (typeof (Color), "Black")]
Public New Color BackColor
Gets or sets the background color of the control.
{
get {return _backcolor;}
set {_backcolor = value; Invalidate (); }
}

[
Bindable (True),
Category ("appearance"),
DefaultValue (Shapeborderstyles.shapebsnone),
Description ("Style of border to is drawn around control")
]
Public Shapeborderstyles Shapeborderstyle
{
get {return _borderstyle;}
set {_borderstyle = value; Invalidate (); }
}

[DefaultValue (typeof (color), "white"), Category ("appearance"), Description ("The border color of the")]
Gets or sets the outer border color of the control.
Public Color BorderColor
{
get {return _bordercolor;}
set {_bordercolor = value; Invalidate (); }
}

[
Bindable (True),
Category ("appearance"),
DefaultValue (125),
Description ("The alpha value used to blend" background.) Valid values are 0 through 255. ")
]
public int Opacity
{
get {return _opacity;}
set {_opacity = value; Invalidate (); }
}

[
Bindable (True),
Category ("Layout"),
DefaultValue (20),
Description ("Radius of rounded borders")
]
public int Radius
{
get {return _radius;}
set {_radius = value; Invalidate (); }
}

[
Bindable (True),
Category ("appearance"),
DefaultValue ("Doatransparentlabel"),
Description ("Text in the Doatransparentlabel")
]
Public String Caption
{
get {return _text;}
set {_text = value; Invalidate (); }
}

public override font Font
{
get {return base. Font; }
set {base. Font = value; This. Invalidate (); }
}

public override Color ForeColor
{
get {return base. ForeColor; }
set {base. ForeColor = value; This. Invalidate (); }
}

[
Bindable (True),
Category ("appearance"),
Description ("dimmed Color"),
]
Public Color Dimmedcolor
{
get {return _dimmedcolor;}
set {_dimmedcolor = value; Invalidate (); }
}

[
Bindable (True),
Category ("Behavior"),
Description ("Text movement"),
DefaultValue (Movetype.none)
]
Public Movetype Moving
{
get {return _moving;}
set {_moving = value; Invalidate (); }
}

[
Bindable (True),
Category ("appearance"),
Description ("Text alignment (left, right, or Center), only with moving None"),
DefaultValue (Textalignment.center)
]
Public TextAlignment TextAlign
{
get {return _textalign;}
set {_textalign = value; Invalidate (); }
}

[
Bindable (True),
Category ("Behavior"),
Description ("Active the Text movement"),
DefaultValue (FALSE)
]
public bool Movingactive
{
get {return this.timer1.Enabled;}
Set
{
if (value = = false) _moving = Movetype.none;
this.timer1.Enabled = value;
This. Invalidate ();
}
}

#endregion

#region Methods

protected override void OnPaint (PaintEventArgs e)
{
SmoothingMode sm = E.graphics.smoothingmode;
E.graphics.smoothingmode = Smoothingmode.antialias;
if (_borderstyle = = Shapeborderstyles.shapebsfixedsingle)
Drawborder (E.graphics);
Drawlabelbackground (E.graphics);
DrawText (e);
E.graphics.smoothingmode = SM;
}

private void Drawborder (Graphics g)
{
Rectangle rect = this. ClientRectangle;
Rect. width--;
Rect. height--;
using (GraphicsPath BP = GetPath (rect, _radius))
{
using (pen p = new Pen (_bordercolor))
{
G.drawpath (P, BP);
}
}
}

private void Drawlabelbackground (Graphics g)
{
Rectangle rect = this. ClientRectangle;
Irect = rect;
Rect. x + +;
Rect. y++;
Rect. Width = 2;
Rect. Height = 2;
using (GraphicsPath BB = GetPath (rect, _radius))
{
using (Brush br = new SolidBrush (Color.FromArgb (_opacity, _backcolor))
{
G.fillpath (BR, BB);
}
}
}

Protected GraphicsPath GetPath (Rectangle rc, int r)
Build the path and the round corners in the rectangle
R is the radious of rounded corner.
{
int x = RC. X, y = rc. Y, W = RC. Width, h = RC. Height;
R = r << 1;
GraphicsPath path = new GraphicsPath ();
if (R > 0)
If the radious of rounded corner is greater than one side then
Do the side rounded
{
if (R > H) {r = h;}; Rounded
if (R > W) {r = w;}; Rounded
Path.                    AddArc (x, Y, R, R, 180, 90); Upper left corner
Path.            AddArc (x + w-r, y, R, R, 270, 90); Upper right Corner
Path.        AddArc (x + w-r, y + h-r, R, R, 0, 90); Lower right Corner
Path.            AddArc (x, y + h-r, R, R, 90, 90); Lower left corner
Path. Closefigure ();
}
Else
If the radious of rounded corner is zero then the path is a rectangle
{
Path. AddRectangle (RC);
}

return path;
}

protected void DrawText (PaintEventArgs pe)
{
This is a workaround to get measurestring to work properly
Pe. Graphics.textrenderinghint = Textrenderinghint.antialias;
SizeF sz = pe. Graphics.measurestring (_text, base. Font);
Switch (_moving)
{
Case Movetype.none:
Nomove ();
Break
Case Movetype.righttoleft:
Moverighttoleft ();
Break
Case Movetype.downtoup:
Movedowntoup ();
Break
Case Movetype.lefttoright:
Movelefttoright ();
Break
Case Movetype.uptodown:
Moveuptodown ();
Break
}
Rectangle bounds for the text
Txtrect = new Rectangle (This.pointx, This.pointy,
(int) Sz. Width + 1, (int) sz. Height);

If the mouse is passing over the text it is selected and would be dimmed
otherwise nothing.
Brush brtext = new SolidBrush (base. ForeColor);
Brush brtextdimmed = new SolidBrush (_dimmedcolor);
if (_isselected)
Pe. Graphics.DrawString (_text,
Base. Font,
Brtextdimmed,
Txtrect);
Else
Pe. Graphics.DrawString (_text,
Base. Font,
Brtext,
Txtrect);
}

protected void Nomove ()
{
Align text
Switch (_textalign)
{
Case Textalignment.left:
Pointx = (int) this.irect.x;
Break
Case Textalignment.center:
Pointx = (this.irect.width-this.txtrect.width)/2;
Break
Case Textalignment.right:
Pointx = (this.irect.width-this.txtrect.width);
Break
}
Pointy = (this.irect.height-this.txtrect.height)/2;
}

protected void Moverighttoleft ()
{
if (Pointx <-this.txtrect.width)
{Pointx = This.irect.x + this.iRect.Width;}
Else
{Pointx-= 2;}
Pointy = (this.irect.height-this.txtrect.height)/2;
}

protected void Movedowntoup ()
{
Pointx = (this.irect.width-this.txtrect.width)/2;
if (Pointy <-this.txtrect.height)
{pointy = (int) this.irect.y + this.iRect.Height;}
Else
{pointy-= 2;}
}

protected void Movelefttoright ()
{
if (Pointx > this.irect.x + this.iRect.Width)
{pointx = This.irect.x-this.txtrect.width;}
Else
{pointx = 2;}
Pointy = (this.irect.height-this.txtrect.height)/2;
}

protected void Moveuptodown ()
{
Pointx = (this.irect.width-this.txtrect.width)/2;
if (Pointy > This.irect.y + this.iRect.Height)
{pointy = (int) this.irect.y-this.irect.height;}
Else
{pointy = 2;}
}

protected override void Onmouseenter (EventArgs e)
{
Base. Onmouseenter (e);
_isselected = true;
This. Invalidate ();
}

protected override void OnMouseLeave (EventArgs e)
{
Base. OnMouseLeave (e);
_isselected = false;
This. Invalidate ();
}

private void Timer1_Tick (object sender, System.EventArgs e)
{
This. Invalidate ();
This. Update ();
}

#endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.