-
Notifications
You must be signed in to change notification settings - Fork 27
/
App.xaml.cs
144 lines (122 loc) · 5.23 KB
/
App.xaml.cs
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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using System.Runtime.InteropServices;
using WinRT;
using System.Xml.Linq;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace ITATKWinUI
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
///
public partial class App : Application
{
/// <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>
public App()
{
this.InitializeComponent();
//Set the theme and app icon from settings at startup
XDocument settingsXML = XDocument.Load(@"Settings.xml");
foreach (XElement item in from y in settingsXML.Descendants("Item") select y)
{
if (item.Attribute("Name").Value == "SettingTheme")
{
switch (item.Attribute("Setting").Value)
{
case "Dark Mode":
App.Current.RequestedTheme = ApplicationTheme.Dark;
break;
case "Light Mode":
App.Current.RequestedTheme = ApplicationTheme.Light;
break;
case "System Theme":
//We don't need to do anything, this is already the default
break;
default:
break;
}
}
if (item.Attribute("Name").Value == "SettingApplicationIconImage")
{
AppIcon = item.Attribute("Setting").Value;
}
}
}
public static string AppIcon;
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
/*protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
m_window.Activate();
}
private Window m_window;*/
protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
{
m_window = new MainWindow();
//Get the Window's HWND
var windowNative = m_window.As<IWindowNative>();
m_windowHandle = windowNative.WindowHandle;
void LoadIcon(string iconName)
{
string i = iconName.Replace("ms-appx:///","");
//Get the Window's HWND
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(m_window);
var hIcon = PInvoke.User32.LoadImage(System.IntPtr.Zero, i,
PInvoke.User32.ImageType.IMAGE_ICON, 16, 16, PInvoke.User32.LoadImageFlags.LR_LOADFROMFILE);
PInvoke.User32.SendMessage(hwnd, PInvoke.User32.WindowMessage.WM_SETICON, (System.IntPtr)0, hIcon);
}
LoadIcon(AppIcon);
m_window.Activate();
// The Window object doesn't have Width and Height properties in WInUI 3 Desktop yet.
// To set the Width and Height, you can use the Win32 API SetWindowPos.
// Note, you should apply the DPI scale factor if you are thinking of dpi instead of pixels.
SetWindowSize(m_windowHandle, 1450, 820);
}
public static string Title;
private void SetWindowSize(IntPtr hwnd, int width, int height)
{
var dpi = PInvoke.User32.GetDpiForWindow(hwnd);
float scalingFactor = (float)dpi / 96;
width = (int)(width * scalingFactor);
height = (int)(height * scalingFactor);
PInvoke.User32.SetWindowPos(hwnd, PInvoke.User32.SpecialWindowHandles.HWND_TOP,
0, 0, width, height,
PInvoke.User32.SetWindowPosFlags.SWP_NOMOVE);
}
private Window m_window;
private IntPtr m_windowHandle;
public Window Window => m_window;
public IntPtr WindowHandle { get { return m_windowHandle; } }
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("EECDBF0E-BAE9-4CB6-A68E-9598E1CB57BB")]
internal interface IWindowNative
{
IntPtr WindowHandle { get; }
}
}
}