-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.xaml.cpp
More file actions
365 lines (303 loc) · 9.6 KB
/
App.xaml.cpp
File metadata and controls
365 lines (303 loc) · 9.6 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "pch.h"
#include "App.xaml.h"
#include "MainWindow.xaml.h"
bool is_light_theme();
std::wstring ToOpenFile;
using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
WNDPROC wProc = 0;
HICON hIcon1 = 0;
std::map<HWND, winrt::Windows::Foundation::IInspectable> windows;
winrt::Windows::Foundation::IInspectable PageFromHWND(HWND hh)
{
auto wi = windows[hh];
return PageFromWindow(wi);
}
winrt::Windows::Foundation::IInspectable PageFromWindow(winrt::Windows::Foundation::IInspectable wi)
{
auto the_window = wi.try_as<winrt::VisualWinUI3::MainWindow>();
if (!the_window)
return nullptr;
// Find the MainPage for it
auto tnv = the_window.Content().try_as<winrt::Microsoft::UI::Xaml::Controls::NavigationView>();
if (!tnv)
return nullptr;
auto fr = tnv.FindName(L"contentFrame").as<winrt::Microsoft::UI::Xaml::Controls::Frame>();
if (!fr)
return nullptr;
auto main_page = fr.Content().as<winrt::VisualWinUI3::MainPage>();
if (!main_page)
return nullptr;
return main_page;
}
LRESULT CALLBACK cbx(HWND hh, UINT mm, WPARAM ww, LPARAM ll)
{
if (mm == WM_CLOSE)
{
// find the window
auto wi = windows[hh];
auto main_page = PageFromWindow(wi).as<winrt::VisualWinUI3::MainPage>();
if (!main_page)
return CallWindowProc(wProc, hh, mm, ww, ll);
if (main_page.Dirty())
{
main_page.AskSaveChanges();
/* auto res = MessageBox((HWND)hh, s(34), ttitle, MB_YESNOCANCEL);
if (res == IDYES)
{
main_page.Save();
}
else if (res == IDCANCEL)
{
return 0;
}
*/
return 0;
}
windows.erase(hh);
if (windows.size() == 0)
ExitProcess(0);
}
return CallWindowProc(wProc, hh, mm, ww, ll);
}
void PostRefresh()
{
// Apply
for (auto& wi : windows)
{
auto the_window = wi.second.as<winrt::VisualWinUI3::MainWindow>();
// Find the MainPage for it
auto tnv = the_window.Content().as<winrt::Microsoft::UI::Xaml::Controls::NavigationView>();
if (!tnv)
continue;
auto fr = tnv.FindName(L"contentFrame").as<winrt::Microsoft::UI::Xaml::Controls::Frame>();
if (!fr)
continue;
auto main_page = fr.Content().as<winrt::VisualWinUI3::MainPage>();
if (!main_page)
continue;
main_page.Refresh();
}
}
void PostThemeChange()
{
int Theme = SettingsX->GetRootElement().vv("Theme").GetValueInt();
if (Theme == 0)
{
if (is_light_theme())
Theme = 1;
else
Theme = 2;
}
auto et = winrt::Microsoft::UI::Xaml::ElementTheme::Dark;
if (Theme == 1)
et = winrt::Microsoft::UI::Xaml::ElementTheme::Light;
for (auto& w : windows)
{
BOOL value = true;
if (Theme == 1)
value = false;
DwmSetWindowAttribute(w.first, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
auto w2 = w.second.as<::VisualWinUI3::MainWindow>();
// and any navigation page
auto topnv = w2.Content().try_as<FrameworkElement>();
if (topnv)
{
topnv.RequestedTheme(et);
// and the frame
auto fr = topnv.FindName(L"contentFrame");
if (fr)
{
auto fr2 = fr.try_as<Frame>();
if (fr2)
{
fr2.RequestedTheme(et);
auto c = fr2.Content();
if (c)
{
// and the content
auto c2 = c.try_as<FrameworkElement>();
if (c2)
c2.RequestedTheme(et);
}
}
}
topnv.UpdateLayout();
}
}
}
#ifndef _DEBUG
#define NEW_WINDOW_APP
#endif
winrt::VisualWinUI3::MainWindow CreateWi()
{
#ifdef NEW_WINDOW_APP
if (windows.size() > 0)
{
// spawn new process
STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi = {};
wchar_t a[1000] = {};
GetModuleFileName(0, a, 1000);
if (!ToOpenFile.empty())
swprintf_s(a + wcslen(a), 1000 - wcslen(a), L" \"%s\"", ToOpenFile.c_str());
ToOpenFile.clear();
if (CreateProcess(0, a, 0, 0, false, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
return nullptr;
}
#endif
winrt::VisualWinUI3::MainWindow j;
j.Activate();
static int One = 0;
auto n = j.as<::IWindowNative>();
if (n)
{
HWND hh;
n->get_WindowHandle(&hh);
if (hh)
{
j.wnd((int64_t)hh);
j.ExtendsContentIntoTitleBar(true);
windows[hh] = j;
hIcon1 = LoadIcon(GetModuleHandle(0), L"ICON_1");
wProc = (WNDPROC)GetWindowLongPtr(hh, GWLP_WNDPROC);
SetWindowLongPtr(hh, GWLP_WNDPROC, (LONG_PTR)cbx);
SetWindowText(hh, ttitle);
if (One == 0)
ShowWindow(hh, SW_SHOWMAXIMIZED);
One = 1;
#define GCL_HICONSM (-34)
#define GCL_HICON (-14)
SetClassLongPtr(hh, GCL_HICONSM, (LONG_PTR)hIcon1);
SetClassLongPtr(hh, GCL_HICON, (LONG_PTR)hIcon1);
if (1)
{
BOOL value = true;
if (SettingsX)
{
int Theme = SettingsX->GetRootElement().vv("Theme").GetValueInt();
if (Theme == 0)
{
if (is_light_theme())
Theme = 1;
else
Theme = 2;
}
if (Theme == 1)
value = false;
::DwmSetWindowAttribute(hh, DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
}
}
}
}
return j;
}
namespace winrt::VisualWinUI3::implementation
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
App::App()
{
// Xaml objects should not call InitializeComponent during construction.
// See https://github.com/microsoft/cppwinrt/tree/master/nuget#initializecomponent
if (SettingsX)
{
int Theme = SettingsX->GetRootElement().vv("Theme").GetValueInt();
if (Theme == 1)
RequestedTheme(ApplicationTheme::Light);
if (Theme == 2)
RequestedTheme(ApplicationTheme::Dark);
}
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException([](IInspectable const&, UnhandledExceptionEventArgs const& e)
{
if (IsDebuggerPresent())
{
auto errorMessage = e.Message();
__debugbreak();
}
});
#endif
}
/// <summary>
/// Invoked when the application is launched.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
void App::OnLaunched([[maybe_unused]] LaunchActivatedEventArgs const& e)
{
if (__argc > 1)
{
if (std::filesystem::exists(__wargv[1]))
{
ToOpenFile = __wargv[1];
}
}
window = CreateWi();
}
}
// Helper
CComPtr<IWICBitmap> LoadWic(const wchar_t* o);
void SaveWic(IWICBitmapSource* src, const wchar_t* out);
extern CComPtr<IWICImagingFactory> wbfact;
void ChangeAssets(const wchar_t* src, const wchar_t* dir_dest)
{
// Wic Bitmap Loader
auto png = LoadWic(src);
for (const auto& entry : std::filesystem::directory_iterator(dir_dest))
{
auto png2 = LoadWic(entry.path().c_str());
if (!png2)
continue;
UINT wi = 0, he = 0;
png2->GetSize(&wi, &he);
CComPtr<IWICBitmapScaler> pScaler;
wbfact->CreateBitmapScaler(&pScaler);
pScaler->Initialize(png, wi, he, WICBitmapInterpolationModeHighQualityCubic);
png2 = 0;
DeleteFile(entry.path().c_str());
SaveWic(pScaler, entry.path().c_str());
}
}
int __stdcall wWinMain(HINSTANCE h, HINSTANCE, [[maybe_unused]] PWSTR t, int)
{
CoInitializeEx(0, COINIT_APARTMENTTHREADED);
// ChangeAssets(L"f:\\wuitools\\VisualWinUI3\\app.png", L"f:\\wuitools\\VisualWinUI3\\assets");
hIcon1 = LoadIcon(h, L"ICON_1");
{
void (WINAPI * pfnXamlCheckProcessRequirements)();
auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");
if (module)
{
pfnXamlCheckProcessRequirements = reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(GetProcAddress(module, "XamlCheckProcessRequirements"));
if (pfnXamlCheckProcessRequirements)
{
(*pfnXamlCheckProcessRequirements)();
}
::FreeLibrary(module);
}
}
PWSTR p = 0;
SHGetKnownFolderPath(FOLDERID_ProgramData, 0, 0, &p);
std::wstring de = p;
CoTaskMemFree(p);
de += L"\\1264A553-CB70-4C77-BA41-6221EC8AAF86";
SHCreateDirectory(0, de.c_str());
datafolder = de.c_str();
std::wstring sf = de + L"\\settings.xml";
SettingsX = std::make_shared<XML3::XML>(sf.c_str());
winrt::init_apartment(winrt::apartment_type::single_threaded);
::winrt::Microsoft::UI::Xaml::Application::Start(
[](auto&&)
{
::winrt::make<::winrt::VisualWinUI3::implementation::App>();
});
SettingsX->Save();
return 0;
}