WinForm中实现透明Label程序代码

 代码如下 复制代码

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 透明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 透明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 be drawn around control")
        ]
        public ShapeBorderStyles ShapeBorderStyle
        {
            get { return _borderStyle; }
            set { _borderStyle = value; this.Invalidate(); }
        }
 
        [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("The border color of the control.")]
        /// 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 the control's background. Valid values are 0 through 255.")
        ]
        public int Opacity
        {
            get { return _opacity; }
            set { _opacity = value; this.Invalidate(); }
        }
 
        [
        Bindable(true),
        Category("Layout"),
        DefaultValue(20),
        Description("Radius of rounded borders")
        ]
        public int Radius
        {
            get { return _radius; }
            set { _radius = value; this.Invalidate(); }
        }
 
        [
        Bindable(true),
        Category("Appearance"),
        DefaultValue("DOATransparentLabel"),
        Description("Text in the DOATransparentLabel")
        ]
        public String Caption
        {
            get { return _text; }
            set { _text = value; this.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; this.Invalidate(); }
        }
 
        [
        Bindable(true),
        Category("Behavior"),
        Description("Text movement"),
        DefaultValue(MoveType.None)
        ]
        public MoveType Moving
        {
            get { return _moving; }
            set { _moving = value; this.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; this.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 with 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 will 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
    }
}
 

时间: 2024-09-30 03:52:59

WinForm中实现透明Label程序代码的相关文章

Python中删除文件的程序代码_python

Python是一种面向对象的解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定.Python 具有脚本语言中最丰富和强大的类库,足以支持绝大多数日常应用.它具有简单.易学.免费.开源.可移植性.解释性.面向对象.可扩展性.可嵌入性以及丰富的库等特性,目前的应用范围也非常广泛,如系统编程.图像处理.数据库编程等方面. Python开发者可以使用文本编辑器(如Windows的记事本等)或者专业的IDE(集成开发环境)来编写程序代码.IDE使得开发者可

php中两日期比较程序代码

 代码如下 复制代码 <?php /*   * 作者:不知道作者是谁   * 日期:2008-4-13   * 功能:计算两个日期的时间差   * 备注:基本上没有修改程序代码,仅将书写规范了下,以及做了注释  // 曼查罗   */    // 定义要比较的两个日期 $Date_1="2008-7-15"; $Date_2="2006-7-14";   // 将日期化成数组,以便制作时间戳 $Date_List_1=explode("-"

asp.net中禁止页面缓存程序代码

方法一:导航时用服务器端的Response.Redirect方法,或者用前端得window.location.replace方法. 方法二:禁用ASP.NET页面缓存. 在必要的时候我还是推荐方法二的.因为我们做开发的不能控制浏览器的设置.用户如果把IE设置为从不检查更新缓存的问题则不好避免,所以还是用方法二,让IE的临时文件夹不存在页面文件. 另外,是否禁用页面缓存要根据实际情况来说,起初我认为页面缓存只是缓存在服务器,后来看了一篇blog 发现缓存的意义还是很大的.在很多情况下可以大大减小服

在火狐浏览器中使用透明效果的代码

火狐浏览器|透明 火狐浏览器中如何使用透明效果呢? 设置为半通明的代码如下: filter: Alpha(Opacity=0);-moz-opacity:0.5;opacity: 0;

Winform 中嵌入外部exe程序

问题描述 我在WInform界面嵌入了一个其他exe程序这个Exe程序上面有ToolStrip控件我点击它里面的ToolStripButton按钮为什么没反应,根本就不执行ToolStripButton_Click事件里的代码,好像都没进去,请高手指点下! 解决方案 解决方案二:无语了.你的ToolStripButton_Click怎么注册给人家进程了?解决方案三:赚点分好难,楼主的问题是玄学问题解决方案四:你是怎么嵌入的?解决方案五:而且,你要测试按钮好使不好使,也要在那个exe程序里弹出个对

asp.net中无损图片添加水印程序代码

水印是为了防止别盗用我们的图片. 两种方式实现水印效果 1)可以在用户上传时添加水印. a) 好处:与2种方法相比,用户每次读取此图片时,服务器直接发送给客户就行了. b) 缺点:破坏了原始图片. 2)通过全局的一般处理程序,当用户请求这张图片时,加水印. a) 好处:原始图片没有被破坏 b) 缺点:用户每次请求时都需要对请求的图片进行加水印处理,浪费的服务器的资源. 代码实现第二种方式: 代码如下:  代码如下 复制代码   using System; using System.Collect

WordPress中批量替换正文程序代码

在 WordPress 主题的 functions.php 中插入以下代码:  代码如下 复制代码 function content_str_replace($content = ''){ $content = str_replace('old', 'new', $content); return $content; } add_filter('the_content', 'content_str_replace', 10); 和之前一样,其中 old 是旧的字符串,new 是你要替换的文字.

php中数组的搜索程序代码

例  代码如下 复制代码 <?php $people = array("Peter", "Joe", "Glenn", "Cleveland"); if (in_array("Glenn",$people))   {   echo "Match found";   } else   {   echo "Match not found";   } ?> 输出

php中向数组中插入一元素程序代码

 代码如下 复制代码 <?php /** * 逆序二维数组插入一元素 * * @author WadeYu * @date 2012-05-30 */ $aSorted = array( array(1, 100), array(2, 90), array(3, 80), array(4, 70), array(5, 60), array(6, 50), array(7, 40), array(8, 40), array(9, 40), array(10, 20), ); $aInsert =