1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Drawing ;
4+ using System . Linq ;
35using System . Windows . Forms ;
46using ContactPoint . BaseDesign . BaseNotifyControls ;
57
@@ -9,8 +11,8 @@ public class NotifyManager
911 {
1012 private static NotifyManager _instance ;
1113
12- List < NotifyForm > _items = new List < NotifyForm > ( ) ;
13- Form _mainForm ;
14+ private readonly List < NotifyForm > _notifyForms = new List < NotifyForm > ( ) ;
15+ private readonly Form _mainForm ;
1416
1517 private NotifyManager ( Form mainForm )
1618 {
@@ -41,64 +43,64 @@ public void Notify(NotifyControl control)
4143 Notify ( control , 5000 ) ;
4244 }
4345
44- private delegate void NotifyDelegate ( NotifyControl control , int timeout ) ;
4546 public void Notify ( NotifyControl control , int timeout )
4647 {
4748 if ( _mainForm . InvokeRequired )
4849 {
49- _mainForm . BeginInvoke ( new NotifyDelegate ( Notify ) , control , timeout ) ;
50-
50+ _mainForm . BeginInvoke ( new Action < NotifyControl , int > ( Notify ) , control , timeout ) ;
5151 return ;
5252 }
5353
54- var frm = new NotifyForm ( control ) { NotifyControl = control , Timeout = timeout } ;
55- control . NotifyForm = frm ;
56-
57- frm . SetPosition ( GetNextPosition ( frm . Width , frm . Height ) ) ;
54+ var notifyForm = new NotifyForm ( control )
55+ {
56+ NotifyControl = control ,
57+ Timeout = timeout
58+ } ;
5859
59- this . _items . Add ( frm ) ;
60+ notifyForm . SetPosition ( GetNextPosition ( notifyForm . Width , notifyForm . Height ) ) ;
61+ lock ( _notifyForms )
62+ {
63+ _notifyForms . Add ( notifyForm ) ;
64+ }
6065
61- frm . FormClosed += new FormClosedEventHandler ( frm_FormClosed ) ;
66+ notifyForm . FormClosed += NotifyFormClosed ;
6267
68+ control . NotifyForm = notifyForm ;
6369 control . NotifyOnCreate ( ) ;
6470
65- frm . ShowGracefully ( ) ;
71+ notifyForm . ShowGracefully ( ) ;
6672 }
6773
6874 private Point GetNextPosition ( int frmWidth , int frmHeight )
6975 {
70- Rectangle rect = Screen . GetWorkingArea ( this . _mainForm ) ;
76+ var rect = Screen . GetWorkingArea ( _mainForm ) ;
7177
72- for ( int i = 1 ; i <= this . _items . Count ; i ++ )
78+ lock ( _notifyForms )
7379 {
74- Point checkPoint = new Point (
75- rect . Width - frmWidth - 10 ,
76- rect . Height - ( frmHeight + 10 ) * i
77- ) ;
78-
79- if ( CheckPosition ( checkPoint ) )
80- return checkPoint ;
80+ for ( int i = 1 ; i <= _notifyForms . Count ; i ++ )
81+ {
82+ var position = new Point ( rect . Width - frmWidth - 10 , rect . Height - ( frmHeight + 10 ) * i ) ;
83+ if ( _notifyForms . All ( form => form . Location . Y != position . Y ) )
84+ {
85+ return position ;
86+ }
87+ }
8188 }
8289
83- return new Point (
84- rect . Width - frmWidth - 10 ,
85- rect . Height - ( frmHeight + 10 ) * ( this . _items . Count + 1 )
86- ) ;
90+ return new Point ( rect . Width - frmWidth - 10 , rect . Height - ( frmHeight + 10 ) * ( _notifyForms . Count + 1 ) ) ;
8791 }
8892
89- private bool CheckPosition ( Point point )
93+ private void NotifyFormClosed ( object sender , FormClosedEventArgs e )
9094 {
91- foreach ( NotifyForm form in this . _items )
92- if ( form . Location . Y == point . Y )
93- return false ;
94-
95- return true ;
96- }
95+ if ( sender is NotifyForm form )
96+ {
97+ form . FormClosed -= NotifyFormClosed ;
9798
98- void frm_FormClosed ( object sender , FormClosedEventArgs e )
99- {
100- if ( sender != null )
101- this . _items . Remove ( sender as NotifyForm ) ;
99+ lock ( _notifyForms )
100+ {
101+ _notifyForms . Remove ( form ) ;
102+ }
103+ }
102104 }
103105 }
104106}
0 commit comments