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 Presentation Foundation Discussion :

[Wrapper NotifyIcon] Comment implémenter au mieux BalloonToolTip ?


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Expert confirmé
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 65
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Par défaut [Wrapper NotifyIcon] Comment implémenter au mieux BalloonToolTip ?
    Bonjour à tous

    Comme WPF ne possède pas de contrôle NotifyIcon, j'essaye d'implémenter un wrapper du System.Windows.Forms.NotifyIcon pour WPF.

    Pour l'instant, tout se passe côté code-behind. Le XAML se résume à :
    1. Le CustomControl NDINotifyIcon
      Code : Sélectionner tout - Visualiser dans une fenêtre à part
      1
      2
      3
      4
      5
      <!-- Template et Style par défaut du NotifyIcon NDI -->
      <Style TargetType="{x:Type local:NDINotifyIcon}">
         <Setter Property="Icon" Value="NDIIcon.Ico" />
      </Style>
      

    2. L'ajout dans ma fenêtre
      Code : Sélectionner tout - Visualiser dans une fenêtre à part
      1
      2
      3
      4
      5
      <local:NDINotifyIcon x:Name="NotifyIcon"
         Tag="Cliquez pour faire apparaître/disparaître la fenêtre."
         Text="{Binding Path=Title,RelativeSource={RelativeSource
      AncestorType={x:Type Window}, Mode=FindAncestor}}"/>
      
    A l'exécution, tout semble se passer correctement. Le NotifyIcon apparaît, et lorsque je clique dessus, la fenêtre apparaît ou disparaît comme souhaité.
    Voici le code du CustomControl NDINotifyIcon :

    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
    public class NDINotifyIcon : FrameworkElement, IAddChild
    {
       //...
       public event MouseButtonEventHandler MouseClick
       {
        add{ AddHandler(Commands.MouseClickEvent, value); }
        remove{ RemoveHandler(Commands.MouseClickEvent, value); }
       }
       //...
       protected virtual void OnMouseClick(Object sender, System.Windows.Forms.MouseEventArgs e)
       {
         OnRaiseEvent(Commands.MouseClickEvent, new MouseButtonEventArgs
    (InputManager.Current.PrimaryMouseDevice, 0, ToMouseButton(e.Button)));
       }
       //...
       protected virtual void OnRaiseEvent(RoutedEvent handler, MouseButtonEventArgs e)
      {
         e.RoutedEvent = handler;
         RaiseEvent(e);
      }
     
    //...
     
    private MouseButton ToMouseButton(System.Windows.Forms.MouseButtons button)
    {
       switch (button)
       {
         case System.Windows.Forms.MouseButtons.Left: return MouseButton.Left;
         case System.Windows.Forms.MouseButtons.Right: return MouseButton.Right;
         case System.Windows.Forms.MouseButtons.Middle: return MouseButton.Middle;
         case System.Windows.Forms.MouseButtons.XButton1: return MouseButton.XButton1;
         case System.Windows.Forms.MouseButtons.XButton2: return MouseButton.XButton2;
       }
       throw new InvalidOperationException();
    }
     
    }
    Et le code de la fenêtre :
    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
    public override void OnApplyTemplate()
    {
     this.NotifyIcon = (NDINotifyIcon)(this.Template.FindName("NotifyIcon", this));
     //...
     this.NotifyIcon.AddHandler
    (Commands.MouseClickEvent,new MouseButtonEventHandler(OnNotifyIconClick));
    }
     
    //...
     
    protected virtual void OnNotifyIconClick(Object sender, MouseButtonEventArgs e)
    {
    switch (e.ChangedButton)
    {
      case MouseButton.Left:
         if (this.WindowState == WindowState.Minimized) 
            { this.WindowState = WindowState.Normal; }
         else if (this.WindowState == WindowState.Normal)
            { this.WindowState = WindowState.Minimized; }
         break;
      case MouseButton.Right:
         //faire apparaitre menu contextuel
         break;
     }
     e.Handled = true;
    }
    Maintenant je veux obtenir un texte info-bulle qui s'affiche lorsque le pointeur de la souris se place sur le NDINotifyIcon. J'ai donc définit une DependancyProperty Text:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(String), typeof(NDINotifyIcon),
    new UIPropertyMetadata(String.Empty));
     
    public String Text
    {
       get { return (String)GetValue(TextProperty); }
       set { SetValue(TextProperty, value); }
    }
    Que faut-il que je fasse de plus pour que j'obtienne l'info-bulle à l'exécution? Je vous demande de l'aide, car j'ai un peu l'esprit embrouillé.

  2. #2
    Expert confirmé
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 65
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Par défaut
    J'ai oublié de vous montrer le code d'initialisation du NDINotifyIcon :
    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
    protected override void OnInitialized(EventArgs e)
    {
     base.OnInitialized(e);
     this.InitializeNotifyIcon();
     Dispatcher.ShutdownStarted += OnShutdown;
    }
     
    protected virtual void InitializeNotifyIcon()
    {
     this._notifyIcon = new NotifyIcon();
     this._notifyIcon.Text = this.Text;
     this._notifyIcon.Icon = FromImageSource(this.Icon);
     this._notifyIcon.Visible = FromVisibility(this.Visibility);
     this._notifyIcon.MouseClick += OnMouseClick;
     
    }

  3. #3
    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 neguib Voir le message
    J'ai oublié de vous montrer le code d'initialisation du NDINotifyIcon :
    ah oui, c'est plus clair comme ça...

    Pourquoi tu n'utilises pas les propriétés BalloonTipIcon, BalloonTipText et BalloonTipTitle du NotifyIcon Windows Forms ?

  4. #4
    Expert confirmé
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 65
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Par défaut
    Bonjour et merci de ta réponse tomlev

    J'ai suivi ton conseil et j'ai donc 3 nouvelles DependancyProperties :
    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
    public static readonly DependencyProperty BalloonTipIconProperty =
    DependencyProperty.Register("BalloonTipIcon", typeof(NDIBalloonTipIcon),
    typeof(NDINotifyIcon), new UIPropertyMetadata(NDIBalloonTipIcon.Info));
    public static readonly DependencyProperty BalloonTipTextProperty =
    DependencyProperty.Register("BalloonTipText", typeof(String),
    typeof(NDINotifyIcon), new UIPropertyMetadata(String.Empty));
    public static readonly DependencyProperty BalloonTipTitleProperty =
    DependencyProperty.Register("BalloonTipTitle", typeof(String),
    typeof(NDINotifyIcon), new UIPropertyMetadata(String.Empty));
     
     
    public NDIBalloonTipIcon BalloonTipIcon
    {
    get { return (NDIBalloonTipIcon)GetValue(BalloonTipIconProperty); }
    set { SetValue(BalloonTipIconProperty, value); }
    }
    public String BalloonTipText
    {
    get { return (String)GetValue(BalloonTipTextProperty); }
    set { SetValue(BalloonTipTextProperty, value); }
    }
    public String BalloonTipTitle
    {
    get { return (String)GetValue(BalloonTipTitleProperty); }
    set { SetValue(BalloonTipTitleProperty, value); }
    }
     
     
    protected virtual void InitializeNotifyIcon()
    {
      this._notifyIcon = new NotifyIcon();
      this._notifyIcon.BalloonTipText = this.BalloonTipText;
      this._notifyIcon.Text = this.Text;
      this._notifyIcon.Icon = FromImageSource(this.Icon);
      this._notifyIcon.Visible = FromVisibility(this.Visibility);
      this._notifyIcon.MouseClick += OnMouseClick;
     
    }
    
    ainsi qu'une Enumeration NDIBalloonTipIcon.

    Mais bien évidemment cela ne donne rien à l'exécution.
    Que faut-il faire de plus ?

  5. #5
    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
    ben là tu n'as fait aucun lien avec les propriétés du NotifyIcon WinForms... il faudrait que dans le set de tes DependencyProperty tu mettes à jour les propriétés du composant WinForms :
    Code C# : 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
    public NDIBalloonTipIcon BalloonTipIcon
    {
    get { return (NDIBalloonTipIcon)GetValue(BalloonTipIconProperty); }
    set {
        _notifyIcon.BalloonTipIcon = value;
        SetValue(BalloonTipIconProperty, value);
        }
    }
    public String BalloonTipText
    {
    get { return (String)GetValue(BalloonTipTextProperty); }
    set {
        _notifyIcon.BalloonTipText = value;
        SetValue(BalloonTipTextProperty, value);
    }
    }
    public String BalloonTipTitle
    {
    get { return (String)GetValue(BalloonTipTitleProperty); }
    set {
        _notifyIcon.BalloonTipTitle = value;
        SetValue(BalloonTipTitleProperty, value);
    }
    }

  6. #6
    Expert confirmé
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 65
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Par défaut
    Finalement, sans passer par la solution de tomlev, cela fonctionne en initialisant la propriété Text dans le CustomControl NDINotifyIcon.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    <Style TargetType="{x:Type local:NDINotifyIcon}">
       <Setter Property="Icon" Value=".../NDIIcon.Ico" />
       <Setter Property="Text" Value="Cliquez pour faire apparaître /disparaîre la fenêtre." />
    
    </Style>
    
    Quelqu'un peut-il m'expliquer pourquoi ?

  7. #7
    Expert confirmé
    Avatar de neguib
    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 627
    Détails du profil
    Informations personnelles :
    Âge : 65
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 627
    Par défaut
    Pardon tomlev je n'avais pas vu ton dernier message.

    Je comprends ce que tu veux dire. J'ai donc modifié :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public String Text
    {
     get { return (String)GetValue(TextProperty); }
     set
     {
        this._notifyIcon.Text = value;
        SetValue(TextProperty, value);
     }
    }
    j'ai aussi modifier ton code pour BalloonTipIcon :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public NDIBalloonTipIcon BalloonTipIcon
    {
      get { return (NDIBalloonTipIcon)GetValue(BalloonTipIconProperty); }
      set
      {
          this._notifyIcon.BalloonTipIcon = (System.Windows.Forms.ToolTipIcon)value;
          SetValue(BalloonTipIconProperty, value); 
      }
    }
    D'ailleurs, ne serait-il pas indiqué de passer par une méthode style OnPropertyChanged ?

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

Discussions similaires

  1. Comment implémenter lemonldap?
    Par Aldo dans le forum Apache
    Réponses: 7
    Dernier message: 25/01/2007, 21h32
  2. Réponses: 13
    Dernier message: 28/05/2006, 15h05
  3. [Droits] Comment garantir au mieux la confidentialité ?
    Par mencaglia dans le forum Décisions SGBD
    Réponses: 2
    Dernier message: 06/02/2006, 09h39
  4. Réponses: 2
    Dernier message: 02/12/2005, 17h22
  5. Comment implémenter un Datawarehouse ?
    Par raslain dans le forum Alimentation
    Réponses: 2
    Dernier message: 20/10/2005, 11h09

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