@@ -171,6 +171,16 @@ public void UpdateSerialPortDescription()
171171 else toolStripPortDetails . Text = portName ;
172172 }
173173
174+ public static string EncodeNumConfig ( int [ ] param )
175+ {
176+ return string . Join ( "," , param ) ;
177+ }
178+
179+ public static int DecodeNumConfig ( string config , int index )
180+ {
181+ return int . Parse ( config . Split ( ',' ) [ index ] ) ;
182+ }
183+
174184 public static string ByteArrayToFormatedString ( byte [ ] bytes )
175185 {
176186 var sb = new StringBuilder ( ) ;
@@ -221,11 +231,6 @@ public MainForm()
221231 }
222232
223233 private void MainForm_Load ( object sender , EventArgs e )
224- {
225-
226- }
227-
228- private void MainForm_Shown ( object sender , EventArgs e )
229234 {
230235 try
231236 {
@@ -237,11 +242,31 @@ private void MainForm_Shown(object sender, EventArgs e)
237242 comboBoxPortName . SelectedIndex = comboBoxPortName . Items . IndexOf ( Config . PortName ) ;
238243 if ( comboBoxBaudRate . Items . Contains ( Config . BaudRate ) )
239244 comboBoxBaudRate . SelectedIndex = comboBoxBaudRate . Items . IndexOf ( Config . BaudRate ) ;
245+ this . Size = new Size ( DecodeNumConfig ( Config . WindowSize , 0 ) , DecodeNumConfig ( Config . WindowSize , 1 ) ) ;
246+ var winlocation = new Point ( DecodeNumConfig ( Config . WindowLocation , 0 ) , DecodeNumConfig ( Config . WindowLocation , 1 ) ) ;
247+
248+ // check location within screen bounds
249+ if ( winlocation . X >= Screen . AllScreens . Sum ( p => p . Bounds . Width ) )
250+ {
251+ winlocation . X = ( Screen . PrimaryScreen . Bounds . Width - this . Width ) / 2 ;
252+ winlocation . Y = ( Screen . PrimaryScreen . Bounds . Height - this . Height ) / 2 ;
253+ }
254+ this . Location = winlocation ;
255+ }
256+ catch ( Exception ex )
257+ {
258+ PopupException ( ex . Message ) ;
259+ }
260+ }
240261
262+ private void MainForm_Shown ( object sender , EventArgs e )
263+ {
264+ try
265+ {
241266 MonitorDeviceChanges ( ) ;
242- toolStripStatusLabel1 . Text = "Ready to Connect" ;
243267 labelRxSelection . Text = "RxSel: 0" ;
244268 labelTxSelection . Text = "TxSel: 0" ;
269+ toolStripStatusLabel1 . Text = "Ready to Connect" ;
245270 }
246271 catch ( Exception ex )
247272 {
@@ -258,6 +283,8 @@ private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
258283 portName = _portNames [ comboBoxPortName . SelectedIndex ] ;
259284 Config . PortName = portName ;
260285 Config . BaudRate = comboBoxBaudRate . Text ;
286+ Config . WindowSize = EncodeNumConfig ( new [ ] { this . Size . Width , this . Size . Height } ) ;
287+ Config . WindowLocation = EncodeNumConfig ( new [ ] { this . Location . X , this . Location . Y } ) ;
261288 Config . Save ( ) ;
262289 _serialCom . Dispose ( ) ;
263290 }
0 commit comments