Skip to content

Commit 25bfda1

Browse files
committed
Fix DPI, Extra white windows
1 parent 0690235 commit 25bfda1

File tree

24 files changed

+486
-249
lines changed

24 files changed

+486
-249
lines changed

src/Avalonia.WebView2.Sample/Avalonia.WebView2.Sample.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
<AvaloniaXaml Include="**\*.xaml" />
1111
</ItemGroup>
1212

13-
<ItemGroup>
14-
<None Remove="WebView2Compat.xaml" />
15-
</ItemGroup>
16-
1713
<ItemGroup>
1814
<Compile Include="..\Avalonia.WebView2\Properties\AssemblyInfo.OS.cs">
1915
<Link>Properties\AssemblyInfo.OS.cs</Link>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationCore/SR.cs
5+
6+
#if !NETFRAMEWORK && WINDOWS
7+
8+
namespace MS.Internal.PresentationCore;
9+
10+
internal static class SR
11+
{
12+
public static string Get(string name) => name switch
13+
{
14+
SRID.CantShowMBServiceWithOwner => "Cannot show MessageBox Service with Owner.",
15+
_ => "",
16+
};
17+
}
18+
19+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#if !NETFRAMEWORK && WINDOWS
2+
3+
namespace MS.Internal.PresentationCore;
4+
5+
static class SRID
6+
{
7+
/// <summary>
8+
/// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationFramework/Resources/Strings.resx#L510
9+
/// </summary>
10+
public const string CantShowMBServiceWithOwner = "CantShowMBServiceWithOwner";
11+
}
12+
13+
#endif
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/ExternDll.cs
5+
6+
#if !NETFRAMEWORK && WINDOWS
7+
8+
namespace MS.Win32;
9+
10+
internal static class ExternDll
11+
{
12+
//public const string Activeds = "activeds.dll";
13+
//public const string Advapi32 = "advapi32.dll";
14+
//public const string Comctl32 = "comctl32.dll";
15+
//public const string Comdlg32 = "comdlg32.dll";
16+
//public const string DwmAPI = "dwmapi.dll";
17+
//public const string Gdi32 = "gdi32.dll";
18+
//public const string Gdiplus = "gdiplus.dll";
19+
//public const string Hhctrl = "hhctrl.ocx";
20+
//public const string Imm32 = "imm32.dll";
21+
//public const string Kernel32 = "kernel32.dll";
22+
//public const string Loadperf = "Loadperf.dll";
23+
//public const string Mqrt = "mqrt.dll";
24+
//public const string Mscoree = "mscoree.dll";
25+
//public const string MsDrm = "msdrm.dll";
26+
//public const string Mshwgst = "mshwgst.dll";
27+
//public const string Msi = "msi.dll";
28+
//public const string NaturalLanguage6 = "naturallanguage6.dll";
29+
//public const string Ntdll = "ntdll.dll";
30+
//public const string Ole32 = "ole32.dll";
31+
//public const string Oleacc = "oleacc.dll";
32+
//public const string Oleaut32 = "oleaut32.dll";
33+
//public const string Olepro32 = "olepro32.dll";
34+
//public const string Penimc = "PenIMC_cor3.dll";
35+
//public const string PresentationCore = "PresentationCore.dll";
36+
//public const string PresentationFramework = "PresentationFramework.dll";
37+
//public const string PresentationHostDll = "PresentationHost_cor3.dll";
38+
//public const string PresentationNativeDll = "PresentationNative_cor3.dll";
39+
//public const string Psapi = "psapi.dll";
40+
//public const string Shcore = "shcore.dll";
41+
//public const string Shell32 = "shell32.dll";
42+
//public const string Shfolder = "shfolder.dll";
43+
//public const string Urlmon = "urlmon.dll";
44+
public const string User32 = "user32.dll";
45+
//public const string Uxtheme = "uxtheme.dll";
46+
//public const string Version = "version.dll";
47+
//public const string Vsassert = "vsassert.dll";
48+
//public const string WindowsBase = "windowsbase.dll";
49+
//public const string Wininet = "wininet.dll";
50+
//public const string Winmm = "winmm.dll";
51+
//public const string Winspool = "winspool.drv";
52+
//public const string Wldp = "wldp.dll";
53+
//public const string WpfGfx = "WpfGfx_cor3.dll";
54+
//public const string WtsApi32 = "wtsapi32.dll";
55+
}
56+
57+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsCLR.cs
5+
6+
#if !NETFRAMEWORK && WINDOWS
7+
8+
using System.Runtime.InteropServices;
9+
10+
namespace MS.Win32;
11+
12+
internal static class UnsafeNativeMethods
13+
{
14+
[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
15+
public static extern IntPtr GetActiveWindow();
16+
17+
[DllImport(ExternDll.User32, SetLastError = true, CharSet = CharSet.Auto, BestFitMapping = false)]
18+
public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);
19+
}
20+
21+
#endif
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
// https://github.com/dotnet/wpf/blob/v6.0.6/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs
5+
6+
#if !NETFRAMEWORK && WINDOWS
7+
8+
using MS.Win32;
9+
using System.ComponentModel;
10+
using System.Runtime.InteropServices;
11+
using SR_ = MS.Internal.PresentationCore.SR;
12+
using SRID = MS.Internal.PresentationCore.SRID;
13+
14+
namespace System.Windows;
15+
16+
public static class MessageBox
17+
{
18+
const int IDOK = 1;
19+
const int IDCANCEL = 2;
20+
const int IDYES = 6;
21+
const int IDNO = 7;
22+
const int DEFAULT_BUTTON1 = 0x00000000;
23+
const int DEFAULT_BUTTON2 = 0x00000100;
24+
const int DEFAULT_BUTTON3 = 0x00000200;
25+
26+
static MessageBoxResult Win32ToMessageBoxResult(int value) => value switch
27+
{
28+
IDOK => MessageBoxResult.OK,
29+
IDCANCEL => MessageBoxResult.Cancel,
30+
IDYES => MessageBoxResult.Yes,
31+
IDNO => MessageBoxResult.No,
32+
_ => MessageBoxResult.No,
33+
};
34+
35+
public static MessageBoxResult Show(
36+
string messageBoxText,
37+
string caption,
38+
MessageBoxButton button,
39+
MessageBoxImage icon,
40+
MessageBoxResult defaultResult,
41+
MessageBoxOptions options) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, options);
42+
43+
public static MessageBoxResult Show(
44+
string messageBoxText,
45+
string caption,
46+
MessageBoxButton button,
47+
MessageBoxImage icon,
48+
MessageBoxResult defaultResult) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, defaultResult, 0);
49+
50+
public static MessageBoxResult Show(
51+
string messageBoxText,
52+
string caption,
53+
MessageBoxButton button,
54+
MessageBoxImage icon) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, icon, 0, 0);
55+
56+
public static MessageBoxResult Show(
57+
string messageBoxText,
58+
string caption,
59+
MessageBoxButton button) => ShowCore(IntPtr.Zero, messageBoxText, caption, button, MessageBoxImage.None, 0, 0);
60+
61+
public static MessageBoxResult Show(string messageBoxText, string caption) => ShowCore(IntPtr.Zero, messageBoxText, caption, MessageBoxButton.OK, MessageBoxImage.None, 0, 0);
62+
63+
public static MessageBoxResult Show(string messageBoxText) => ShowCore(IntPtr.Zero, messageBoxText, string.Empty, MessageBoxButton.OK, MessageBoxImage.None, 0, 0);
64+
65+
static int DefaultResultToButtonNumber(MessageBoxResult result, MessageBoxButton button)
66+
{
67+
if (result == 0) return DEFAULT_BUTTON1;
68+
69+
switch (button)
70+
{
71+
case MessageBoxButton.OK:
72+
return DEFAULT_BUTTON1;
73+
case MessageBoxButton.OKCancel:
74+
if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON2;
75+
return DEFAULT_BUTTON1;
76+
case MessageBoxButton.YesNo:
77+
if (result == MessageBoxResult.No) return DEFAULT_BUTTON2;
78+
return DEFAULT_BUTTON1;
79+
case MessageBoxButton.YesNoCancel:
80+
if (result == MessageBoxResult.No) return DEFAULT_BUTTON2;
81+
if (result == MessageBoxResult.Cancel) return DEFAULT_BUTTON3;
82+
return DEFAULT_BUTTON1;
83+
default:
84+
return DEFAULT_BUTTON1;
85+
}
86+
}
87+
88+
internal static MessageBoxResult ShowCore(
89+
IntPtr owner,
90+
string messageBoxText,
91+
string caption,
92+
MessageBoxButton button,
93+
MessageBoxImage icon,
94+
MessageBoxResult defaultResult,
95+
MessageBoxOptions options)
96+
{
97+
if (!IsValidMessageBoxButton(button))
98+
{
99+
throw new InvalidEnumArgumentException("button", (int)button, typeof(MessageBoxButton));
100+
}
101+
if (!IsValidMessageBoxImage(icon))
102+
{
103+
throw new InvalidEnumArgumentException("icon", (int)icon, typeof(MessageBoxImage));
104+
}
105+
if (!IsValidMessageBoxResult(defaultResult))
106+
{
107+
throw new InvalidEnumArgumentException("defaultResult", (int)defaultResult, typeof(MessageBoxResult));
108+
}
109+
if (!IsValidMessageBoxOptions(options))
110+
{
111+
throw new InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions));
112+
}
113+
114+
// UserInteractive??
115+
//
116+
/*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {
117+
throw new InvalidOperationException("UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)");
118+
}*/
119+
120+
if ((options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
121+
{
122+
if (owner != IntPtr.Zero)
123+
{
124+
throw new ArgumentException(SR_.Get(SRID.CantShowMBServiceWithOwner));
125+
}
126+
}
127+
else
128+
{
129+
if (owner == IntPtr.Zero)
130+
{
131+
owner = UnsafeNativeMethods.GetActiveWindow();
132+
}
133+
}
134+
135+
int style = (int)button | (int)icon | (int)DefaultResultToButtonNumber(defaultResult, button) | (int)options;
136+
137+
// modal dialog notification?
138+
//
139+
//Application.BeginModalMessageLoop();
140+
//MessageBoxResult result = Win32ToMessageBoxResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), messageBoxText, caption, style));
141+
MessageBoxResult result = Win32ToMessageBoxResult(UnsafeNativeMethods.MessageBox(new HandleRef(null, owner), messageBoxText, caption, style));
142+
// modal dialog notification?
143+
//
144+
//Application.EndModalMessageLoop();
145+
146+
return result;
147+
}
148+
149+
static bool IsValidMessageBoxButton(MessageBoxButton value) => value == MessageBoxButton.OK
150+
|| value == MessageBoxButton.OKCancel
151+
|| value == MessageBoxButton.YesNo
152+
|| value == MessageBoxButton.YesNoCancel;
153+
154+
static bool IsValidMessageBoxImage(MessageBoxImage value) => value == MessageBoxImage.Asterisk
155+
|| value == MessageBoxImage.Error
156+
|| value == MessageBoxImage.Exclamation
157+
|| value == MessageBoxImage.Hand
158+
|| value == MessageBoxImage.Information
159+
|| value == MessageBoxImage.None
160+
|| value == MessageBoxImage.Question
161+
|| value == MessageBoxImage.Stop
162+
|| value == MessageBoxImage.Warning;
163+
164+
static bool IsValidMessageBoxResult(MessageBoxResult value) => value == MessageBoxResult.Cancel
165+
|| value == MessageBoxResult.No
166+
|| value == MessageBoxResult.None
167+
|| value == MessageBoxResult.OK
168+
|| value == MessageBoxResult.Yes;
169+
170+
static bool IsValidMessageBoxOptions(MessageBoxOptions value)
171+
{
172+
int mask = ~((int)MessageBoxOptions.ServiceNotification |
173+
(int)MessageBoxOptions.DefaultDesktopOnly |
174+
(int)MessageBoxOptions.RightAlign |
175+
(int)MessageBoxOptions.RtlReading);
176+
177+
if (((int)value & mask) == 0)
178+
return true;
179+
return false;
180+
}
181+
}
182+
183+
public enum MessageBoxResult
184+
{
185+
None = 0,
186+
187+
OK = 1,
188+
189+
Cancel = 2,
190+
191+
Yes = 6,
192+
193+
No = 7,
194+
195+
// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxResult()
196+
}
197+
198+
[Flags]
199+
public enum MessageBoxOptions
200+
{
201+
None = 0x00000000,
202+
203+
ServiceNotification = 0x00200000,
204+
205+
DefaultDesktopOnly = 0x00020000,
206+
207+
RightAlign = 0x00080000,
208+
209+
RtlReading = 0x00100000,
210+
}
211+
212+
public enum MessageBoxImage
213+
{
214+
None = 0,
215+
216+
Hand = 0x00000010,
217+
218+
Question = 0x00000020,
219+
220+
Exclamation = 0x00000030,
221+
222+
Asterisk = 0x00000040,
223+
224+
Stop = Hand,
225+
226+
Error = Hand,
227+
228+
Warning = Exclamation,
229+
230+
Information = Asterisk,
231+
232+
// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxIcon()
233+
}
234+
235+
public enum MessageBoxButton
236+
{
237+
OK = 0x00000000,
238+
239+
OKCancel = 0x00000001,
240+
241+
YesNoCancel = 0x00000003,
242+
243+
YesNo = 0x00000004,
244+
245+
// NOTE: if you add or remove any values in this enum, be sure to update MessageBox.IsValidMessageBoxButton()
246+
}
247+
248+
#endif

src/Avalonia.WebView2.Sample/MainWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ static string GetTitle(string browserVersion, Architecture architecture = Unknow
3636
{
3737
if (architecture != Unknown)
3838
{
39-
return $"Microsoft Edge WebView2 {browserVersion} {architecture} for Avalonia";
39+
return $"Microsoft Edge WebView2 {browserVersion} {architecture} for Avalonia on {System.Environment.OSVersion.VersionString}";
4040
}
41-
return $"Microsoft Edge WebView2 {browserVersion} for Avalonia";
41+
return $"Microsoft Edge WebView2 {browserVersion} for Avalonia on {System.Environment.OSVersion.VersionString}";
4242
}
4343

4444
void SetTitle(string browserVersion, Architecture architecture = Unknown) => Title.Content = base.Title = GetTitle(browserVersion, architecture);

0 commit comments

Comments
 (0)