IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Forms Discussion :

NotifyIcon et gif


Sujet :

Windows Forms

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 527
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 527
    Par défaut NotifyIcon et gif
    Bonjour,

    Ne pouvant utiliser de gif animé dans l'icône standard de notification fournie avec VS 2010, je me suis rabattu sur l'utilisation conjointe de plusieurs icônes et d'un timer pour simuler l'animation que je désirais.

    A tout hasard, est-ce que vous connaitriez une NotifyIcon améliorée qui prendrait en charge directement l'utilisation d'une animation gif ?

    Papy !

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Citation Envoyé par Papy214 Voir le message
    A tout hasard, est-ce que vous connaitriez une NotifyIcon améliorée qui prendrait en charge directement l'utilisation d'une animation gif ?
    Non, mais c'est pas très difficile à faire :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    public class AnimatedNotifyIcon
    {
        private readonly NotifyIcon _notifyIcon = new NotifyIcon();
     
        private Bitmap _bitmap;
        public Bitmap Bitmap
        {
            get { return _bitmap; }
            set
            {
                if (value == _bitmap)
                    return;
                StopAnimation();
                _bitmap = value;
                InitIconCache();
                if (_animationEnabled)
                    StartAnimation();
            }
        }
     
        private bool _animationEnabled;
        public bool AnimationEnabled
        {
            get { return _animationEnabled; }
            set
            {
                if (value == _animationEnabled)
                    return;
                _animationEnabled = value;
                if (_animationEnabled)
                    StartAnimation();
                else
                    StopAnimation();
            }
        }
     
        public bool Visible
        {
            get { return _notifyIcon.Visible; } 
            set { _notifyIcon.Visible = value; }
        }
     
        private List<Icon> _icons;
        private int _currentIconIndex;
        private int _frameCount;
     
        private void InitIconCache()
        {
            if (_bitmap == null)
            {
                _icons = null;
                return;
            }
            _frameCount = _bitmap.GetFrameCount(FrameDimension.Time);
            _icons = new List<Icon>();
        }
     
        private void StartAnimation()
        {
            if (_icons != null)
            {
                _currentIconIndex = 0;
                ImageAnimator.Animate(_bitmap, AnimateCallback);
            }
            else
            {
                _notifyIcon.Icon = null;
            }
        }
     
        private void StopAnimation()
        {
            ImageAnimator.StopAnimate(_bitmap, AnimateCallback);
        }
     
        private void AnimateCallback(object sender, EventArgs e)
        {
            int index = _currentIconIndex % _frameCount;
            if (index >= _icons.Count)
            {
                ImageAnimator.UpdateFrames(_bitmap);
                IntPtr hIcon = _bitmap.GetHicon();
                Icon icon = Icon.FromHandle(hIcon);
                _icons.Add(icon);
            }
            _currentIconIndex.Dump();
            _notifyIcon.Icon = _icons[index];
            _currentIconIndex++;
        }
    }
    Note l'utilisation de ImageAnimator, plutôt qu'un timer : ça permet de prendre en compte la durée de chaque frame, si elle est différente.

    Pour l'utiliser, affecte ton GIF à la propriété Bitmap et mets AnimationEnabled à true

  3. #3
    Membre Expert

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 527
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 527
    Par défaut
    J'ai essayé d'intégrer ton code mais il doit me manquer une info.
    Comment je fais pour le lier à mon NotifyIcon ?
    NotifyIcon ne sait pas utiliser un bitmap.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Library.AnimatedNotifyIcon notico = new Library.AnimatedNotifyIcon();
    notico.Bitmap = Properties.Resources.paraboles;
    ModulerNotifyIcon.Icon = notico.Bitmap;
    notico.AnimationEnabled = true;
    VS râle (normal) à l'affectation du bitmap.
    Mais c'est peut-être pas comme ça qu'il faut l'utiliser ?

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Citation Envoyé par Papy214 Voir le message
    Comment je fais pour le lier à mon NotifyIcon ?
    Tu ne le lies pas à une NotifyIcon, tu l'utilises directement.

    Ou alors si tu préfères, tu peux modifier un peu pour l'associer à une NotifyIcon existante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class AnimatedNotifyIcon
    {
        private readonly NotifyIcon _notifyIcon;
     
        public AnimatedNotifyIcon()
            : (new NotifyIcon())
        {
        }
     
        public AnimatedNotifyIcon(NotifyIcon notifyIcon)
        {
            _notifyIcon = notifyIcon;
        }
     
        ...

    Et pour l'utiliser :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Library.AnimatedNotifyIcon notico = new Library.AnimatedNotifyIcon(ModulerNotifyIcon);
    notico.Bitmap = Properties.Resources.paraboles;
    notico.AnimationEnabled = true;

  5. #5
    Membre Expert

    Homme Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2007
    Messages
    3 527
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 63
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 3 527
    Par défaut
    Encore une fois, tu m'as dépanné. Merci !

    J'ai juste modifié un peu la classe pour en faire un singleton parce que ça m'arrange mieux dans mon code mais sinon, ça fonctionne très bien.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Afficher tout type d'image (gif ,png, jpg...)
    Par jfb53 dans le forum C++Builder
    Réponses: 22
    Dernier message: 20/07/2005, 14h52
  2. Conversion automatique de tiffs en jpg/gif
    Par Davenico dans le forum Composants VCL
    Réponses: 2
    Dernier message: 03/07/2003, 15h07
  3. [TP] gif et jpeg
    Par vinyl74 dans le forum Turbo Pascal
    Réponses: 11
    Dernier message: 22/12/2002, 15h02
  4. Rx Controls Gif animés
    Par toufou dans le forum Composants VCL
    Réponses: 6
    Dernier message: 23/08/2002, 14h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo