Skip to content

Commit

Permalink
move WindowEx to namespace Frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
Scighost committed Nov 16, 2024
1 parent e4a2a3a commit 1027a37
Show file tree
Hide file tree
Showing 18 changed files with 126 additions and 145 deletions.
1 change: 1 addition & 0 deletions src/Starward/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.Windows.AppLifecycle;
using Starward.Frameworks;
using Starward.Helpers;
using System;
using System.Globalization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI;
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Starward.Messages;
using System;
using System.Runtime.InteropServices;
using Vanara.PInvoke;
using Windows.Graphics;
using Windows.UI;

namespace Starward.MyWindows;
namespace Starward.Frameworks;

public abstract partial class WindowEx : Window
{


public IntPtr WindowHandle { get; private init; }

public IntPtr BridgeHandle { get; private init; }
private IntPtr InputSiteHandle { get; init; }


public double UIScale => User32.GetDpiForWindow(WindowHandle) / 96d;
Expand All @@ -28,11 +26,12 @@ public abstract partial class WindowEx : Window
public WindowEx()
{
WindowHandle = (IntPtr)AppWindow.Id.Value;
BridgeHandle = (IntPtr)User32.FindWindowEx(WindowHandle, IntPtr.Zero, "Microsoft.UI.Content.DesktopChildSiteBridge", null);
HWND bridge = (IntPtr)User32.FindWindowEx(WindowHandle, IntPtr.Zero, "Microsoft.UI.Content.DesktopChildSiteBridge", null);
InputSiteHandle = (IntPtr)User32.FindWindowEx(bridge, IntPtr.Zero, "InputSiteWindowClass", null);
windowSubclassProc = new(WindowSubclassProc);
bridgeSubclassProc = new(BridgeSubclassProc);
inputSiteSubclassProc = new(InputSiteSubclassProc);
ComCtl32.SetWindowSubclass(WindowHandle, windowSubclassProc, 1001, IntPtr.Zero);
ComCtl32.SetWindowSubclass(BridgeHandle, bridgeSubclassProc, 1002, IntPtr.Zero);
ComCtl32.SetWindowSubclass(InputSiteHandle, inputSiteSubclassProc, 1002, IntPtr.Zero);
}


Expand All @@ -44,18 +43,18 @@ public WindowEx()

private readonly ComCtl32.SUBCLASSPROC windowSubclassProc;

private readonly ComCtl32.SUBCLASSPROC bridgeSubclassProc;
private readonly ComCtl32.SUBCLASSPROC inputSiteSubclassProc;



public unsafe virtual IntPtr WindowSubclassProc(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
protected unsafe virtual IntPtr WindowSubclassProc(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
{
return ComCtl32.DefSubclassProc(hWnd, uMsg, wParam, lParam);
}



public unsafe virtual IntPtr BridgeSubclassProc(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
protected unsafe virtual IntPtr InputSiteSubclassProc(HWND hWnd, uint uMsg, IntPtr wParam, IntPtr lParam, nuint uIdSubclass, IntPtr dwRefData)
{
return ComCtl32.DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
Expand All @@ -78,15 +77,13 @@ public virtual void Show()
AppWindow.MoveInZOrderAtTop();
User32.ShowWindow(WindowHandle, ShowWindowCommand.SW_SHOWNORMAL);
User32.SetForegroundWindow(WindowHandle);
WeakReferenceMessenger.Default.Send(new WindowStateChangedMessage(false));
}



public virtual void Hide()
{
AppWindow.Hide();
WeakReferenceMessenger.Default.Send(new WindowStateChangedMessage(true));
}


Expand All @@ -113,31 +110,6 @@ public virtual void CenterInScreen(int? width = null, int? height = null)




public bool IsTopMost
{
get
{
User32.WindowStylesEx flags = (User32.WindowStylesEx)User32.GetWindowLong(WindowHandle, User32.WindowLongFlags.GWL_EXSTYLE);
return flags.HasFlag(User32.WindowStylesEx.WS_EX_TOPMOST);
}
set
{
User32.WindowStylesEx flags = (User32.WindowStylesEx)User32.GetWindowLong(WindowHandle, User32.WindowLongFlags.GWL_EXSTYLE);
if (value)
{
flags |= User32.WindowStylesEx.WS_EX_TOPMOST;
}
else
{
flags &= ~User32.WindowStylesEx.WS_EX_TOPMOST;
}
User32.SetWindowLong(WindowHandle, User32.WindowLongFlags.GWL_EXSTYLE, (nint)flags);
}
}



public void SetIcon(string? iconPath = null)
{
if (string.IsNullOrWhiteSpace(iconPath))
Expand All @@ -159,9 +131,18 @@ public void SetIcon(string? iconPath = null)



#region Title Bar
#region Theme



public void SetDragRectangles(params RectInt32[] value)
{
if (AppWindowTitleBar.IsCustomizationSupported() && AppWindow.TitleBar.ExtendsContentIntoTitleBar == true)
{
AppWindow.TitleBar.SetDragRectangles(value);
}
}


public void AdaptTitleBarButtonColorToActuallTheme()
{
Expand Down Expand Up @@ -197,24 +178,6 @@ public void AdaptTitleBarButtonColorToActuallTheme()



public void SetDragRectangles(params RectInt32[] value)
{
if (AppWindowTitleBar.IsCustomizationSupported() && AppWindow.TitleBar.ExtendsContentIntoTitleBar == true)
{
AppWindow.TitleBar.SetDragRectangles(value);
}
}



#endregion




#region Accent Color


public virtual void ChangeAccentColor(Color? backColor = null, Color? foreColor = null)
{
if (Content is FrameworkElement element)
Expand All @@ -234,13 +197,14 @@ public virtual void ChangeAccentColor(Color? backColor = null, Color? foreColor
}


#endregion



[return: MarshalAs(UnmanagedType.Bool)]
[LibraryImport("uxtheme.dll", EntryPoint = "#138", SetLastError = true)]
protected static partial bool ShouldSystemUseDarkMode();



#endregion


}
19 changes: 10 additions & 9 deletions src/Starward/MyWindows/CloudGameGachaWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:WindowEx x:Class="Starward.MyWindows.CloudGameGachaWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lang="using:Starward.Language"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<sf:WindowEx x:Class="Starward.MyWindows.CloudGameGachaWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lang="using:Starward.Language"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sf="using:Starward.Frameworks"
mc:Ignorable="d">

<Grid x:Name="RootGrid"
Padding="0,32,0,0"
Expand Down Expand Up @@ -43,4 +44,4 @@

</Grid>

</local:WindowEx>
</sf:WindowEx>
3 changes: 2 additions & 1 deletion src/Starward/MyWindows/CloudGameGachaWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Starward.Core;
using Starward.Frameworks;
using Starward.Messages;
using System;
using System.Text.Json.Nodes;
Expand Down Expand Up @@ -50,7 +51,7 @@ private void RootGrid_Loaded(object sender, RoutedEventArgs e)
{
try
{
// todo WinAppSDK 升级到 1.5 后,为云游戏的网页缓存设置单独的文件夹
// todo WinAppSDK ������ 1.5 ��Ϊ����Ϸ����ҳ�������õ������ļ���
if (GameBiz.ToGame() == GameBiz.hk4e)
{
webview.Source = new Uri("https://ys.mihoyo.com/cloud/");
Expand Down
17 changes: 9 additions & 8 deletions src/Starward/MyWindows/GameNoticesWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:WindowEx x:Class="Starward.MyWindows.GameNoticesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<sf:WindowEx x:Class="Starward.MyWindows.GameNoticesWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sf="using:Starward.Frameworks"
mc:Ignorable="d">

<Grid Loaded="Grid_Loaded">
<Border Padding="20"
Expand All @@ -28,4 +29,4 @@
</WebView2.Resources>
</WebView2>
</Grid>
</local:WindowEx>
</sf:WindowEx>
5 changes: 3 additions & 2 deletions src/Starward/MyWindows/GameNoticesWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.UI.Xaml;
using Starward.Core;
using Starward.Core.Launcher;
using Starward.Frameworks;
using Starward.Helpers;
using Starward.Messages;
using Starward.Models;
Expand Down Expand Up @@ -90,7 +91,7 @@ private void InitializeWindow()



public override nint BridgeSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
protected override nint InputSiteSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
{
if (uMsg == (uint)User32.WindowMessage.WM_KEYDOWN)
{
Expand All @@ -102,7 +103,7 @@ public override nint BridgeSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint
return 0;
}
}
return base.BridgeSubclassProc(hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData);
return base.InputSiteSubclassProc(hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData);
}


Expand Down
19 changes: 10 additions & 9 deletions src/Starward/MyWindows/InstallGameWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<local:WindowEx x:Class="Starward.MyWindows.InstallGameWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sp="using:Starward.Pages"
mc:Ignorable="d">
<sf:WindowEx x:Class="Starward.MyWindows.InstallGameWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sf="using:Starward.Frameworks"
xmlns:sp="using:Starward.Pages"
mc:Ignorable="d">

<Grid>
<sp:DownloadGamePage />
</Grid>

</local:WindowEx>
</sf:WindowEx>
3 changes: 2 additions & 1 deletion src/Starward/MyWindows/InstallGameWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.UI.Windowing;
using Starward.Frameworks;
using System;
using System.IO;
using Vanara.PInvoke;
Expand Down Expand Up @@ -47,7 +48,7 @@ private void InitializeWindow()



public override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
protected override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
{
if (uMsg == (uint)User32.WindowMessage.WM_SYSCOMMAND)
{
Expand Down
25 changes: 13 additions & 12 deletions src/Starward/MyWindows/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!-- Copyright (c) Microsoft Corporation and Contributors. -->
<!-- Licensed under the MIT License. -->

<local:WindowEx x:Class="Starward.MyWindows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sh="using:Starward.Helpers"
xmlns:sp="using:Starward.Pages"
Closed="MainWindow_Closed"
mc:Ignorable="d">
<sf:WindowEx x:Class="Starward.MyWindows.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
xmlns:local="using:Starward.MyWindows"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sf="using:Starward.Frameworks"
xmlns:sh="using:Starward.Helpers"
xmlns:sp="using:Starward.Pages"
Closed="MainWindow_Closed"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

Expand All @@ -36,4 +37,4 @@

</Grid>

</local:WindowEx>
</sf:WindowEx>
7 changes: 4 additions & 3 deletions src/Starward/MyWindows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Animation;
using Starward.Controls;
using Starward.Frameworks;
using Starward.Models;
using Starward.Services.Download;
using System;
Expand Down Expand Up @@ -227,7 +228,7 @@ public void CloseOverlayPage()



public override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
protected override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
{
if (uMsg == (uint)User32.WindowMessage.WM_SYSCOMMAND)
{
Expand All @@ -254,7 +255,7 @@ public override nint WindowSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint
}


public unsafe override nint BridgeSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
protected unsafe override nint InputSiteSubclassProc(HWND hWnd, uint uMsg, nint wParam, nint lParam, nuint uIdSubclass, nint dwRefData)
{
bool handled = false;
if (uMsg == (uint)User32.WindowMessage.WM_KEYDOWN)
Expand All @@ -272,7 +273,7 @@ public unsafe override nint BridgeSubclassProc(HWND hWnd, uint uMsg, nint wParam
return IntPtr.Zero;
}
}
return base.BridgeSubclassProc(hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData);
return base.InputSiteSubclassProc(hWnd, uMsg, wParam, lParam, uIdSubclass, dwRefData);
}


Expand Down
Loading

0 comments on commit 1027a37

Please sign in to comment.