Skip to content

Commit b722630

Browse files
committed
add system menu items: "About" & "Check for new version"
1 parent 06e4a41 commit b722630

5 files changed

Lines changed: 252 additions & 26 deletions

File tree

winUpdateMiniTool/MainForm.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Runtime.InteropServices;
1010
using System.Windows.Forms;
11+
using sergiye.Common;
1112
using winUpdateMiniTool.Common;
1213
using winUpdateMiniTool.Properties;
1314
using WUApiLib;
@@ -30,9 +31,10 @@ public partial class MainForm : Form {
3031
public const int MF_UNCHECKED = 0x00000000;
3132
private const int MfByposition = 0x400;
3233
public const int MF_BYCOMMAND = 0x000;
33-
//private const int WmSyscommand = 0x112;
34+
private const int WmSyscommand = 0x112;
3435
//public const Int32 MF_REMOVE = 0x1000;
35-
// private const int MymenuAbout = 1000;
36+
private const int SysMenuCheckUpdates = 1000;
37+
private const int SysMenuAboutId = 1001;
3638
private static Timer mTimer;
3739
private readonly WuAgent agent;
3840
private readonly int idleDelay;
@@ -59,15 +61,15 @@ public MainForm() {
5961

6062
Icon = Icon.ExtractAssociatedIcon(typeof(MainForm).Assembly.Location);
6163
notifyIcon.Icon = Icon;
62-
notifyIcon.Text = Program.APP_TITLE;
64+
notifyIcon.Text = Updater.ApplicationTitle;
6365

6466
if (Program.TestArg("-tray")) {
6567
allowShowDisplay = false;
6668
notifyIcon.Visible = true;
6769
}
6870

6971
if (!MiscFunc.IsRunningAsUwp())
70-
Text = $"{Program.APP_TITLE} v{Program.MVersion}";
72+
Text = $"{Updater.ApplicationTitle} v{Program.MVersion}";
7173

7274
btnWinUpd.Text = string.Format("Windows Update ({0})", 0);
7375
btnInstalled.Text = string.Format("Installed Updates ({0})", 0);
@@ -102,7 +104,7 @@ public MainForm() {
102104
agent.Finished += OnFinished;
103105

104106
if (!agent.IsActive())
105-
if (MessageBox.Show("Windows Update Service is not available, try to start it?", Program.APP_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
107+
if (MessageBox.Show("Windows Update Service is not available, try to start it?", Updater.ApplicationTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {
106108
agent.EnableWuAuServ();
107109
agent.Init();
108110
}
@@ -245,7 +247,8 @@ public MainForm() {
245247
var menuHandle = GetSystemMenu(Handle, false); // Note: to restore default set true
246248
InsertMenu(menuHandle, 5, MfByposition | MfSeparator, 0, string.Empty); // <-- Add a menu separator
247249
InsertMenu(menuHandle, 6, MfByposition | MfPopup, (int)mToolsMenu.Handle, mToolsMenu.Text);
248-
// InsertMenu(menuHandle, 7, MfByposition, MymenuAbout, "About");
250+
InsertMenu(menuHandle, 7, MfByposition, SysMenuCheckUpdates, "Check for new version");
251+
InsertMenu(menuHandle, 8, MfByposition, SysMenuAboutId, "&About…");
249252

250253
UpdateCounts();
251254
SwitchList(UpdateLists.UpdateHistory);
@@ -259,8 +262,38 @@ public MainForm() {
259262

260263
Program.Ipc.PipeMessage += PipesMessageHandler;
261264
Program.Ipc.Listen();
265+
266+
//will display prompt only if update available & when main form displayed
267+
var timer = new Timer();
268+
timer.Interval = 1000;
269+
timer.Tick += (_, _) => {
270+
timer.Enabled = false;
271+
timer.Enabled = !Updater.CheckForUpdates(true);
272+
};
273+
timer.Enabled = true;
274+
262275
}
263276

277+
protected override void WndProc(ref Message m) {
278+
279+
base.WndProc(ref m);
280+
if (m.Msg == WmSyscommand) {
281+
switch ((int)m.WParam) {
282+
case SysMenuAboutId:
283+
var asm = GetType().Assembly;
284+
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);
285+
break;
286+
case SysMenuCheckUpdates:
287+
Updater.CheckForUpdates(false);
288+
break;
289+
}
290+
// } else if (m.Msg == Common.WM_SHOWME) {
291+
// if (WindowState == FormWindowState.Minimized)
292+
// WindowState = FormWindowState.Normal;
293+
// Activate();
294+
}
295+
}
296+
264297
[DllImport("user32.dll")]
265298
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
266299

@@ -304,7 +337,7 @@ private void OnTimedEvent(object source, EventArgs e) {
304337
if (lastBalloon < DateTime.Now.AddHours(-4)) {
305338
lastBalloon = DateTime.Now;
306339
notifyIcon.ShowBalloonTip(int.MaxValue, "Please Check For Updates",
307-
$"{Program.APP_TITLE} couldn't check for updates for {daysDue} days, please check for updates manually and resolve possible issues", ToolTipIcon.Warning);
340+
$"{Updater.ApplicationTitle} couldn't check for updates for {daysDue} days, please check for updates manually and resolve possible issues", ToolTipIcon.Warning);
308341
}
309342
}
310343
}
@@ -313,7 +346,7 @@ private void OnTimedEvent(object source, EventArgs e) {
313346
if (lastBalloon < DateTime.Now.AddHours(-4)) {
314347
lastBalloon = DateTime.Now;
315348
notifyIcon.ShowBalloonTip(int.MaxValue, "New Updates found",
316-
string.Format("{0} has found {1} new updates, please review the updates and install them", Program.APP_TITLE,
349+
string.Format("{0} has found {1} new updates, please review the updates and install them", Updater.ApplicationTitle,
317350
string.Join(Environment.NewLine, agent.MPendingUpdates.Select(x => $"- {x.Title}"))),
318351
ToolTipIcon.Info);
319352
}
@@ -670,7 +703,7 @@ private void menuExec_Click(object sender, EventArgs e, string exec, string dir,
670703
var startInfo = Program.PrepExec(exec, silent);
671704
startInfo.WorkingDirectory = dir;
672705
if (!Program.DoExec(startInfo))
673-
MessageBox.Show("Failed to start tool", Program.APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
706+
MessageBox.Show("Failed to start tool", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
674707
}
675708

676709
private void menuExit_Click(object sender, EventArgs e) {
@@ -730,7 +763,7 @@ private void btnSearch_Click(object sender, EventArgs e) {
730763

731764
private void btnDownload_Click(object sender, EventArgs e) {
732765
if (!chkManual.Checked && !MiscFunc.IsAdministrator()) {
733-
MessageBox.Show("Administrator privileges are required in order to download updates using windows update services. Use 'Manual' download instead.", Program. APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
766+
MessageBox.Show("Administrator privileges are required in order to download updates using windows update services. Use 'Manual' download instead.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
734767
return;
735768
}
736769

@@ -746,7 +779,7 @@ private void btnDownload_Click(object sender, EventArgs e) {
746779

747780
private void btnInstall_Click(object sender, EventArgs e) {
748781
if (!MiscFunc.IsAdministrator()) {
749-
MessageBox.Show("Administrator privileges are required in order to install updates.", Program.APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
782+
MessageBox.Show("Administrator privileges are required in order to install updates.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
750783
return;
751784
}
752785

@@ -762,7 +795,7 @@ private void btnInstall_Click(object sender, EventArgs e) {
762795

763796
private void btnUnInstall_Click(object sender, EventArgs e) {
764797
if (!MiscFunc.IsAdministrator()) {
765-
MessageBox.Show("Administrator privileges are required in order to remove updates.", Program.APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
798+
MessageBox.Show("Administrator privileges are required in order to remove updates.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
766799
return;
767800
}
768801

@@ -879,27 +912,27 @@ private void OnFinished(object sender, WuAgent.FinishedArgs args) {
879912
private void ShowResult(WuAgent.AgentOperation op, WuAgent.RetCodes ret, bool reboot = false) {
880913
if (op == WuAgent.AgentOperation.DownloadingUpdates && chkManual.Checked) {
881914
if (ret == WuAgent.RetCodes.Success) {
882-
MessageBox.Show($"Updates downloaded to {agent.DlPath}, ready to be installed by the user.", Program.APP_TITLE, MessageBoxButtons.OK,
915+
MessageBox.Show($"Updates downloaded to {agent.DlPath}, ready to be installed by the user.", Updater.ApplicationTitle, MessageBoxButtons.OK,
883916
MessageBoxIcon.Information);
884917
return;
885918
}
886919

887920
if (ret == WuAgent.RetCodes.DownloadFailed) {
888-
MessageBox.Show($"Updates downloaded to {agent.DlPath}, some updates failed to download.", Program.APP_TITLE, MessageBoxButtons.OK,
921+
MessageBox.Show($"Updates downloaded to {agent.DlPath}, some updates failed to download.", Updater.ApplicationTitle, MessageBoxButtons.OK,
889922
MessageBoxIcon.Exclamation);
890923
return;
891924
}
892925
}
893926

894927
if (op == WuAgent.AgentOperation.InstallingUpdates && reboot) {
895928
if (ret == WuAgent.RetCodes.Success) {
896-
MessageBox.Show("Updates successfully installed, however, a reboot is required.", Program.APP_TITLE, MessageBoxButtons.OK,
929+
MessageBox.Show("Updates successfully installed, however, a reboot is required.", Updater.ApplicationTitle, MessageBoxButtons.OK,
897930
MessageBoxIcon.Information);
898931
return;
899932
}
900933

901934
if (ret == WuAgent.RetCodes.DownloadFailed) {
902-
MessageBox.Show("Installation of some Updates has failed, also a reboot is required.", Program.APP_TITLE, MessageBoxButtons.OK,
935+
MessageBox.Show("Installation of some Updates has failed, also a reboot is required.", Updater.ApplicationTitle, MessageBoxButtons.OK,
903936
MessageBoxIcon.Exclamation);
904937
return;
905938
}
@@ -936,7 +969,7 @@ private void ShowResult(WuAgent.AgentOperation op, WuAgent.RetCodes ret, bool re
936969
var action = GetOpStr(op);
937970

938971
resultShown = true;
939-
MessageBox.Show($"{action} failed: {status}.", Program.APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Error);
972+
MessageBox.Show($"{action} failed: {status}.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
940973
resultShown = false;
941974
}
942975

@@ -1008,7 +1041,7 @@ private void radGPO_CheckedChanged(object sender, EventArgs e) {
10081041
var test = Gpo.GetDisableAu();
10091042
Gpo.DisableAu(true);
10101043
if (!test)
1011-
MessageBox.Show("For the new configuration to fully take effect a reboot is required.", Program.APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
1044+
MessageBox.Show("For the new configuration to fully take effect a reboot is required.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
10121045
}
10131046

10141047
Gpo.ConfigAu(Gpo.AuOptions.Disabled);
@@ -1037,7 +1070,7 @@ private void chkBlockMS_CheckedChanged(object sender, EventArgs e) {
10371070
}
10381071
else {
10391072
if (!chkDisableAU.Checked)
1040-
switch (MessageBox.Show("Your version of Windows does not respect the standard GPO's, to keep automatic Windows updates blocked, update facilitation services must be disabled.", Program.APP_TITLE, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning)) {
1073+
switch (MessageBox.Show("Your version of Windows does not respect the standard GPO's, to keep automatic Windows updates blocked, update facilitation services must be disabled.", Updater.ApplicationTitle, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning)) {
10411074
case DialogResult.Yes:
10421075
chkDisableAU.Checked = true; // Note: this triggers chkDisableAU_CheckedChanged
10431076
break;

winUpdateMiniTool/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
using System.Threading;
88
using System.Windows.Forms;
99
using Microsoft.Win32;
10+
using sergiye.Common;
1011
using TaskScheduler;
1112
using winUpdateMiniTool.Common;
1213

1314
namespace winUpdateMiniTool;
1415

1516
internal static class Program {
1617
private const string AppTaskName = "wumtNoUAC";
17-
public const string APP_TITLE = "Windows Update Mini Tool";
1818
private static string[] args;
1919
private static bool mConsole;
2020
public static string MVersion;
@@ -43,15 +43,15 @@ private static void Main(string[] mainArgs) {
4343
}
4444

4545
if (TestArg("-dbg_wait"))
46-
MessageBox.Show("Waiting for debugger. (press ok when attached)", APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
46+
MessageBox.Show("Waiting for debugger. (press ok when attached)", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
4747

4848
Console.WriteLine(@"Starting...");
4949

5050
var assembly = typeof(Program).Assembly;
5151
WrkPath = appPath = Path.GetDirectoryName(assembly.Location);
5252
MVersion = assembly.GetName().Version.ToString(3);
5353

54-
AppLog.Line("{0}, Version v{1}", APP_TITLE, MVersion);
54+
AppLog.Line("{0}, Version v{1}", Updater.ApplicationTitle, MVersion);
5555
AppLog.Line("This Tool is Open Source under the GNU General Public License, Version 3\r\n");
5656

5757
Ipc = new PipeIpc("wumt_pipe");
@@ -62,7 +62,7 @@ private static void Main(string[] mainArgs) {
6262
client.Send("show");
6363
var ret = client.Read(1000);
6464
if (!ret.Equals("ok", StringComparison.CurrentCultureIgnoreCase))
65-
MessageBox.Show("Application is already running.", APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
65+
MessageBox.Show("Application is already running.", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
6666
return;
6767
}
6868

@@ -106,7 +106,7 @@ private static void Main(string[] mainArgs) {
106106
Directory.CreateDirectory(WrkPath);
107107
}
108108
catch {
109-
MessageBox.Show($"Can't write to working directory: {WrkPath}", APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Warning);
109+
MessageBox.Show($"Can't write to working directory: {WrkPath}", Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
110110
}
111111
}
112112

@@ -437,7 +437,7 @@ private static void ShowHelp() {
437437
"-help\t\tShow this help message"
438438
];
439439
if (!mConsole) {
440-
MessageBox.Show(message + string.Join("\r\n", help), APP_TITLE, MessageBoxButtons.OK, MessageBoxIcon.Information);
440+
MessageBox.Show(message + string.Join("\r\n", help), Updater.ApplicationTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
441441
}
442442
else {
443443
Console.WriteLine(message);

winUpdateMiniTool/WuAgent.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Linq;
66
using System.ServiceProcess;
77
using System.Windows.Threading;
8+
using sergiye.Common;
89
using winUpdateMiniTool.Common;
910
using WUApiLib;
1011
using StringCollection = System.Collections.Specialized.StringCollection;
@@ -86,7 +87,7 @@ public WuAgent() {
8687
AppLog.Line("Windows Update Agent Version: {0}", currentVersion);
8788

8889
mUpdateSession = new UpdateSession {
89-
ClientApplicationID = Program.APP_TITLE
90+
ClientApplicationID = Updater.ApplicationTitle
9091
};
9192
//mUpdateSession.UserLocale = 1033; // always show strings in englisch
9293

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.IO;
2+
using System.Web.Script.Serialization;
3+
4+
namespace sergiye.Common {
5+
6+
internal static class SerializeHelper {
7+
8+
internal static string ToJson(this object value) {
9+
return new JavaScriptSerializer().Serialize(value);
10+
}
11+
12+
internal static T FromJson<T>(this string json) {
13+
return new JavaScriptSerializer().Deserialize<T>(json);
14+
}
15+
16+
internal static void ToJsonFile(this object value, string filePath) {
17+
File.WriteAllText(filePath, value.ToJson());
18+
}
19+
20+
internal static T ReadJsonFile<T>(string fileName) where T : class {
21+
T result = null;
22+
if (File.Exists(fileName))
23+
result = File.ReadAllText(fileName).FromJson<T>();
24+
return result;
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)