- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Drawing.Imaging;
- namespace ImageFlash
- {
- public class AnimateControl : Control
- {
- private Image _image;
- private System.Threading.Timer _timer;
- private static readonly int Interval = 100;
- private bool _stop = true;
- private bool _flashIn = true;
- private float _opaque;
- private bool _flash;
- private float _step = 0.02f;
- public AnimateControl()
- {
- SetStyle(
- ControlStyles.AllPaintingInWmPaint |
- ControlStyles.UserPaint |
- ControlStyles.ResizeRedraw |
- ControlStyles.OptimizedDoubleBuffer, true);
- }
- [DefaultValue(null)]
- public Image Image
- {
- get { return _image; }
- set
- {
- if (!_stop)
- throw new Exception("Flashing");
- _image = value;
- if (DesignMode)
- Invalidate();
- }
- }
- [DefaultValue(true)]
- public bool FlashIn
- {
- get { return _flashIn; }
- set
- {
- if (!_stop)
- throw new Exception("Flashing");
- _flashIn = value;
- }
- }
- [DefaultValue(0.02f)]
- public float Step
- {
- get { return _step; }
- set
- {
- if (!_stop)
- throw new Exception("Flashing");
- if (value < 0)
- _step = 0.01f;
- else if (value > 1)
- _step = 1f;
- else
- _step = value;
- }
- }
- private System.Threading.Timer Timer
- {
- get
- {
- if (_timer == null)
- {
- _timer = new System.Threading.Timer(
- new System.Threading.TimerCallback(Animate),
- null,
- -1,
- Interval);
- }
- return _timer;
- }
- }
- protected override void OnPaint(PaintEventArgs pe)
- {
- base.OnPaint(pe);
- if (!DesignMode && _flash)
- {
- AnimateDraw(pe.Graphics, _opaque);
- }
- else if (DesignMode)
- {
- if (_image != null)
- pe.Graphics.DrawImage(
- _image,
- 0,
- 0);
- }
- }
- private void AnimateDraw(Graphics g, float op)
- {
- if (_image == null)
- return;
- float[][] matrix = new float[][]{
- new float[]{op,0,0,0,0},
- new float[]{0,op,0,0,0},
- new float[]{0,0,op,0,0},
- new float[]{0,0,0,op,0},
- new float[]{0,0,0,0,1.0f}};
- ImageAttributes attributes = new ImageAttributes();
- ColorMatrix colorMatrix = new ColorMatrix(matrix);
- int width = _image.Width;
- int height = _image.Height;
- attributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
- g.DrawImage(
- _image,
- new Rectangle(0, 0, width, height),
- 0,
- 0,
- width,
- height,
- GraphicsUnit.Pixel,
- attributes);
- }
- private void Animate(object obj)
- {
- bool exit = false;
- if (_flashIn)
- {
- _opaque += _step;
- if (_opaque > 1)
- {
- _opaque = 1f;
- exit = true;
- }
- }
- else
- {
- _opaque -= _step;
- if (_opaque < 0)
- {
- _opaque = 0f;
- exit = true;
- }
- }
- Invalidate();
- if (exit)
- {
- _timer.Change(-1, Interval);
- _stop = true;
- }
- }
- public void StartFlash()
- {
- if (!_stop)
- return;
- _flash = true;
- _stop = false;
- _opaque = _flashIn ? 0f : 1f;
- Timer.Change(0, Interval);
- }
- public void StopFlash()
- {
- if (!_stop)
- {
- Timer.Change(-1, Interval);
- _stop = true;
- _flash = false;
- }
- }
- protected override Size DefaultSize
- {
- get
- {
- return new Size(200,200);
- }
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (disposing)
- {
- if (_timer != null)
- {
- _timer.Dispose();
- _timer = null;
- }
- if (_image != null)
- {
- _image.Dispose();
- _image = null;
- }
- }
- }
- }
- }
淡入淡出显示图像控件
最新推荐文章于 2023-02-14 18:42:55 发布