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 à :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é.
- 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>
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}}"/>
Voici le code du CustomControl NDINotifyIcon :
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
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(); } }
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
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; }
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é.
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); } }![]()
Partager