Skip to content

Commit c696a5b

Browse files
committed
code clean
1 parent b722630 commit c696a5b

2 files changed

Lines changed: 97 additions & 128 deletions

File tree

winUpdateMiniTool/MainForm.cs

Lines changed: 90 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,25 @@ namespace winUpdateMiniTool;
1717

1818
public partial class MainForm : Form {
1919

20-
public const int MF_BITMAP = 0x00000004;
21-
public const int MF_CHECKED = 0x00000008;
22-
public const int MF_DISABLED = 0x00000002;
23-
public const int MF_ENABLED = 0x00000000;
24-
public const int MF_GRAYED = 0x00000001;
25-
public const int MF_MENUBARBREAK = 0x00000020;
26-
public const int MF_MENUBREAK = 0x00000040;
27-
public const int MF_OWNERDRAW = 0x00000100;
28-
private const int MfPopup = 0x00000010;
29-
private const int MfSeparator = 0x00000800;
30-
public const int MF_STRING = 0x00000000;
31-
public const int MF_UNCHECKED = 0x00000000;
32-
private const int MfByposition = 0x400;
33-
public const int MF_BYCOMMAND = 0x000;
34-
private const int WmSyscommand = 0x112;
35-
//public const Int32 MF_REMOVE = 0x1000;
36-
private const int SysMenuCheckUpdates = 1000;
37-
private const int SysMenuAboutId = 1001;
20+
// private const int MF_BITMAP = 0x00000004;
21+
// private const int MF_CHECKED = 0x00000008;
22+
// private const int MF_DISABLED = 0x00000002;
23+
// private const int MF_ENABLED = 0x00000000;
24+
// private const int MF_GRAYED = 0x00000001;
25+
// private const int MF_MENUBARBREAK = 0x00000020;
26+
// private const int MF_MENUBREAK = 0x00000040;
27+
// private const int MF_OWNERDRAW = 0x00000100;
28+
// private const int MF_STRING = 0x00000000;
29+
// private const int MF_UNCHECKED = 0x00000000;
30+
// private const int MF_BYCOMMAND = 0x000;
31+
//private const Int32 MF_REMOVE = 0x1000;
32+
private const int MF_POPUP = 0x00000010;
33+
private const int MF_SEPARATOR = 0x00000800;
34+
private const int MF_BY_POSITION = 0x400;
35+
private const int MF_WM_SYS_COMMAND = 0x112;
36+
private const int MF_SYS_MENU_CHECK_UPDATES = 1000;
37+
private const int MF_SYS_MENU_ABOUT_ID = 1001;
38+
3839
private static Timer mTimer;
3940
private readonly WuAgent agent;
4041
private readonly int idleDelay;
@@ -69,7 +70,7 @@ public MainForm() {
6970
}
7071

7172
if (!MiscFunc.IsRunningAsUwp())
72-
Text = $"{Updater.ApplicationTitle} v{Program.MVersion}";
73+
Text = $"{Updater.ApplicationTitle} v{Updater.CurrentVersion}";
7374

7475
btnWinUpd.Text = string.Format("Windows Update ({0})", 0);
7576
btnInstalled.Text = string.Format("Installed Updates ({0})", 0);
@@ -151,6 +152,7 @@ public MainForm() {
151152
dlShTime.SelectedIndex = time;
152153
}
153154
catch {
155+
// ignored
154156
}
155157

156158
if (mWinVersion >= 10) // 10 or abive
@@ -164,6 +166,7 @@ public MainForm() {
164166
dlAutoCheck.SelectedIndex = MiscFunc.ParseInt(GetConfig("AutoUpdate", "0"));
165167
}
166168
catch {
169+
// ignored
167170
}
168171

169172
chkAutoRun.Checked = Program.IsAutoStart();
@@ -245,10 +248,10 @@ public MainForm() {
245248
notifyIcon.ContextMenu.MenuItems.AddRange([mToolsMenu, new MenuItem("-"), new MenuItem("E&xit", menuExit_Click)]);
246249

247250
var menuHandle = GetSystemMenu(Handle, false); // Note: to restore default set true
248-
InsertMenu(menuHandle, 5, MfByposition | MfSeparator, 0, string.Empty); // <-- Add a menu separator
249-
InsertMenu(menuHandle, 6, MfByposition | MfPopup, (int)mToolsMenu.Handle, mToolsMenu.Text);
250-
InsertMenu(menuHandle, 7, MfByposition, SysMenuCheckUpdates, "Check for new version");
251-
InsertMenu(menuHandle, 8, MfByposition, SysMenuAboutId, "&About…");
251+
InsertMenu(menuHandle, 5, MF_BY_POSITION | MF_SEPARATOR, 0, string.Empty); // <-- Add a menu separator
252+
InsertMenu(menuHandle, 6, MF_BY_POSITION | MF_POPUP, (int)mToolsMenu.Handle, mToolsMenu.Text);
253+
InsertMenu(menuHandle, 7, MF_BY_POSITION, MF_SYS_MENU_CHECK_UPDATES, "Check for new version");
254+
InsertMenu(menuHandle, 8, MF_BY_POSITION, MF_SYS_MENU_ABOUT_ID, "&About…");
252255

253256
UpdateCounts();
254257
SwitchList(UpdateLists.UpdateHistory);
@@ -277,13 +280,13 @@ public MainForm() {
277280
protected override void WndProc(ref Message m) {
278281

279282
base.WndProc(ref m);
280-
if (m.Msg == WmSyscommand) {
283+
if (m.Msg == MF_WM_SYS_COMMAND) {
281284
switch ((int)m.WParam) {
282-
case SysMenuAboutId:
285+
case MF_SYS_MENU_ABOUT_ID:
283286
var asm = GetType().Assembly;
284287
MessageBox.Show($"{Updater.ApplicationTitle} {asm.GetName().Version.ToString(3)} {(Environment.Is64BitProcess ? "x64" : "x32")}\nWritten by Sergiy Egoshyn (egoshin.sergey@gmail.com)", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
285288
break;
286-
case SysMenuCheckUpdates:
289+
case MF_SYS_MENU_CHECK_UPDATES:
287290
Updater.CheckForUpdates(false);
288291
break;
289292
}
@@ -470,31 +473,19 @@ private void LoadList(List<MsUpdate> list) {
470473

471474
updateView.Items.Clear();
472475
List<ListViewItem> items = [];
473-
for (var i = 0; i < list.Count; i++) {
474-
var update = list[i];
476+
foreach (var update in list) {
475477
var state = "";
476478
switch (update.State) {
477479
case MsUpdate.UpdateState.History:
478-
switch ((OperationResultCode)update.ResultCode) {
479-
case OperationResultCode.orcNotStarted:
480-
state = "Not Started";
481-
break;
482-
case OperationResultCode.orcInProgress:
483-
state = "In Progress";
484-
break;
485-
case OperationResultCode.orcSucceeded:
486-
state = "Succeeded";
487-
break;
488-
case OperationResultCode.orcSucceededWithErrors:
489-
state = "Succeeded with Errors";
490-
break;
491-
case OperationResultCode.orcFailed:
492-
state = "Failed";
493-
break;
494-
case OperationResultCode.orcAborted:
495-
state = "Aborted";
496-
break;
497-
}
480+
state = (OperationResultCode) update.ResultCode switch {
481+
OperationResultCode.orcNotStarted => "Not Started",
482+
OperationResultCode.orcInProgress => "In Progress",
483+
OperationResultCode.orcSucceeded => "Succeeded",
484+
OperationResultCode.orcSucceededWithErrors => "Succeeded with Errors",
485+
OperationResultCode.orcFailed => "Failed",
486+
OperationResultCode.orcAborted => "Aborted",
487+
_ => state
488+
};
498489

499490
state += " (0x" + string.Format("{0:X8}", update.HResult) + ")";
500491
break;
@@ -534,11 +525,11 @@ private void LoadList(List<MsUpdate> list) {
534525

535526
string[] strings = [
536527
update.Title,
537-
update.Category,
538-
currentList == UpdateLists.UpdateHistory ? update.ApplicationId : update.Kb,
539-
update.Date.ToString(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern),
540-
FileOps.FormatSize(update.Size),
541-
state
528+
update.Category,
529+
currentList == UpdateLists.UpdateHistory ? update.ApplicationId : update.Kb,
530+
update.Date.ToString(CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern),
531+
FileOps.FormatSize(update.Size),
532+
state
542533
];
543534

544535
if (mSearchFilter != null) {
@@ -592,7 +583,7 @@ private void LoadList(List<MsUpdate> list) {
592583
//updateView.SetGroupState(ListViewGroupState.Collapsible);
593584
}
594585

595-
public List<MsUpdate> GetUpdates() {
586+
private List<MsUpdate> GetUpdates() {
596587
List<MsUpdate> updates = [];
597588
foreach (ListViewItem item in updateView.CheckedItems)
598589
updates.Add((MsUpdate)item.Tag);
@@ -726,10 +717,10 @@ private void menuWuAu_Click(object sender, EventArgs e) {
726717

727718
private void menuRefresh_Click(object sender, EventArgs e) {
728719
var menuHandle = GetSystemMenu(Handle, false); // Note: to restore default set true
729-
RemoveMenu(menuHandle, 6, MfByposition);
720+
RemoveMenu(menuHandle, 6, MF_BY_POSITION);
730721
mToolsMenu.MenuItems.Clear();
731722
BuildToolsMenu();
732-
InsertMenu(menuHandle, 6, MfByposition | MfPopup, (int)mToolsMenu.Handle, "&Tools");
723+
InsertMenu(menuHandle, 6, MF_BY_POSITION | MF_POPUP, (int)mToolsMenu.Handle, "&Tools");
733724
}
734725

735726
private void btnWinUpd_CheckedChanged(object sender, EventArgs e) {
@@ -753,11 +744,9 @@ private void btnHistory_CheckedChanged(object sender, EventArgs e) {
753744
private void btnSearch_Click(object sender, EventArgs e) {
754745
if (!agent.IsActive() || agent.IsBusy())
755746
return;
756-
var ret = WuAgent.RetCodes.Undefined;
757-
if (chkOffline.Checked)
758-
ret = agent.SearchForUpdates(chkDownload.Checked, chkOld.Checked);
759-
else
760-
ret = agent.SearchForUpdates(dlSource.Text, chkOld.Checked);
747+
var ret = chkOffline.Checked
748+
? agent.SearchForUpdates(chkDownload.Checked, chkOld.Checked)
749+
: agent.SearchForUpdates(dlSource.Text, chkOld.Checked);
761750
ShowResult(WuAgent.AgentOperation.CheckingUpdates, ret);
762751
}
763752

@@ -769,11 +758,9 @@ private void btnDownload_Click(object sender, EventArgs e) {
769758

770759
if (!agent.IsActive() || agent.IsBusy())
771760
return;
772-
var ret = WuAgent.RetCodes.Undefined;
773-
if (chkManual.Checked)
774-
ret = agent.DownloadUpdatesManually(GetUpdates());
775-
else
776-
ret = agent.DownloadUpdates(GetUpdates());
761+
var ret = chkManual.Checked
762+
? agent.DownloadUpdatesManually(GetUpdates())
763+
: agent.DownloadUpdates(GetUpdates());
777764
ShowResult(WuAgent.AgentOperation.DownloadingUpdates, ret);
778765
}
779766

@@ -785,11 +772,9 @@ private void btnInstall_Click(object sender, EventArgs e) {
785772

786773
if (!agent.IsActive() || agent.IsBusy())
787774
return;
788-
var ret = WuAgent.RetCodes.Undefined;
789-
if (chkManual.Checked)
790-
ret = agent.DownloadUpdatesManually(GetUpdates(), true);
791-
else
792-
ret = agent.DownloadUpdates(GetUpdates(), true);
775+
var ret = chkManual.Checked
776+
? agent.DownloadUpdatesManually(GetUpdates(), true)
777+
: agent.DownloadUpdates(GetUpdates(), true);
793778
ShowResult(WuAgent.AgentOperation.InstallingUpdates, ret);
794779
}
795780

@@ -801,8 +786,7 @@ private void btnUnInstall_Click(object sender, EventArgs e) {
801786

802787
if (!agent.IsActive() || agent.IsBusy())
803788
return;
804-
var ret = WuAgent.RetCodes.Undefined;
805-
ret = agent.UnInstallUpdatesManually(GetUpdates());
789+
var ret = agent.UnInstallUpdatesManually(GetUpdates());
806790
ShowResult(WuAgent.AgentOperation.RemovingUpdates, ret);
807791
}
808792

@@ -1169,34 +1153,31 @@ private void chkStore_CheckedChanged(object sender, EventArgs e) {
11691153

11701154
private void updateView_SelectedIndexChanged(object sender, EventArgs e) {
11711155
lblSupport.Visible = false;
1172-
if (updateView.SelectedItems.Count == 1) {
1173-
var update = (MsUpdate)updateView.SelectedItems[0].Tag;
1174-
if (update.Kb != null && update.Kb.Length > 2) {
1175-
lblSupport.Links[0].LinkData = "https://support.microsoft.com/help/" + update.Kb.Substring(2);
1176-
lblSupport.Links[0].Visited = false;
1177-
lblSupport.Visible = true;
1178-
toolTip.SetToolTip(lblSupport, lblSupport.Links[0].LinkData.ToString());
1179-
}
1180-
}
1156+
if (updateView.SelectedItems.Count != 1) return;
1157+
var update = (MsUpdate)updateView.SelectedItems[0].Tag;
1158+
if (update.Kb == null || update.Kb.Length <= 2) return;
1159+
lblSupport.Links[0].LinkData = "https://support.microsoft.com/help/" + update.Kb.Substring(2);
1160+
lblSupport.Links[0].Visited = false;
1161+
lblSupport.Visible = true;
1162+
toolTip.SetToolTip(lblSupport, lblSupport.Links[0].LinkData.ToString());
11811163
}
11821164

11831165
private void lblSupport_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
1184-
var target = e.Link.LinkData as string;
1185-
Process.Start(target);
1166+
if (e.Link.LinkData is string target) Process.Start(target);
11861167
}
11871168

1188-
public string GetConfig(string name, string def = "") {
1169+
private string GetConfig(string name, string def = "") {
11891170
return Program.IniReadValue("Options", name, def);
11901171
}
11911172

1192-
public void SetConfig(string name, string value) {
1173+
private void SetConfig(string name, string value) {
11931174
if (mSuspendUpdate)
11941175
return;
11951176
Program.IniWriteValue("Options", name, value);
11961177
}
11971178

11981179
[DllImport("User32.dll")]
1199-
public static extern int SetForegroundWindow(int hWnd);
1180+
private static extern int SetForegroundWindow(int hWnd);
12001181

12011182
private void notifyIcon_BalloonTipClicked(object sender, EventArgs e) {
12021183
if (!allowShowDisplay) {
@@ -1210,35 +1191,34 @@ private void notifyIcon_BalloonTipClicked(object sender, EventArgs e) {
12101191
}
12111192

12121193
private void updateView_ColumnClick(object sender, ColumnClickEventArgs e) {
1213-
if (updateView.ListViewItemSorter == null)
1214-
updateView.ListViewItemSorter = new ListViewItemComparer();
1194+
updateView.ListViewItemSorter ??= new ListViewItemComparer();
12151195
((ListViewItemComparer)updateView.ListViewItemSorter).Update(e.Column);
12161196
updateView.Sort();
12171197
}
12181198

12191199
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
1220-
if (keyData == (Keys.Control | Keys.F)) {
1221-
txtFilter.SelectAll();
1222-
txtFilter.Focus();
1223-
return true;
1224-
}
1200+
switch (keyData) {
1201+
case Keys.Control | Keys.F:
1202+
txtFilter.SelectAll();
1203+
txtFilter.Focus();
1204+
return true;
1205+
case Keys.Control | Keys.C: {
1206+
var info = "";
1207+
foreach (ListViewItem item in updateView.SelectedItems) {
1208+
if (info.Length != 0)
1209+
info += "\r\n";
1210+
info += item.Text;
1211+
for (var i = 1; i < item.SubItems.Count; i++)
1212+
info += "; " + item.SubItems[i].Text;
1213+
}
12251214

1226-
if (keyData == (Keys.Control | Keys.C)) {
1227-
var info = "";
1228-
foreach (ListViewItem item in updateView.SelectedItems) {
12291215
if (info.Length != 0)
1230-
info += "\r\n";
1231-
info += item.Text;
1232-
for (var i = 1; i < item.SubItems.Count; i++)
1233-
info += "; " + item.SubItems[i].Text;
1216+
Clipboard.SetText(info);
1217+
return true;
12341218
}
1235-
1236-
if (info.Length != 0)
1237-
Clipboard.SetText(info);
1238-
return true;
1219+
default:
1220+
return base.ProcessCmdKey(ref msg, keyData);
12391221
}
1240-
1241-
return base.ProcessCmdKey(ref msg, keyData);
12421222
}
12431223

12441224
private void btnSearchOff_Click(object sender, EventArgs e) {
@@ -1307,13 +1287,8 @@ private enum UpdateLists {
13071287

13081288
// Implements the manual sorting of items by columns.
13091289
private class ListViewItemComparer : IComparer {
1310-
private int col;
1311-
private int inv;
1312-
1313-
public ListViewItemComparer() {
1314-
col = 0;
1315-
inv = 1;
1316-
}
1290+
private int col = 0;
1291+
private int inv = 1;
13171292

13181293
public int Compare(object x, object y) {
13191294
if (col == 3) // date
@@ -1326,10 +1301,7 @@ public int Compare(object x, object y) {
13261301
}
13271302

13281303
public void Update(int column) {
1329-
if (col == column)
1330-
inv = -inv;
1331-
else
1332-
inv = 1;
1304+
inv = col == column ? -inv : 1;
13331305
col = column;
13341306
}
13351307
}

0 commit comments

Comments
 (0)