-
Notifications
You must be signed in to change notification settings - Fork 946
/
NotifyIcon.cs
883 lines (812 loc) · 31.6 KB
/
NotifyIcon.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using static Interop;
using static Interop.Shell32;
namespace System.Windows.Forms
{
/// <summary>
/// Specifies a component that creates
/// an icon in the Windows System Tray. This class cannot be inherited.
/// </summary>
[DefaultProperty(nameof(Text))]
[DefaultEvent(nameof(MouseDoubleClick))]
[Designer("System.Windows.Forms.Design.NotifyIconDesigner, " + AssemblyRef.SystemDesign)]
[ToolboxItemFilter("System.Windows.Forms")]
[SRDescription(nameof(SR.DescriptionNotifyIcon))]
public sealed class NotifyIcon : Component
{
private static readonly object EVENT_MOUSEDOWN = new object();
private static readonly object EVENT_MOUSEMOVE = new object();
private static readonly object EVENT_MOUSEUP = new object();
private static readonly object EVENT_CLICK = new object();
private static readonly object EVENT_DOUBLECLICK = new object();
private static readonly object EVENT_MOUSECLICK = new object();
private static readonly object EVENT_MOUSEDOUBLECLICK = new object();
private static readonly object EVENT_BALLOONTIPSHOWN = new object();
private static readonly object EVENT_BALLOONTIPCLICKED = new object();
private static readonly object EVENT_BALLOONTIPCLOSED = new object();
private const int WM_TRAYMOUSEMESSAGE = (int)User32.WM.USER + 1024;
private static readonly User32.WM WM_TASKBARCREATED = User32.RegisterWindowMessageW("TaskbarCreated");
private readonly object syncObj = new object();
private Icon icon;
private string text = string.Empty;
private readonly uint id;
private bool added;
private NotifyIconNativeWindow window;
private ContextMenuStrip contextMenuStrip;
private ToolTipIcon balloonTipIcon;
private string balloonTipText = string.Empty;
private string balloonTipTitle = string.Empty;
private static uint s_nextId;
private object userData;
private bool doubleClick; // checks if doubleclick is fired
// Visible defaults to false, but the NotifyIconDesigner makes it seem like the default is
// true. We do this because while visible is the more common case, if it was a true default,
// there would be no way to create a hidden NotifyIcon without being visible for a moment.
private bool visible;
/// <summary>
/// Initializes a new instance of the <see cref='NotifyIcon'/> class.
/// </summary>
public NotifyIcon()
{
id = ++s_nextId;
window = new NotifyIconNativeWindow(this);
UpdateIcon(visible);
}
/// <summary>
/// Initializes a new instance of the <see cref='NotifyIcon'/> class.
/// </summary>
public NotifyIcon(IContainer container) : this()
{
if (container is null)
{
throw new ArgumentNullException(nameof(container));
}
container.Add(this);
}
/// <summary>
/// Gets or sets the BalloonTip text displayed when
/// the mouse hovers over a system tray icon.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[Localizable(true)]
[DefaultValue("")]
[SRDescription(nameof(SR.NotifyIconBalloonTipTextDescr))]
[Editor("System.ComponentModel.Design.MultilineStringEditor, " + AssemblyRef.SystemDesign, typeof(Drawing.Design.UITypeEditor))]
public string BalloonTipText
{
get
{
return balloonTipText;
}
set
{
if (value != balloonTipText)
{
balloonTipText = value;
}
}
}
/// <summary>
/// Gets or sets the BalloonTip icon displayed when
/// the mouse hovers over a system tray icon.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[DefaultValue(ToolTipIcon.None)]
[SRDescription(nameof(SR.NotifyIconBalloonTipIconDescr))]
public ToolTipIcon BalloonTipIcon
{
get
{
return balloonTipIcon;
}
set
{
//valid values are 0x0 to 0x3
SourceGenerated.EnumValidator.Validate(value);
if (value != balloonTipIcon)
{
balloonTipIcon = value;
}
}
}
/// <summary>
/// Gets or sets the BalloonTip title displayed when
/// the mouse hovers over a system tray icon.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[Localizable(true)]
[DefaultValue("")]
[SRDescription(nameof(SR.NotifyIconBalloonTipTitleDescr))]
public string BalloonTipTitle
{
get
{
return balloonTipTitle;
}
set
{
if (value != balloonTipTitle)
{
balloonTipTitle = value;
}
}
}
/// <summary>
/// [This event is raised on the NIN_BALLOONUSERCLICK message.]
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.NotifyIconOnBalloonTipClickedDescr))]
public event EventHandler BalloonTipClicked
{
add => Events.AddHandler(EVENT_BALLOONTIPCLICKED, value);
remove => Events.RemoveHandler(EVENT_BALLOONTIPCLICKED, value);
}
/// <summary>
/// [This event is raised on the NIN_BALLOONTIMEOUT message.]
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.NotifyIconOnBalloonTipClosedDescr))]
public event EventHandler BalloonTipClosed
{
add => Events.AddHandler(EVENT_BALLOONTIPCLOSED, value);
remove => Events.RemoveHandler(EVENT_BALLOONTIPCLOSED, value);
}
/// <summary>
/// [This event is raised on the NIN_BALLOONSHOW or NIN_BALLOONHIDE message.]
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.NotifyIconOnBalloonTipShownDescr))]
public event EventHandler BalloonTipShown
{
add => Events.AddHandler(EVENT_BALLOONTIPSHOWN, value);
remove => Events.RemoveHandler(EVENT_BALLOONTIPSHOWN, value);
}
[DefaultValue(null)]
[SRCategory(nameof(SR.CatBehavior))]
[SRDescription(nameof(SR.NotifyIconMenuDescr))]
public ContextMenuStrip ContextMenuStrip
{
get
{
return contextMenuStrip;
}
set
{
contextMenuStrip = value;
}
}
/// <summary>
/// Gets or sets the current
/// icon.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[Localizable(true)]
[DefaultValue(null)]
[SRDescription(nameof(SR.NotifyIconIconDescr))]
public Icon Icon
{
get
{
return icon;
}
set
{
if (icon != value)
{
icon = value;
UpdateIcon(visible);
}
}
}
/// <summary>
/// Gets or sets the ToolTip text displayed when
/// the mouse hovers over a system tray icon.
/// </summary>
[SRCategory(nameof(SR.CatAppearance))]
[Localizable(true)]
[DefaultValue("")]
[SRDescription(nameof(SR.NotifyIconTextDescr))]
[Editor("System.ComponentModel.Design.MultilineStringEditor, " + AssemblyRef.SystemDesign, typeof(Drawing.Design.UITypeEditor))]
public string Text
{
get
{
return text;
}
set
{
if (value is null)
{
value = string.Empty;
}
if (value != null && !value.Equals(text))
{
if (value != null && value.Length > 63)
{
throw new ArgumentOutOfRangeException(nameof(Text), value, SR.TrayIcon_TextTooLong);
}
text = value;
if (added)
{
UpdateIcon(true);
}
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the icon is visible in the Windows System Tray.
/// </summary>
[SRCategory(nameof(SR.CatBehavior))]
[Localizable(true)]
[DefaultValue(false)]
[SRDescription(nameof(SR.NotifyIconVisDescr))]
public bool Visible
{
get
{
return visible;
}
set
{
if (visible != value)
{
UpdateIcon(value);
visible = value;
}
}
}
[SRCategory(nameof(SR.CatData))]
[Localizable(false)]
[Bindable(true)]
[SRDescription(nameof(SR.ControlTagDescr))]
[DefaultValue(null)]
[TypeConverter(typeof(StringConverter))]
public object Tag
{
get
{
return userData;
}
set
{
userData = value;
}
}
/// <summary>
/// Occurs when the user clicks the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.ControlOnClickDescr))]
public event EventHandler Click
{
add => Events.AddHandler(EVENT_CLICK, value);
remove => Events.RemoveHandler(EVENT_CLICK, value);
}
/// <summary>
/// Occurs when the user double-clicks the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.ControlOnDoubleClickDescr))]
public event EventHandler DoubleClick
{
add => Events.AddHandler(EVENT_DOUBLECLICK, value);
remove => Events.RemoveHandler(EVENT_DOUBLECLICK, value);
}
/// <summary>
/// Occurs when the user clicks the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.NotifyIconMouseClickDescr))]
public event MouseEventHandler MouseClick
{
add => Events.AddHandler(EVENT_MOUSECLICK, value);
remove => Events.RemoveHandler(EVENT_MOUSECLICK, value);
}
/// <summary>
/// Occurs when the user mouse double clicks the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatAction))]
[SRDescription(nameof(SR.NotifyIconMouseDoubleClickDescr))]
public event MouseEventHandler MouseDoubleClick
{
add => Events.AddHandler(EVENT_MOUSEDOUBLECLICK, value);
remove => Events.RemoveHandler(EVENT_MOUSEDOUBLECLICK, value);
}
/// <summary>
/// Occurs when the
/// user presses a mouse button while the pointer is over the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatMouse))]
[SRDescription(nameof(SR.ControlOnMouseDownDescr))]
public event MouseEventHandler MouseDown
{
add => Events.AddHandler(EVENT_MOUSEDOWN, value);
remove => Events.RemoveHandler(EVENT_MOUSEDOWN, value);
}
/// <summary>
/// Occurs
/// when the user moves the mouse pointer over the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatMouse))]
[SRDescription(nameof(SR.ControlOnMouseMoveDescr))]
public event MouseEventHandler MouseMove
{
add => Events.AddHandler(EVENT_MOUSEMOVE, value);
remove => Events.RemoveHandler(EVENT_MOUSEMOVE, value);
}
/// <summary>
/// Occurs when the
/// user releases the mouse button while the pointer
/// is over the icon in the system tray.
/// </summary>
[SRCategory(nameof(SR.CatMouse))]
[SRDescription(nameof(SR.ControlOnMouseUpDescr))]
public event MouseEventHandler MouseUp
{
add => Events.AddHandler(EVENT_MOUSEUP, value);
remove => Events.RemoveHandler(EVENT_MOUSEUP, value);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="NotifyIcon" />
/// and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (window != null)
{
icon = null;
Text = string.Empty;
UpdateIcon(false);
window.DestroyHandle();
window = null;
contextMenuStrip = null;
}
}
else
{
// This same post is done in ControlNativeWindow's finalize method, so if you change
// it, change it there too.
//
if (window != null && window.Handle != IntPtr.Zero)
{
User32.PostMessageW(window, User32.WM.CLOSE);
window.ReleaseHandle();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method raised the BalloonTipClicked event.
/// </summary>
private void OnBalloonTipClicked()
{
((EventHandler)Events[EVENT_BALLOONTIPCLICKED])?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// This method raised the BalloonTipClosed event.
/// </summary>
private void OnBalloonTipClosed()
{
((EventHandler)Events[EVENT_BALLOONTIPCLOSED])?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// This method raised the BalloonTipShown event.
/// </summary>
private void OnBalloonTipShown()
{
((EventHandler)Events[EVENT_BALLOONTIPSHOWN])?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// This method actually raises the Click event. Inheriting classes should
/// override this if they wish to be notified of a Click event. (This is far
/// preferable to actually adding an event handler.) They should not,
/// however, forget to call base.onClick(e); before exiting, to ensure that
/// other recipients do actually get the event.
/// </summary>
private void OnClick(EventArgs e)
{
((EventHandler)Events[EVENT_CLICK])?.Invoke(this, e);
}
/// <summary>
/// Inheriting classes should override this method to handle this event.
/// Call base.onDoubleClick to send this event to any registered event listeners.
/// </summary>
private void OnDoubleClick(EventArgs e)
{
((EventHandler)Events[EVENT_DOUBLECLICK])?.Invoke(this, e);
}
/// <summary>
/// Inheriting classes should override this method to handle this event.
/// Call base.OnMouseClick to send this event to any registered event listeners.
/// </summary>
private void OnMouseClick(MouseEventArgs mea)
{
((MouseEventHandler)Events[EVENT_MOUSECLICK])?.Invoke(this, mea);
}
/// <summary>
/// Inheriting classes should override this method to handle this event.
/// Call base.OnMouseDoubleClick to send this event to any registered event listeners.
/// </summary>
private void OnMouseDoubleClick(MouseEventArgs mea)
{
((MouseEventHandler)Events[EVENT_MOUSEDOUBLECLICK])?.Invoke(this, mea);
}
/// <summary>
/// Raises the <see cref='MouseDown'/> event.
/// Inheriting classes should override this method to handle this event.
/// Call base.onMouseDown to send this event to any registered event listeners.
/// </summary>
private void OnMouseDown(MouseEventArgs e)
{
((MouseEventHandler)Events[EVENT_MOUSEDOWN])?.Invoke(this, e);
}
/// <summary>
/// Inheriting classes should override this method to handle this event.
/// Call base.onMouseMove to send this event to any registered event listeners.
/// </summary>
private void OnMouseMove(MouseEventArgs e)
{
((MouseEventHandler)Events[EVENT_MOUSEMOVE])?.Invoke(this, e);
}
/// <summary>
/// Inheriting classes should override this method to handle this event.
/// Call base.onMouseUp to send this event to any registered event listeners.
/// </summary>
private void OnMouseUp(MouseEventArgs e)
{
((MouseEventHandler)Events[EVENT_MOUSEUP])?.Invoke(this, e);
}
/// <summary>
/// Displays a balloon tooltip in the taskbar.
///
/// The system enforces minimum and maximum timeout values. Timeout
/// values that are too large are set to the maximum value and values
/// that are too small default to the minimum value. The operating system's
/// default minimum and maximum timeout values are 10 seconds and 30 seconds,
/// respectively.
///
/// No more than one balloon ToolTip at at time is displayed for the taskbar.
/// If an application attempts to display a ToolTip when one is already being displayed,
/// the ToolTip will not appear until the existing balloon ToolTip has been visible for at
/// least the system minimum timeout value. For example, a balloon ToolTip with timeout
/// set to 30 seconds has been visible for seven seconds when another application attempts
/// to display a balloon ToolTip. If the system minimum timeout is ten seconds, the first
/// ToolTip displays for an additional three seconds before being replaced by the second ToolTip.
/// </summary>
public void ShowBalloonTip(int timeout)
{
ShowBalloonTip(timeout, balloonTipTitle, balloonTipText, balloonTipIcon);
}
/// <summary>
/// Displays a balloon tooltip in the taskbar with the specified title,
/// text, and icon for a duration of the specified timeout value.
///
/// The system enforces minimum and maximum timeout values. Timeout
/// values that are too large are set to the maximum value and values
/// that are too small default to the minimum value. The operating system's
/// default minimum and maximum timeout values are 10 seconds and 30 seconds,
/// respectively.
///
/// No more than one balloon ToolTip at at time is displayed for the taskbar.
/// If an application attempts to display a ToolTip when one is already being displayed,
/// the ToolTip will not appear until the existing balloon ToolTip has been visible for at
/// least the system minimum timeout value. For example, a balloon ToolTip with timeout
/// set to 30 seconds has been visible for seven seconds when another application attempts
/// to display a balloon ToolTip. If the system minimum timeout is ten seconds, the first
/// ToolTip displays for an additional three seconds before being replaced by the second ToolTip.
/// </summary>
public unsafe void ShowBalloonTip(int timeout, string tipTitle, string tipText, ToolTipIcon tipIcon)
{
if (timeout < 0)
{
throw new ArgumentOutOfRangeException(nameof(timeout), timeout, string.Format(SR.InvalidArgument, nameof(timeout), timeout));
}
if (string.IsNullOrEmpty(tipText))
{
throw new ArgumentException(SR.NotifyIconEmptyOrNullTipText);
}
//valid values are 0x0 to 0x3
SourceGenerated.EnumValidator.Validate(tipIcon, nameof(tipIcon));
if (added)
{
// Bail if in design mode...
if (DesignMode)
{
return;
}
var data = new NOTIFYICONDATAW
{
cbSize = (uint)sizeof(NOTIFYICONDATAW),
uFlags = NIF.INFO,
uID = id,
uTimeoutOrVersion = (uint)timeout
};
if (window.Handle == IntPtr.Zero)
{
window.CreateHandle(new CreateParams());
}
data.hWnd = window.Handle;
data.InfoTitle = tipTitle;
data.Info = tipText;
switch (tipIcon)
{
case ToolTipIcon.Info:
data.dwInfoFlags = NIIF.INFO;
break;
case ToolTipIcon.Warning:
data.dwInfoFlags = NIIF.WARNING;
break;
case ToolTipIcon.Error:
data.dwInfoFlags = NIIF.ERROR;
break;
case ToolTipIcon.None:
data.dwInfoFlags = NIIF.NONE;
break;
}
Shell32.Shell_NotifyIconW(NIM.MODIFY, ref data);
}
}
/// <summary>
/// Shows the context menu for the tray icon.
/// </summary>
private void ShowContextMenu()
{
if (contextMenuStrip != null)
{
User32.GetCursorPos(out Point pt);
// Summary: the current window must be made the foreground window
// before calling TrackPopupMenuEx, and a task switch must be
// forced after the call.
User32.SetForegroundWindow(window);
// this will set the context menu strip to be toplevel
// and will allow us to overlap the system tray
contextMenuStrip.ShowInTaskbar(pt.X, pt.Y);
}
}
/// <summary>
/// Updates the icon in the system tray.
/// </summary>
private unsafe void UpdateIcon(bool showIconInTray)
{
lock (syncObj)
{
// Bail if in design mode...
//
if (DesignMode)
{
return;
}
window.LockReference(showIconInTray);
var data = new NOTIFYICONDATAW
{
cbSize = (uint)sizeof(NOTIFYICONDATAW),
uCallbackMessage = WM_TRAYMOUSEMESSAGE,
uFlags = NIF.MESSAGE,
uID = id
};
if (showIconInTray)
{
if (window.Handle == IntPtr.Zero)
{
window.CreateHandle(new CreateParams());
}
}
data.hWnd = window.Handle;
if (icon != null)
{
data.uFlags |= NIF.ICON;
data.hIcon = icon.Handle;
}
data.uFlags |= NIF.TIP;
data.Tip = text;
if (showIconInTray && icon != null)
{
if (!added)
{
Shell_NotifyIconW(NIM.ADD, ref data);
added = true;
}
else
{
Shell_NotifyIconW(NIM.MODIFY, ref data);
}
}
else if (added)
{
Shell_NotifyIconW(NIM.DELETE, ref data);
added = false;
}
}
}
/// <summary>
/// Handles the mouse-down event
/// </summary>
private void WmMouseDown(ref Message m, MouseButtons button, int clicks)
{
if (clicks == 2)
{
OnDoubleClick(new MouseEventArgs(button, 2, 0, 0, 0));
OnMouseDoubleClick(new MouseEventArgs(button, 2, 0, 0, 0));
doubleClick = true;
}
OnMouseDown(new MouseEventArgs(button, clicks, 0, 0, 0));
}
/// <summary>
/// Handles the mouse-move event
/// </summary>
private void WmMouseMove(ref Message m)
{
OnMouseMove(new MouseEventArgs(Control.MouseButtons, 0, 0, 0, 0));
}
/// <summary>
/// Handles the mouse-up event
/// </summary>
private void WmMouseUp(ref Message m, MouseButtons button)
{
OnMouseUp(new MouseEventArgs(button, 0, 0, 0, 0));
//subhag
if (!doubleClick)
{
OnClick(new MouseEventArgs(button, 0, 0, 0, 0));
OnMouseClick(new MouseEventArgs(button, 0, 0, 0, 0));
}
doubleClick = false;
}
private void WmTaskbarCreated(ref Message m)
{
added = false;
UpdateIcon(visible);
}
private void WndProc(ref Message msg)
{
switch ((User32.WM)msg.Msg)
{
case (User32.WM)WM_TRAYMOUSEMESSAGE:
switch ((int)msg.LParam)
{
case (int)User32.WM.LBUTTONDBLCLK:
WmMouseDown(ref msg, MouseButtons.Left, 2);
break;
case (int)User32.WM.LBUTTONDOWN:
WmMouseDown(ref msg, MouseButtons.Left, 1);
break;
case (int)User32.WM.LBUTTONUP:
WmMouseUp(ref msg, MouseButtons.Left);
break;
case (int)User32.WM.MBUTTONDBLCLK:
WmMouseDown(ref msg, MouseButtons.Middle, 2);
break;
case (int)User32.WM.MBUTTONDOWN:
WmMouseDown(ref msg, MouseButtons.Middle, 1);
break;
case (int)User32.WM.MBUTTONUP:
WmMouseUp(ref msg, MouseButtons.Middle);
break;
case (int)User32.WM.MOUSEMOVE:
WmMouseMove(ref msg);
break;
case (int)User32.WM.RBUTTONDBLCLK:
WmMouseDown(ref msg, MouseButtons.Right, 2);
break;
case (int)User32.WM.RBUTTONDOWN:
WmMouseDown(ref msg, MouseButtons.Right, 1);
break;
case (int)User32.WM.RBUTTONUP:
if (contextMenuStrip != null)
{
ShowContextMenu();
}
WmMouseUp(ref msg, MouseButtons.Right);
break;
case (int)NIN.BALLOONSHOW:
OnBalloonTipShown();
break;
case (int)NIN.BALLOONHIDE:
OnBalloonTipClosed();
break;
case (int)NIN.BALLOONTIMEOUT:
OnBalloonTipClosed();
break;
case (int)NIN.BALLOONUSERCLICK:
OnBalloonTipClicked();
break;
}
break;
case User32.WM.COMMAND:
if (IntPtr.Zero == msg.LParam)
{
if (Command.DispatchID((int)msg.WParam & 0xFFFF))
{
return;
}
}
else
{
window.DefWndProc(ref msg);
}
break;
case User32.WM.DESTROY:
// Remove the icon from the taskbar
UpdateIcon(false);
break;
case User32.WM.INITMENUPOPUP:
default:
if (msg.Msg == (int)WM_TASKBARCREATED)
{
WmTaskbarCreated(ref msg);
}
window.DefWndProc(ref msg);
break;
}
}
/// <summary>
/// Defines a placeholder window that the NotifyIcon is attached to.
/// </summary>
private class NotifyIconNativeWindow : NativeWindow
{
internal NotifyIcon reference;
private GCHandle rootRef; // We will root the control when we do not want to be elligible for garbage collection.
/// <summary>
/// Create a new NotifyIcon, and bind the window to the NotifyIcon component.
/// </summary>
internal NotifyIconNativeWindow(NotifyIcon component)
{
reference = component;
}
~NotifyIconNativeWindow()
{
// This same post is done in Control's Dispose method, so if you change
// it, change it there too.
//
if (Handle != IntPtr.Zero)
{
User32.PostMessageW(this, User32.WM.CLOSE);
}
// This releases the handle from our window proc, re-routing it back to
// the system.
}
public void LockReference(bool locked)
{
if (locked)
{
if (!rootRef.IsAllocated)
{
rootRef = GCHandle.Alloc(reference, GCHandleType.Normal);
}
}
else
{
if (rootRef.IsAllocated)
{
rootRef.Free();
}
}
}
protected override void OnThreadException(Exception e)
{
Application.OnThreadException(e);
}
/// <summary>
/// Pass messages on to the NotifyIcon object's wndproc handler.
/// </summary>
protected override void WndProc(ref Message m)
{
Debug.Assert(reference != null, "NotifyIcon was garbage collected while it was still visible. How did we let that happen?");
reference.WndProc(ref m);
}
}
}
}