Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
166 lines (145 loc) · 4.91 KB
/
Copy pathProgram.cs
File metadata and controls
166 lines (145 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.HiDpi;
using Windows.Win32.UI.WindowsAndMessaging;
using static Windows.Win32.PInvoke;
internal static class Program
{
private const string WindowClassName = "WebView2Browser.NativeAot";
private const string IconFileName = "WebView2Browser.ico";
private static BrowserWindow? s_window;
[STAThread]
private static int Main()
{
try
{
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
using FreeLibrarySafeHandle instanceHandle = GetModuleHandle();
if (instanceHandle.IsInvalid)
{
throw new InvalidOperationException("GetModuleHandle failed.");
}
HINSTANCE instance = (HINSTANCE)instanceHandle.DangerousGetHandle();
RegisterWindowClass(instance);
if (!TryLaunchWindow(instanceHandle))
{
return 1;
}
_ = s_window!.InitializeWebViewsAsync();
MSG message;
while (GetMessage(out message, HWND.Null, 0, 0))
{
TranslateMessage(in message);
DispatchMessage(in message);
}
return (int)message.wParam.Value;
}
catch (Exception ex)
{
LogFailure(ex.ToString());
ShowError(ex.Message);
return ex.HResult;
}
finally
{
s_window?.Dispose();
s_window = null;
}
}
private static unsafe void RegisterWindowClass(HINSTANCE instance)
{
HICON appIcon = LoadApplicationIcon();
fixed (char* className = WindowClassName)
{
WNDCLASSEXW windowClass = new()
{
cbSize = (uint)Unsafe.SizeOf<WNDCLASSEXW>(),
style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW,
lpfnWndProc = &WndProc,
hInstance = instance,
hIcon = appIcon,
hCursor = LoadCursor(HINSTANCE.Null, IDC_ARROW),
hbrBackground = (HBRUSH)new IntPtr((int)SYS_COLOR_INDEX.COLOR_WINDOW + 1),
lpszClassName = className,
hIconSm = appIcon,
};
ushort atom = RegisterClassEx(in windowClass);
if (atom == 0)
{
throw new InvalidOperationException("RegisterClassEx failed.");
}
}
}
private static unsafe HICON LoadApplicationIcon()
{
string iconPath = Path.Combine(AppContext.BaseDirectory, IconFileName);
if (File.Exists(iconPath))
{
fixed (char* iconPathPointer = iconPath)
{
HANDLE iconHandle = LoadImage(
HINSTANCE.Null,
iconPathPointer,
GDI_IMAGE_TYPE.IMAGE_ICON,
0,
0,
IMAGE_FLAGS.LR_LOADFROMFILE | IMAGE_FLAGS.LR_DEFAULTSIZE | IMAGE_FLAGS.LR_SHARED);
if (!iconHandle.IsNull)
{
return (HICON)(IntPtr)iconHandle;
}
}
}
return LoadIcon(HINSTANCE.Null, IDI_APPLICATION);
}
private static bool TryLaunchWindow(SafeHandle instanceHandle)
{
while (true)
{
s_window = new BrowserWindow(instanceHandle, WindowClassName);
if (s_window.Create(SHOW_WINDOW_CMD.SW_SHOW))
{
return true;
}
s_window.Dispose();
s_window = null;
MESSAGEBOX_RESULT result = MessageBox(
HWND.Null,
"Could not launch the browser.",
"Error",
MESSAGEBOX_STYLE.MB_RETRYCANCEL | MESSAGEBOX_STYLE.MB_ICONERROR);
if (result != MESSAGEBOX_RESULT.IDRETRY)
{
return false;
}
}
}
private static void ShowError(string message)
{
MessageBox(
HWND.Null,
message,
BrowserWindow.WindowTitle,
MESSAGEBOX_STYLE.MB_OK | MESSAGEBOX_STYLE.MB_ICONERROR);
}
internal static void LogFailure(string message)
{
try
{
string path = Path.Combine(AppContext.BaseDirectory, "WebView2Browser.log");
File.AppendAllText(path, $"{DateTimeOffset.Now:O} {message}{Environment.NewLine}");
}
catch
{
}
}
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static LRESULT WndProc(HWND hwnd, uint message, WPARAM wParam, LPARAM lParam)
{
return s_window?.HandleWindowMessage(hwnd, message, wParam, lParam)
?? DefWindowProc(hwnd, message, wParam, lParam);
}
}