using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace HuizhongLibrary.MyAlphaControl
{
public partial class AlphaLabel : UserControl
{
private Bitmap m_BackgroundImage;
///
/// 背景图片
///
public new Bitmap BackgroundImage
{
get { return m_BackgroundImage; }
set
{
m_BackgroundImage = value;
}
}
private StringPool m_ScrollText = new StringPool();
///
/// 滚动文字列表
///
public StringPool ScrollText
{
get { return m_ScrollText; }
set { m_ScrollText = value; }
}
private int m_ScrollTop = 0;
public int ScrollTop
{
get { return m_ScrollTop; }
set { m_ScrollTop = value; }
}
private int m_ScrollLeft = 0;
public int ScrollLeft
{
get { return m_ScrollLeft; }
set { m_ScrollLeft = value; }
}
private string m_MoveDirection = "None";
///
/// 移动方向(None,Left,Right,Top,Boottom)
///
public string MoveDirection
{
get { return m_MoveDirection; }
set { m_MoveDirection = value; }
}
private int m_DurationStep = 5;
///
/// 每次移动多少个像素(默认一个像素,最慢的移动速度)
///
public int DurationStep
{
get { return m_DurationStep; }
set { m_DurationStep = value; }
}
private StringFormat m_TextAlign = new StringFormat();
///
/// 文本对齐
///
public StringFormat TextAlign
{
get { return m_TextAlign; }
set { m_TextAlign = value; }
}
private int m_Interval = 1;
public int Interval
{
get { return m_Interval; }
set { m_Interval = value; }
}
private int m_TimeOut = 30;
public int TimeOut
{
get { return m_TimeOut; }
set { m_TimeOut = value; }
}
private bool m_TickEnable = false;
///
/// 是否启用倒计时
///
public bool TickEnable
{
get { return m_TickEnable; }
set
{
m_TickEnable = value;
if (value == true) { StartTimeer(); }
else { StopTimeer(); }
}
}
private Color m_BorderColor = Color.Black;
///
/// 边框粗细
///
public Color BorderColor
{
get { return m_BorderColor; }
set { m_BorderColor = value; }
}
private bool m_Visible = true;
public new bool Visible
{
get { return m_Visible; }
set
{
m_Visible = value;
base.Visible = value;
}
}
private string m_NullText = "";
///
/// 空时显示的文本
///
public string NullText
{
get { return m_NullText; }
set { m_NullText = value; }
}
public bool ShowDateTime = false; //是否显示时间
public string DateTimeFormat = "yyyy年M月d日HH:mm:ss";
public int MaxRowNumber = 1;
///
/// 当倒计时正常终止时触发
///
public event Action TimeTick;
private int ScrollTextWidth = 0;
private int SRCCOPY = 13369376;
private Graphics G1;
private Graphics G2;
private IntPtr hdc1;
private IntPtr hdc2;
public AlphaLabel()
{
TextAlign.LineAlignment = StringAlignment.Near;
TextAlign.Alignment = StringAlignment.Near;
InitializeComponent();
}
#region 画背景图片(用于假透明显示)
public void SetBackground(Bitmap bmp)
{
if (bmp == null) return;
BackgroundImage = new Bitmap(this.Bounds.Width, this.Bounds.Height);
Graphics G2 = Graphics.FromImage(BackgroundImage);
G2.DrawImage(bmp, new Rectangle(0, 0, this.Bounds.Width, this.Bounds.Height), new Rectangle(this.Left, this.Top, this.Bounds.Width, this.Bounds.Height), GraphicsUnit.Pixel);
G2.Dispose();
//this.BorderStyle = BorderStyle.None;
//this.BackColor=Color.Transparent;
}
#endregion
#region 重画文字
//private byte[] CacheImage = null;
public void DrawString(int x, int y, string s)
{
Bitmap bmp = null;
if (BackgroundImage == null)
{
bmp = new Bitmap(this.Bounds.Width, this.Bounds.Height);
}
else
{
//复制图片
//CacheImage = CustomIO.GetPhoto(this.BackgroundImage);
bmp = (Bitmap)BackgroundImage.Clone();
}
G1 = this.CreateGraphics();
G2 = Graphics.FromImage(bmp);
hdc1 = G1.GetHdc();
hdc2 = G2.GetHdc();
if (BackgroundImage == null) G2.Clear(this.BackColor);
Brush backBrush = new SolidBrush(this.ForeColor);
G2.DrawString(s, this.Font, backBrush, x, y);
backBrush.Dispose();
SystemInfo.BitBlt(hdc1, 0, 0, Width, Height, hdc2, 0, 0, SRCCOPY);
G1.ReleaseHdc(hdc1);
G2.ReleaseHdc(hdc2);
G1.Dispose();
G2.Dispose();
bmp.Dispose();
//if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
//pictureBox1.Image = bmp;
//GC.Collect();
}
#endregion
#region 移动文字
public void MoveText()
{
if (this.MoveDirection == "Left")
{
if (this.ScrollLeft <= -this.ScrollTextWidth) { this.ScrollLeft = this.Width; AddScrollTextIndex(); }
this.ScrollLeft -= this.DurationStep;
}
if (this.MoveDirection == "Right")
{
if (this.ScrollLeft >= this.Width) { this.ScrollLeft = -this.ScrollTextWidth; AddScrollTextIndex(); }
this.ScrollLeft += this.DurationStep;
}
if (this.MoveDirection == "Top")
{
if (this.ScrollTop <= 0) { this.ScrollTop = this.Height; AddScrollTextIndex(); }
this.ScrollTop -= this.DurationStep;
}
if (this.MoveDirection == "Bottom")
{
if (this.ScrollTop >= this.Height) { this.ScrollTop = 0; AddScrollTextIndex(); }
this.ScrollTop += this.DurationStep;
}
this.DrawString(this.ScrollLeft, this.ScrollTop, this.Text);
}
#endregion
#region 增加索引
void AddScrollTextIndex()
{
if (string.IsNullOrEmpty(this.Text) == false) ScrollText.CheckIn(this.Text);
if (ScrollText.Available == 0) this.Text = this.NullText;
this.Text = ScrollText.CheckOut();
InitScrollTextSize();
//ScrollTextIndex++;
//if (ScrollTextIndex >= this.ScrollText.Count)
//{
// ScrollTextIndex = 0;
//}
}
#endregion
#region 计算文字大小
private SizeF MeasureString(string Text, Font font)
{
Bitmap bmp = new Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Graphics g = Graphics.FromImage(bmp);
SizeF s = g.MeasureString(Text, font);
return s;
}
#endregion
#region 开始计时
Timer time1 = new Timer();
private int TimeNumber=0;
public void StartTimeer()
{
TimeNumber = this.TimeOut;
time1.Interval = this.Interval * 1000;
time1.Tick += time1_Tick;
time1.Enabled = true;
}
void time1_Tick(object sender, EventArgs e)
{
if (ShowDateTime == true)
{
this.Text = DateTime.Now.ToString(DateTimeFormat);
}
else
{
if (this.TimeNumber == 0)
{
StopTimeer();
TimeNumber = this.TimeOut;
if (this.TimeTick != null) this.TimeTick(this);
return;
}
this.Text = TimeNumber.ToString();
TimeNumber--;
}
this.Refresh();
}
#endregion
#region 停止计时
public void StopTimeer()
{
time1.Tick -= time1_Tick;
time1.Enabled = false;
}
#endregion
#region 刷新
public new void Refresh()
{
Bitmap bmp = null;
if (BackgroundImage == null)
{
bmp = new Bitmap(this.Bounds.Width, this.Bounds.Height);
}
else
{
bmp = (Bitmap)BackgroundImage.Clone();
}
//G1 = this.CreateGraphics();
G2 = Graphics.FromImage(bmp);
//hdc1 = G1.GetHdc();
//hdc2 = G2.GetHdc();
if (BackgroundImage == null) G2.Clear(this.BackColor);
Brush backBrush = new SolidBrush(this.ForeColor);
//if (this.Border > 0) g.DrawRectangle(new Pen(BorderColor, this.Border), this.Bounds);
//if (string.IsNullOrEmpty(this.Text) == false) g.DrawString(this.Text, this.Font, backBrush, new RectangleF(0, 0, this.Bounds.Width, this.Bounds.Height), TextAlign);
if (string.IsNullOrEmpty(this.Text) == false)
{
G2.DrawString(this.Text, this.Font, backBrush, new RectangleF(0, 0, this.Width, this.Height), this.TextAlign);
//if (TextAlign.Alignment == StringAlignment.Near)
//{
// G2.DrawString(this.Text, this.Font, backBrush, 0, 0);
//}
//if (TextAlign.Alignment == StringAlignment.Center)
//{
// float TextWidth = MeasureString(this.Text, this.Font).Width;
// float x = (this.Width - TextWidth) / 2;
// G2.DrawString(this.Text, this.Font, backBrush, x, 0);
//}
//if (TextAlign.Alignment == StringAlignment.Far)
//{
// float TextWidth = MeasureString(this.Text + "AA", this.Font).Width;
// float x = (this.Width - TextWidth);
// G2.DrawString(this.Text, this.Font, backBrush, x, 0);
//}
}
backBrush.Dispose();
//SystemInfo.BitBlt(hdc1, 0, 0, Width, Height, hdc2, 0, 0, SRCCOPY);
//G1.ReleaseHdc(hdc1);
//G2.ReleaseHdc(hdc2);
//G1.Dispose();
//G2.Dispose();
//bmp.Dispose();
if (pictureBox1.Image != null) pictureBox1.Image.Dispose();
pictureBox1.Image = bmp;
}
#endregion
#region 新增文本行
public void AddRowText(string Text)
{
if (this.ScrollText.Available == this.MaxRowNumber)
{
this.ScrollText.CheckOut();
}
this.ScrollText.CheckIn(Text);
string ss = "";
for (int i = 0; i < this.ScrollText.Available; i++)
{
ss+=this.ScrollText.Peek()+ "\n";
}
ss = ss.Substring(0, ss.Length - 1);
this.Text = ss;
}
#endregion
#region 清除文本
public void ClearRowText()
{
this.ScrollText.Clear();
this.Text = "";
}
#endregion
#region 移除滚动文字
public void Remove(string item)
{
this.ScrollText.Remove(item);
if (this.Text == item) this.Text = "";
if (ScrollText.Available == 0)
{
this.Text = this.NullText;
}
else
{
this.Text = this.ScrollText.CheckOut();
}
if (this.MoveDirection == "Left")
{
this.ScrollLeft = this.Width;
}
if (this.MoveDirection == "Right")
{
this.ScrollLeft = 0;
}
if (this.MoveDirection == "Top")
{
this.ScrollTop = this.Height;
}
if (this.MoveDirection == "Bottom")
{
this.ScrollTop = 0;
}
InitScrollTextSize();
}
#endregion
#region 初始化文字大小
public void InitScrollTextSize()
{
this.ScrollTextWidth =Convert.ToInt32(MeasureString(this.Text, this.Font).Width);
}
#endregion
#region 隐藏背景控件
public void VisiblePictureBox()
{
pictureBox1.Visible = false;
}
#endregion
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
}
}