淡入淡出显示图像控件

16 篇文章 0 订阅
14 篇文章 0 订阅
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Drawing.Imaging;
  9. namespace ImageFlash
  10. {
  11.     public class AnimateControl : Control
  12.     {
  13.         private Image _image;
  14.         private System.Threading.Timer _timer;
  15.         private static readonly int Interval = 100;
  16.         private bool _stop = true;
  17.         private bool _flashIn = true;
  18.         private float _opaque;
  19.         private bool _flash;
  20.         private float _step = 0.02f;
  21.         public AnimateControl()
  22.         {
  23.             SetStyle(
  24.                 ControlStyles.AllPaintingInWmPaint |
  25.                 ControlStyles.UserPaint |
  26.                 ControlStyles.ResizeRedraw |
  27.                 ControlStyles.OptimizedDoubleBuffer, true);
  28.         }
  29.         [DefaultValue(null)]
  30.         public Image Image
  31.         {
  32.             get { return _image; }
  33.             set
  34.             {
  35.                 if (!_stop)
  36.                     throw new Exception("Flashing");
  37.                 _image = value;
  38.                 if (DesignMode)
  39.                     Invalidate();
  40.             }
  41.         }
  42.         [DefaultValue(true)]
  43.         public bool FlashIn
  44.         {
  45.             get { return _flashIn; }
  46.             set 
  47.             {
  48.                 if (!_stop)
  49.                     throw new Exception("Flashing");
  50.                 _flashIn = value; 
  51.             }
  52.         }
  53.         [DefaultValue(0.02f)]
  54.         public float Step
  55.         {
  56.             get { return _step; }
  57.             set
  58.             {
  59.                 if (!_stop)
  60.                     throw new Exception("Flashing");
  61.                 if (value < 0)
  62.                     _step = 0.01f;
  63.                 else if (value > 1)
  64.                     _step = 1f;
  65.                 else
  66.                     _step = value;
  67.             }
  68.         }
  69.         private System.Threading.Timer Timer
  70.         {
  71.             get
  72.             {
  73.                 if (_timer == null)
  74.                 {
  75.                     _timer = new System.Threading.Timer(
  76.                         new System.Threading.TimerCallback(Animate),
  77.                         null,
  78.                         -1,
  79.                         Interval);
  80.                 }
  81.                 return _timer;
  82.             }
  83.         }
  84.         protected override void OnPaint(PaintEventArgs pe)
  85.         {
  86.             base.OnPaint(pe);
  87.             if (!DesignMode && _flash)
  88.             {
  89.                 AnimateDraw(pe.Graphics, _opaque);
  90.             }
  91.             else if (DesignMode)
  92.             {
  93.                 if (_image != null)
  94.                     pe.Graphics.DrawImage(
  95.                         _image,
  96.                         0,
  97.                         0);
  98.             }
  99.         }
  100.         private void AnimateDraw(Graphics g, float op)
  101.         {
  102.             if (_image == null)
  103.                 return;
  104.             float[][] matrix = new float[][]{
  105.                 new float[]{op,0,0,0,0},
  106.                 new float[]{0,op,0,0,0},
  107.                 new float[]{0,0,op,0,0},
  108.                 new float[]{0,0,0,op,0},
  109.                 new float[]{0,0,0,0,1.0f}};
  110.             ImageAttributes attributes = new ImageAttributes();
  111.             ColorMatrix colorMatrix = new ColorMatrix(matrix);
  112.             int width = _image.Width;
  113.             int height = _image.Height;
  114.             attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  115.             g.DrawImage(
  116.                 _image,
  117.                 new Rectangle(0, 0, width, height),
  118.                 0,
  119.                 0,
  120.                 width,
  121.                 height,
  122.                 GraphicsUnit.Pixel,
  123.                 attributes);
  124.         }
  125.         private void Animate(object obj)
  126.         {
  127.             bool exit = false;
  128.             if (_flashIn)
  129.             {
  130.                 _opaque += _step;
  131.                 if (_opaque > 1)
  132.                 {
  133.                     _opaque = 1f;
  134.                     exit = true;
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 _opaque -= _step;
  140.                 if (_opaque < 0)
  141.                 {
  142.                     _opaque = 0f;
  143.                     exit = true;
  144.                 }
  145.             }
  146.             Invalidate();
  147.             if (exit)
  148.             {
  149.                 _timer.Change(-1, Interval);
  150.                 _stop = true;
  151.             }
  152.         }
  153.         public void StartFlash()
  154.         {
  155.             if (!_stop)
  156.                 return;
  157.             _flash = true;
  158.             _stop = false;
  159.             _opaque = _flashIn ? 0f : 1f;
  160.             Timer.Change(0, Interval);
  161.         }
  162.         public void StopFlash()
  163.         {
  164.             if (!_stop)
  165.             {
  166.                 Timer.Change(-1, Interval);
  167.                 _stop = true;
  168.                 _flash = false;
  169.             }
  170.         }
  171.         protected override Size DefaultSize
  172.         {
  173.             get
  174.             {
  175.                 return new Size(200,200);
  176.             }
  177.         }
  178.         protected override void Dispose(bool disposing)
  179.         {
  180.             base.Dispose(disposing);
  181.             if (disposing)
  182.             {
  183.                 if (_timer != null)
  184.                 {
  185.                     _timer.Dispose();
  186.                     _timer = null;
  187.                 }
  188.                 if (_image != null)
  189.                 {
  190.                     _image.Dispose();
  191.                     _image = null;
  192.                 }
  193.             }
  194.         }
  195.     }
  196. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值