Skip to content

Commit 650bf69

Browse files
committed
Save/Restore Position
1 parent 1516b51 commit 650bf69

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

winUpdateMiniTool/MainForm.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public MainForm() {
4545
notifyIcon.Text = Updater.ApplicationTitle;
4646

4747
TryApplyUIFont();
48+
RestorePosition();
4849

4950
if (Program.TestArg("-tray")) {
5051
allowShowDisplay = false;
@@ -419,6 +420,8 @@ private void MainFormClosing(object sender, FormClosingEventArgs e) {
419420
return;
420421
}
421422

423+
SavePosition();
424+
422425
agent.Progress -= OnProgress;
423426
agent.UpdatesChanged -= OnUpdates;
424427
agent.Finished -= OnFinished;
@@ -1455,4 +1458,44 @@ private void TryApplyUIFont() {
14551458
AppLog.Line($"Error restoring saved UI font: {ex.Message}");
14561459
}
14571460
}
1461+
1462+
private void SavePosition() {
1463+
var bounds = WindowState == FormWindowState.Normal
1464+
? Bounds
1465+
: RestoreBounds;
1466+
SetConfig("Window_X", bounds.X.ToString());
1467+
SetConfig("Window_Y", bounds.Y.ToString());
1468+
SetConfig("Window_Width", bounds.Width.ToString());
1469+
SetConfig("Window_Height", bounds.Height.ToString());
1470+
1471+
var state = WindowState == FormWindowState.Minimized
1472+
? FormWindowState.Normal
1473+
: WindowState;
1474+
SetConfig("Window_State", ((int)state).ToString());
1475+
}
1476+
1477+
private void RestorePosition() {
1478+
bool hasX = int.TryParse(GetConfig("Window_X"), out var x);
1479+
bool hasY = int.TryParse(GetConfig("Window_Y"), out var y);
1480+
bool hasW = int.TryParse(GetConfig("Window_Width"), out var w);
1481+
bool hasH = int.TryParse(GetConfig("Window_Height"), out var h);
1482+
bool hasAll = hasX && hasY && hasW && hasH;
1483+
1484+
Rectangle bounds = new(x, y, w, h);
1485+
bool isVisible = Screen.AllScreens.Any(s => s.WorkingArea.IntersectsWith(bounds));
1486+
1487+
if (hasAll && isVisible) {
1488+
StartPosition = FormStartPosition.Manual;
1489+
Bounds = bounds;
1490+
if (int.TryParse(GetConfig("Window_State"), out var stateValue)) {
1491+
var state = (FormWindowState)stateValue;
1492+
if (state != FormWindowState.Minimized) {
1493+
WindowState = state;
1494+
}
1495+
}
1496+
}
1497+
else {
1498+
StartPosition = FormStartPosition.CenterScreen;
1499+
}
1500+
}
14581501
}

0 commit comments

Comments
 (0)