11using System ;
22using System . Collections . Generic ;
3+ using System . Diagnostics ;
34using System . Drawing ;
45using System . IO ;
56using System . Linq ;
@@ -23,26 +24,30 @@ public partial class MainForm : Form
2324 public MainForm ( )
2425 {
2526 InitializeComponent ( ) ;
27+ //之后再绑定 ContextMenuStrip,否则可能出BUG
28+ //Another Fix:https://github.com/HMBSbige/TCPingInfoView/commit/e058c15dc7265358ed08f75765d62f9ece6b410e#diff-654cfc907abeeabf4121da2c3df241e7L143
29+ File_MenuItem . DropDown = NotifyIcon_MenuStrip ;
30+ View_MenuItem . DropDown = MainList_MenuStrip ;
31+ //Load Icon
2632 Icon = Resources . TCPing ;
27- notifyIcon1 . Icon = Resources . TCPing_White ;
33+ notifyIcon1 . Icon = Resources . TCPing ;
2834 Config . Load ( ) ;
2935 }
3036
3137 private static string ExeName => Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ;
3238 private readonly AppConfig Config = new AppConfig ( $@ ".\{ ExeName } .json") ;
3339 private static string ListPath => $@ ".\{ ExeName } .txt";
3440
35- #region DPI
41+ #region DPI参数
3642
37- private double Dpi => this . GetDpi ( ) ;
43+ private double Dpi => this . GetDeviceDpi ( ) ;
3844 private static Size DefPicSize => new Size ( 16 , 16 ) ;
3945 private Size DpiPicSize => new Size ( Convert . ToInt32 ( DefPicSize . Width * Dpi ) , Convert . ToInt32 ( DefPicSize . Height * Dpi ) ) ;
4046
4147 #endregion
4248
4349 #region 表格显示参数
4450
45- private int RemainingHeight ;
4651 private double ListRatio ;
4752 public const int ColumnsCount = 12 ;
4853
@@ -52,6 +57,7 @@ public MainForm()
5257
5358 private bool _isNotifyClose ;
5459 private bool _isShowDateList ;
60+ private FormWindowState DefaultState = FormWindowState . Normal ;
5561
5662 #endregion
5763
@@ -102,6 +108,50 @@ public MainForm()
102108
103109 #endregion
104110
111+ #region DPI改变
112+
113+ private void MainForm_DpiChanged ( object sender , DpiChangedEventArgs e )
114+ {
115+ Debug . WriteLine ( $@ "DPI:{ e . DeviceDpiOld } =>{ e . DeviceDpiNew } { this . GetDeviceDpi ( ) * 100 } %") ;
116+ LoadControlsByDpi ( ) ;
117+ Task . Run ( ( ) =>
118+ {
119+ this . Invoke ( ( ) =>
120+ {
121+ ++ Height ;
122+ -- Height ;
123+ } ) ;
124+ } ) ;
125+ }
126+
127+ private void LoadControlsByDpi ( )
128+ {
129+ if ( Dpi > 1.0 )
130+ {
131+ Test_Button . ImageScaling = ToolStripItemImageScaling . None ;
132+ Test_Button . Image = Util . Util . ResizeImage ( Resources . Test , DpiPicSize ) ;
133+ Start_Button . ImageScaling = ToolStripItemImageScaling . None ;
134+ Start_Button . Image = Util . Util . ResizeImage ( Resources . Start , DpiPicSize ) ;
135+ Exit_Button . ImageScaling = ToolStripItemImageScaling . None ;
136+ Exit_Button . Image = Util . Util . ResizeImage ( Resources . Exit , DpiPicSize ) ;
137+ Load_Button . ImageScaling = ToolStripItemImageScaling . None ;
138+ Load_Button . Image = Util . Util . ResizeImage ( Resources . Load , DpiPicSize ) ;
139+ Minimize_Button . ImageScaling = ToolStripItemImageScaling . None ;
140+ Minimize_Button . Image = Util . Util . ResizeImage ( Resources . Minimize , DpiPicSize ) ;
141+ }
142+ else
143+ {
144+ Test_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
145+ Test_Button . Image = Resources . Test ;
146+ Start_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
147+ Exit_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
148+ Load_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
149+ Minimize_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
150+ }
151+ }
152+
153+ #endregion
154+
105155 #region 窗口第一次载入
106156
107157 private void LoadMainList ( )
@@ -142,36 +192,21 @@ private void LoadDateList()
142192 DateList . Columns [ 1 ] . DataPropertyName = @"Latency" ;
143193 }
144194
145- private void LoadButtons ( )
195+ private void LoadSetting ( )
146196 {
147- if ( Dpi > 1.0 )
197+ Height = Config . MainFormHeight ;
198+ Width = Config . MainFormWidth ;
199+ DateList . Height = Config . DateListHeight ;
200+
201+ if ( Util . Util . IsOnScreen ( new Point ( Config . StartPositionLeft , Config . StartPositionTop ) , this ) )
148202 {
149- Test_Button . ImageScaling = ToolStripItemImageScaling . None ;
150- Test_Button . Image = Util . Util . ResizeImage ( Resources . Test , DpiPicSize ) ;
151- Start_Button . ImageScaling = ToolStripItemImageScaling . None ;
152- Start_Button . Image = Util . Util . ResizeImage ( Resources . Start , DpiPicSize ) ;
153- Exit_Button . ImageScaling = ToolStripItemImageScaling . None ;
154- Exit_Button . Image = Util . Util . ResizeImage ( Resources . Exit , DpiPicSize ) ;
155- Load_Button . ImageScaling = ToolStripItemImageScaling . None ;
156- Load_Button . Image = Util . Util . ResizeImage ( Resources . Load , DpiPicSize ) ;
157- Minimize_Button . ImageScaling = ToolStripItemImageScaling . None ;
158- Minimize_Button . Image = Util . Util . ResizeImage ( Resources . Minimize , DpiPicSize ) ;
203+ StartPosition = FormStartPosition . Manual ;
204+ Location = new Point ( Config . StartPositionLeft , Config . StartPositionTop ) ;
159205 }
160206 else
161207 {
162- Test_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
163- Start_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
164- Exit_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
165- Load_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
166- Minimize_Button . ImageScaling = ToolStripItemImageScaling . SizeToFit ;
208+ StartPosition = FormStartPosition . CenterScreen ;
167209 }
168- }
169-
170- private void LoadSetting ( )
171- {
172- Height = Config . MainFormHeight ;
173- Width = Config . MainFormWidth ;
174- DateList . Height = Config . DateListHeight ;
175210
176211 IsNotifyClose_MenuItem . Checked = Config . IsNotifyClose ;
177212 IsShowDateList_MenuItem . CheckState = Config . IsShowDateList ? CheckState . Checked : CheckState . Unchecked ;
@@ -197,6 +232,24 @@ private void LoadSetting()
197232 }
198233 }
199234
235+ private void SetMiniSize ( )
236+ {
237+ var miniHeight = Height - ClientRectangle . Height + menuStrip1 . Height + toolStrip1 . Height + statusStrip1 . Height ;
238+
239+ var h1 = MainList . RowTemplate . Height + MainList . ColumnHeadersHeight ;
240+ miniHeight += h1 ;
241+ splitter1 . MinExtra = h1 ;
242+ MainList . MinimumSize = new Size ( 0 , h1 ) ;
243+
244+ var h2 = DateList . RowTemplate . Height + DateList . ColumnHeadersHeight ;
245+ miniHeight += h2 ;
246+ splitter1 . MinSize = h2 ;
247+ DateList . MinimumSize = new Size ( 0 , h2 ) ;
248+
249+ miniHeight += splitter1 . Height ;
250+ MinimumSize = new Size ( 0 , miniHeight ) ;
251+ }
252+
200253 private void LoadSteam ( )
201254 {
202255 if ( SteamManager . IsLoaded )
@@ -207,14 +260,20 @@ private void LoadSteam()
207260
208261 private void MainForm_Load ( object sender , EventArgs e )
209262 {
263+ if ( ! DpiUtils . CheckHighDpiEnvironment ( ) )
264+ {
265+ MessageBox . Show ( @"TCPingInfoView 可能无法正常适配你的高 DPI 环境!" , @"High DPI Environment Check" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
266+ }
267+
210268 LoadSetting ( ) ;
211269
212270 LoadSteam ( ) ;
213271
214- RemainingHeight = Height - ( MainList . Height + DateList . Height ) ;
272+ SetMiniSize ( ) ;
273+
215274 ChangedRatio ( ) ;
216275
217- LoadButtons ( ) ;
276+ LoadControlsByDpi ( ) ;
218277
219278 MainList . AutoGenerateColumns = false ;
220279 MainList . DataSource = MainTable ;
@@ -371,6 +430,7 @@ private void FirstPing()
371430 state . Stop ( ) ;
372431 }
373432 } ) ;
433+ //notifyIcon1.ShowBalloonTip(1000, ExeName, @"载入完毕", ToolTipIcon.Info);
374434 } ) ;
375435 PingTasks . Add ( t ) ;
376436 t . Start ( ) ;
@@ -409,6 +469,10 @@ private void PingOne(int index)
409469 {
410470 res = Convert . ToInt32 ( Math . Round ( latency . Value ) ) ;
411471 }
472+ else
473+ {
474+ //notifyIcon1.ShowBalloonTip(1000, time.ToString(CultureInfo.CurrentCulture), $"{mainTable[index].HostsName}\n{ipe}", ToolTipIcon.Error);
475+ }
412476
413477 var log = new DateTable
414478 {
@@ -418,12 +482,12 @@ private void PingOne(int index)
418482
419483 mainTable [ index ] . AddNewLog ( log ) ;
420484
421- if ( MainList . SelectedRows . Count > 0 )
485+ if ( MainList . SelectedRows . Count == 1 )
422486 {
423487 var i = MainList . SelectedRows [ 0 ] . Cells [ 0 ] . Value as int ? ;
424- if ( i == index )
488+ if ( i == index && DateList . Visible )
425489 {
426- DateList . Invoke ( ( ) => { LoadLogs ( index ) ; } ) ;
490+ DateList . Invoke ( ( ) => { LoadLogs ( log ) ; } ) ;
427491 }
428492 }
429493 }
@@ -472,13 +536,17 @@ private void toolStripButton1_Click(object sender, EventArgs e)
472536
473537 private void ChangedSize ( )
474538 {
475- var height = Height - RemainingHeight ;
539+ var height = Height - MinimumSize . Height ;
476540 DateList . Height = Convert . ToInt32 ( ListRatio * height ) ;
477541 }
478542
479543 private void ChangedRatio ( )
480544 {
481545 ListRatio = Convert . ToDouble ( DateList . Height ) / Convert . ToDouble ( MainList . Height + DateList . Height ) ;
546+ if ( ! ( ListRatio < 1 && ListRatio > 0 ) )
547+ {
548+ ListRatio = 0.1 ;
549+ }
482550 }
483551
484552 private void splitter1_SplitterMoved ( object sender , SplitterEventArgs e )
@@ -493,11 +561,15 @@ private void MainForm_Resize(object sender, EventArgs e)
493561 {
494562 TriggerMainFormDisplay ( ) ;
495563 }
564+ else
565+ {
566+ DefaultState = WindowState ;
567+ }
496568 }
497569
498570 #endregion
499571
500- #region 主窗口显示隐藏
572+ #region 主窗口切换显示隐藏
501573
502574 private void notifyIcon1_MouseDoubleClick ( object sender , MouseEventArgs e )
503575 {
@@ -521,7 +593,7 @@ private void TriggerMainFormDisplay()
521593 {
522594 if ( WindowState == FormWindowState . Minimized )
523595 {
524- WindowState = FormWindowState . Normal ;
596+ WindowState = DefaultState ;
525597 }
526598
527599 TopMost = true ;
@@ -669,6 +741,8 @@ private void SaveConfig()
669741 //
670742 Config . MainFormHeight = Height ;
671743 Config . MainFormWidth = Width ;
744+ Config . StartPositionLeft = Location . X ;
745+ Config . StartPositionTop = Location . Y ;
672746 Config . DateListHeight = DateList . Height ;
673747 Config . IsNotifyClose = _isNotifyClose ;
674748 Config . IsShowDateList = _isShowDateList ;
@@ -738,7 +812,7 @@ private void MainList_DragEnter(object sender, DragEventArgs e)
738812
739813 private void MainList_SelectionChanged ( object sender , EventArgs e )
740814 {
741- if ( MainList . SelectedRows . Count <= 0 )
815+ if ( MainList . SelectedRows . Count != 1 )
742816 {
743817 return ;
744818 }
@@ -754,6 +828,22 @@ private void MainList_SelectionChanged(object sender, EventArgs e)
754828 }
755829 }
756830
831+ private void LoadLogs ( DateTable log )
832+ {
833+ try
834+ {
835+ DateTable . Add ( log ) ;
836+ if ( DateList . SelectedRows . Count == 0 )
837+ {
838+ DateList . Rows [ 0 ] . Selected = true ;
839+ }
840+ }
841+ catch
842+ {
843+ // ignored
844+ }
845+ }
846+
757847 private void LoadLogs ( int index )
758848 {
759849 try
@@ -958,6 +1048,16 @@ private void IsShowDateList_MenuItem_CheckStateChanged(object sender, EventArgs
9581048 _isShowDateList = IsShowDateList_MenuItem . Checked ;
9591049 splitter1 . Visible = _isShowDateList ;
9601050 DateList . Visible = _isShowDateList ;
1051+ if ( DateList . Visible )
1052+ {
1053+ if ( MainList . SelectedRows . Count == 1 )
1054+ {
1055+ if ( MainList . SelectedRows [ 0 ] . Cells [ 0 ] . Value is int index )
1056+ {
1057+ LoadLogs ( index ) ;
1058+ }
1059+ }
1060+ }
9611061 }
9621062
9631063 #endregion
@@ -982,7 +1082,7 @@ private void DisplayedColumns_MenuItem_Click(object sender, EventArgs e)
9821082 private void ShowLogForm_MenuItem_Click ( object sender , EventArgs e )
9831083 {
9841084 var i = MainList . SelectedRows . Count ;
985- if ( i > 0 )
1085+ if ( i == 1 )
9861086 {
9871087 if ( MainList . Rows [ MainList . SelectedRows [ 0 ] . Index ] . Cells [ 0 ] . Value is int index )
9881088 {
@@ -1056,7 +1156,7 @@ private bool IsContainsString(int rowIndex, string str)
10561156 private void SearchMainList ( )
10571157 {
10581158 var index = 0 ;
1059- if ( MainList . SelectedRows . Count > 0 )
1159+ if ( MainList . SelectedRows . Count == 1 )
10601160 {
10611161 index = MainList . SelectedRows [ 0 ] . Index ;
10621162 }
0 commit comments