Skip to content

Commit

Permalink
Update ICoreWindowInterop and IDesktopWindowXamlSourceNative2 method …
Browse files Browse the repository at this point in the history
…invoke

Update ICoreWindowInterop and IDesktopWindowXamlSourceNative2 method invoke
  • Loading branch information
Gaoyifei1011 committed Dec 29, 2024
1 parent 6619e76 commit d6cdd50
Showing 1 changed file with 7 additions and 34 deletions.
41 changes: 7 additions & 34 deletions WindowsTools/Views/Windows/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Mile.Xaml;
using Mile.Xaml.Interop;
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -22,6 +19,7 @@
using WindowsTools.Services.Root;
using WindowsTools.UI.Backdrop;
using WindowsTools.Views.Pages;
using WindowsTools.WindowsAPI.ComTypes;
using WindowsTools.WindowsAPI.PInvoke.Comctl32;
using WindowsTools.WindowsAPI.PInvoke.Dwmapi;
using WindowsTools.WindowsAPI.PInvoke.Gdi32;
Expand All @@ -44,7 +42,7 @@ public class MainWindow : Form
private readonly IntPtr hwndMaximizeButton;
private readonly int AUTO_HIDE_TASKBAR_HEIGHT = 2;
private readonly int lightTintColor = ColorTranslator.ToWin32(Color.FromArgb(243, 243, 243));
private readonly int darkTintColor = ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(32, 32, 32));
private readonly int darkTintColor = ColorTranslator.ToWin32(Color.FromArgb(32, 32, 32));
private readonly Container components = new();
private readonly DesktopWindowXamlSource desktopWindowXamlSource = new();
private readonly WNDPROC TitleBarWndProc;
Expand All @@ -68,9 +66,9 @@ public MainWindow()
Content = new MainPage();

desktopWindowXamlSource.Content = Content;
IDesktopWindowXamlSourceNative2 desktopWindowXamlSourceInterop = desktopWindowXamlSource.GetInterop();
IDesktopWindowXamlSourceNative2 desktopWindowXamlSourceInterop = desktopWindowXamlSource as IDesktopWindowXamlSourceNative2;
desktopWindowXamlSourceInterop.AttachToWindow(Handle);
hwndXamlIsland = desktopWindowXamlSourceInterop.GetWindowHandle();
desktopWindowXamlSourceInterop.GetWindowHandle(out hwndXamlIsland);
desktopWindowXamlSource.TakeFocusRequested += OnTakeFocusRequested;

Icon = Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
Expand Down Expand Up @@ -170,17 +168,8 @@ public MainWindow()
BackdropService.PropertyChanged += OnServicePropertyChanged;
TopMostService.PropertyChanged += OnServicePropertyChanged;

// 修改 CoreWindow 窗口的大小
IntPtr coreWindowhandle = Window.Current.CoreWindow.GetInterop().GetWindowHandle();
(Window.Current.CoreWindow as object as ICoreWindowInterop).GetWindowHandle(out IntPtr coreWindowhandle);
User32Library.SetWindowPos(coreWindowhandle, IntPtr.Zero, 0, 0, Size.Width, Size.Height, SetWindowPosFlags.SWP_NOOWNERZORDER);

// 修改托管 CoreWindow 窗口的大小和位置
Form coreWindowHostWindowForm = typeof(XamlApplicationExtensions).GetField("CoreWindowHostWindow", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Form;
if (coreWindowHostWindowForm is not null)
{
coreWindowHostWindowForm.Location = Location;
coreWindowHostWindowForm.Size = Size;
}
}

#region 第一部分:窗口类内置需要重载的事件
Expand Down Expand Up @@ -307,14 +296,6 @@ protected override void OnMove(EventArgs args)
popup.CompositeMode = compositeMode;
}
}

// 修改托管 CoreWindow 窗口的大小和位置
Form coreWindowHostWindowForm = typeof(XamlApplicationExtensions).GetField("CoreWindowHostWindow", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Form;
if (coreWindowHostWindowForm is not null)
{
coreWindowHostWindowForm.Location = Location;
coreWindowHostWindowForm.Size = Size;
}
}

/// <summary>
Expand Down Expand Up @@ -343,16 +324,8 @@ protected override void OnSizeChanged(EventArgs args)
}

// 修改 CoreWindow 窗口的大小
IntPtr coreWindowhandle = Window.Current.CoreWindow.GetInterop().GetWindowHandle();
(Window.Current.CoreWindow as object as ICoreWindowInterop).GetWindowHandle(out IntPtr coreWindowhandle);
User32Library.SetWindowPos(coreWindowhandle, IntPtr.Zero, 0, 0, Size.Width, Size.Height, SetWindowPosFlags.SWP_NOOWNERZORDER);

// 修改托管 CoreWindow 窗口的大小和位置
Form coreWindowHostWindowForm = typeof(XamlApplicationExtensions).GetField("CoreWindowHostWindow", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Form;
if (coreWindowHostWindowForm is not null)
{
coreWindowHostWindowForm.Location = Location;
coreWindowHostWindowForm.Size = Size;
}
}

/// <summary>
Expand Down

0 comments on commit d6cdd50

Please sign in to comment.