Skip to content

Commit 743dbe4

Browse files
Fruchtzwerg94ravibpatel
authored andcommitted
Added option to set WPF Window or Windows Form as owner of all dialogs initiated by AutoUpdater. (#617)
This fixes #584.
1 parent 4c34c0a commit 743dbe4

File tree

12 files changed

+76
-24
lines changed

12 files changed

+76
-24
lines changed

AutoUpdater.NET/AutoUpdater.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@
5151
<PackageReference Include="Resource.Embedder" Version="1.2.8" PrivateAssets="All"/>
5252
</ItemGroup>
5353
<ItemGroup>
54-
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1722.45"/>
54+
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.1774.30"/>
5555
</ItemGroup>
5656
</Project>

AutoUpdater.NET/AutoUpdater.cs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
using System;
22
using System.ComponentModel;
33
using System.Diagnostics;
4-
using System.Drawing;
54
using System.Globalization;
65
using System.IO;
76
using System.Net;
87
using System.Net.Cache;
98
using System.Reflection;
109
using System.Threading;
10+
using System.Windows;
1111
using System.Windows.Forms;
1212
using System.Xml;
1313
using System.Xml.Serialization;
1414
using AutoUpdaterDotNET.Properties;
15+
using Application = System.Windows.Forms.Application;
16+
using MessageBox = System.Windows.Forms.MessageBox;
17+
using Size = System.Drawing.Size;
1518
using Timer = System.Timers.Timer;
1619

1720
namespace AutoUpdaterDotNET;
@@ -86,6 +89,8 @@ public static class AutoUpdater
8689

8790
private static bool _isWinFormsApplication;
8891

92+
private static IWin32Window _owner;
93+
8994
private static Timer _remindLaterTimer;
9095

9196
internal static Uri BaseUri;
@@ -243,6 +248,20 @@ public static class AutoUpdater
243248
/// </summary>
244249
public static event ParseUpdateInfoHandler ParseUpdateInfoEvent;
245250

251+
/// <summary>
252+
/// Set the owner for all dialogs.
253+
/// </summary>
254+
/// <param name="obj">WPF Window or Windows Form object to be used as owner for all dialogs.</param>
255+
public static void SetOwner(object obj)
256+
{
257+
_owner = obj switch
258+
{
259+
Form form => form,
260+
Window window => new Wpf32Window(window),
261+
_ => _owner
262+
};
263+
}
264+
246265
/// <summary>
247266
/// Start checking for new version of application and display a dialog to the user if update is available.
248267
/// </summary>
@@ -503,7 +522,8 @@ private static bool StartUpdate(object result)
503522

504523
if (ReportErrors)
505524
{
506-
MessageBox.Show(Resources.UpdateUnavailableMessage,
525+
MessageBox.Show(_owner,
526+
Resources.UpdateUnavailableMessage,
507527
Resources.UpdateUnavailableCaption,
508528
MessageBoxButtons.OK, MessageBoxIcon.Information);
509529
}
@@ -525,15 +545,17 @@ private static void ShowError(Exception exception)
525545
{
526546
if (exception is WebException)
527547
{
528-
MessageBox.Show(
548+
MessageBox.Show(_owner,
529549
Resources.UpdateCheckFailedMessage,
530-
Resources.UpdateCheckFailedCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
550+
Resources.UpdateCheckFailedCaption,
551+
MessageBoxButtons.OK, MessageBoxIcon.Error);
531552
}
532553
else
533554
{
534-
MessageBox.Show(exception.Message,
535-
exception.GetType().ToString(), MessageBoxButtons.OK,
536-
MessageBoxIcon.Error);
555+
MessageBox.Show(_owner,
556+
exception.Message,
557+
exception.GetType().ToString(),
558+
MessageBoxButtons.OK, MessageBoxIcon.Error);
537559
}
538560
}
539561
}
@@ -662,7 +684,7 @@ public static bool DownloadUpdate(UpdateInfoEventArgs args)
662684

663685
try
664686
{
665-
return downloadDialog.ShowDialog().Equals(DialogResult.OK);
687+
return downloadDialog.ShowDialog(_owner).Equals(DialogResult.OK);
666688
}
667689
catch (TargetInvocationException)
668690
{
@@ -684,7 +706,7 @@ public static void ShowUpdateForm(UpdateInfoEventArgs args)
684706
updateForm.Size = UpdateFormSize.Value;
685707
}
686708

687-
if (updateForm.ShowDialog().Equals(DialogResult.OK))
709+
if (updateForm.ShowDialog(_owner).Equals(DialogResult.OK))
688710
{
689711
Exit();
690712
}

AutoUpdater.NET/DownloadUpdateDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ private void WebClientOnDownloadFileCompleted(object sender, AsyncCompletedEvent
260260
}
261261
catch (Exception e)
262262
{
263-
MessageBox.Show(e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
263+
MessageBox.Show(this, e.Message, e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
264264
_webClient = null;
265265
}
266266
finally

AutoUpdater.NET/UpdateForm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private void WebView_CoreWebView2InitializationCompleted(object sender,
9696
{
9797
if (AutoUpdater.ReportErrors)
9898
{
99-
MessageBox.Show(e.InitializationException.Message, e.InitializationException.GetType().ToString(),
99+
MessageBox.Show(this, e.InitializationException.Message, e.InitializationException.GetType().ToString(),
100100
MessageBoxButtons.OK, MessageBoxIcon.Error);
101101
}
102102

@@ -194,7 +194,7 @@ private void ButtonRemindLaterClick(object sender, EventArgs e)
194194
if (AutoUpdater.LetUserSelectRemindLater)
195195
{
196196
using var remindLaterForm = new RemindLaterForm();
197-
DialogResult dialogResult = remindLaterForm.ShowDialog();
197+
DialogResult dialogResult = remindLaterForm.ShowDialog(this);
198198

199199
switch (dialogResult)
200200
{

AutoUpdater.NET/Wpf32Window.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Interop;
4+
using IWin32Window = System.Windows.Forms.IWin32Window;
5+
6+
namespace AutoUpdaterDotNET;
7+
8+
internal class Wpf32Window : IWin32Window
9+
{
10+
public Wpf32Window(Window wpfWindow)
11+
{
12+
Handle = new WindowInteropHelper(wpfWindow).EnsureHandle();
13+
}
14+
15+
public IntPtr Handle { get; }
16+
}

AutoUpdater.NET/build/Autoupdater.NET.Official.nuspec

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
33
<metadata>
44
<id>Autoupdater.NET.Official</id>
5-
<version>1.8.1.0</version>
5+
<version>1.8.2.0</version>
66
<title>AutoUpdater.NET</title>
77
<authors>rbsoft</authors>
88
<requireLicenseAcceptance>false</requireLicenseAcceptance>
@@ -17,16 +17,16 @@
1717
<tags>autoupdate updater c# vb wpf winforms</tags>
1818
<dependencies>
1919
<group targetFramework=".NETFramework4.5">
20-
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
20+
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
2121
</group>
2222
<group targetFramework=".NETCoreApp3.1">
23-
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
23+
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
2424
</group>
2525
<group targetFramework="net5.0-windows7.0">
26-
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
26+
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
2727
</group>
2828
<group targetFramework="net6.0-windows7.0">
29-
<dependency id="Microsoft.Web.WebView2" version="1.0.1722.45" exclude="Build,Analyzers"/>
29+
<dependency id="Microsoft.Web.WebView2" version="1.0.1774.30" exclude="Build,Analyzers"/>
3030
</group>
3131
</dependencies>
3232
<frameworkReferences>

AutoUpdaterTest/AutoUpdaterTest.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<ProjectReference Include="..\AutoUpdater.NET\AutoUpdater.NET.csproj" />
13+
<ProjectReference Include="..\AutoUpdater.NET\AutoUpdater.NET.csproj"/>
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
17+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
1818
</ItemGroup>
1919

2020
</Project>

AutoUpdaterTest/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
55
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
66
mc:Ignorable="d"
7+
Topmost="True"
78
Title="MainWindow" Height="200" Width="400" ResizeMode="NoResize">
89
<Grid Row="2" HorizontalAlignment="Center" VerticalAlignment="Center">
910
<Grid.RowDefinitions>

AutoUpdaterTest/MainWindow.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ private void ButtonCheckForUpdate_Click(object sender, RoutedEventArgs e)
191191
// Uncomment following line if you want to execute different executable after the update. This only works when you use zip file as an update file.
192192
AutoUpdater.ExecutablePath = "bin/AutoUpdaterTest.exe";
193193

194+
// Uncomment following line to set this window as owner of the all dialogs initiated by AutoUpdater. It is necessary to do this if TopMost is set to true in your form or window.
195+
AutoUpdater.SetOwner(this);
196+
194197
AutoUpdater.Start("https://rbsoft.org/updates/AutoUpdaterTest.xml");
195198
}
196199
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ You can specify the size of the update form by using below code.
325325
AutoUpdater.UpdateFormSize = new System.Drawing.Size(800, 600);
326326
````
327327

328+
### Set the owner Form / Window
329+
330+
To ensure the dialogs showed by the auto updater are visible and always focussed correctly related to an application
331+
Form or Window, it may be necessary to set an owner. You can assign a Form or WPF Window as the owner by following the
332+
example below.
333+
334+
````csharp
335+
AutoUpdater.SetOwner(yourMainFormOrWpfWindow);
336+
````
337+
328338
### Change storage method of Remind Later and Skip options
329339

330340
You can change how AutoUpdater.NET saves the Remind Later and Skip values by assigning the PersistenceProvider. If you

0 commit comments

Comments
 (0)