优化代码
This commit is contained in:
@@ -30,8 +30,7 @@ namespace Ink_Canvas.Helpers
|
||||
|
||||
if (!(bool)window.GetValue(IsAvoidFullScreenEnabledProperty))
|
||||
{
|
||||
var hwndSource = PresentationSource.FromVisual(window) as HwndSource;
|
||||
if (hwndSource != null)
|
||||
if (PresentationSource.FromVisual(window) is HwndSource hwndSource)
|
||||
{
|
||||
hwndSource.AddHook(KeepInWorkingAreaHook);
|
||||
window.SetValue(IsAvoidFullScreenEnabledProperty, true);
|
||||
@@ -46,8 +45,7 @@ namespace Ink_Canvas.Helpers
|
||||
|
||||
if ((bool)window.GetValue(IsAvoidFullScreenEnabledProperty))
|
||||
{
|
||||
var hwndSource = PresentationSource.FromVisual(window) as HwndSource;
|
||||
if (hwndSource != null)
|
||||
if (PresentationSource.FromVisual(window) is HwndSource hwndSource)
|
||||
{
|
||||
hwndSource.RemoveHook(KeepInWorkingAreaHook);
|
||||
window.ClearValue(IsAvoidFullScreenEnabledProperty);
|
||||
@@ -61,8 +59,7 @@ namespace Ink_Canvas.Helpers
|
||||
private static IntPtr KeepInWorkingAreaHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
|
||||
{
|
||||
// 只拦截主画布窗口的全屏(最大化)操作
|
||||
var window = HwndSource.FromHwnd(hwnd)?.RootVisual as Window;
|
||||
if (window == null) return IntPtr.Zero;
|
||||
if (!(HwndSource.FromHwnd(hwnd)?.RootVisual is Window window)) return IntPtr.Zero;
|
||||
// 这里假设主画布窗口类名为MainWindow(如有不同请调整)
|
||||
if (window.GetType().Name != "MainWindow") return IntPtr.Zero;
|
||||
|
||||
|
||||
@@ -112,8 +112,7 @@ namespace Ink_Canvas.Helpers
|
||||
{
|
||||
// 将墨迹添加到 inkCanvas 的父容器中,而不是 inkCanvas.Children
|
||||
// 这样可以避免坐标系统问题
|
||||
var parent = _mainWindow.inkCanvas.Parent as Panel;
|
||||
if (parent != null)
|
||||
if (_mainWindow.inkCanvas.Parent is Panel parent)
|
||||
{
|
||||
parent.Children.Add(strokeVisual);
|
||||
}
|
||||
@@ -159,8 +158,7 @@ namespace Ink_Canvas.Helpers
|
||||
try
|
||||
{
|
||||
// 从父容器中移除墨迹
|
||||
var parent = _mainWindow.inkCanvas?.Parent as Panel;
|
||||
if (parent != null && parent.Children.Contains(visual))
|
||||
if (_mainWindow.inkCanvas?.Parent is Panel parent && parent.Children.Contains(visual))
|
||||
{
|
||||
parent.Children.Remove(visual);
|
||||
}
|
||||
@@ -518,8 +516,7 @@ namespace Ink_Canvas.Helpers
|
||||
originalVisual.Visibility = Visibility.Hidden;
|
||||
|
||||
var segments = new List<UIElement>();
|
||||
var parent = _mainWindow.inkCanvas?.Parent as Panel;
|
||||
if (parent == null)
|
||||
if (!(_mainWindow.inkCanvas?.Parent is Panel parent))
|
||||
{
|
||||
// 如果父容器不是Panel,直接使用InkCanvas
|
||||
parent = null; // 稍后会检查并使用InkCanvas.Children
|
||||
@@ -875,8 +872,7 @@ namespace Ink_Canvas.Helpers
|
||||
try
|
||||
{
|
||||
// 从父容器中移除墨迹
|
||||
var parent = _mainWindow.inkCanvas?.Parent as Panel;
|
||||
if (parent != null && parent.Children.Contains(visual))
|
||||
if (_mainWindow.inkCanvas?.Parent is Panel parent && parent.Children.Contains(visual))
|
||||
{
|
||||
parent.Children.Remove(visual);
|
||||
}
|
||||
|
||||
@@ -313,8 +313,7 @@ namespace Ink_Canvas.Helpers.Plugins.BuiltIn
|
||||
var buttonElement = _launcherButton.Element;
|
||||
|
||||
// 查找浮动栏
|
||||
var floatingBar = mainWindow.FindName("StackPanelFloatingBar") as Panel;
|
||||
if (floatingBar == null)
|
||||
if (!(mainWindow.FindName("StackPanelFloatingBar") is Panel floatingBar))
|
||||
{
|
||||
// 如果直接查找失败,则尝试遍历可视树查找
|
||||
Panel floatingBarPanelFromTree = null;
|
||||
@@ -405,8 +404,7 @@ namespace Ink_Canvas.Helpers.Plugins.BuiltIn
|
||||
var buttonElement = _launcherButton.Element;
|
||||
|
||||
// 查找浮动栏
|
||||
var floatingBar = mainWindow.FindName("StackPanelFloatingBar") as Panel;
|
||||
if (floatingBar == null)
|
||||
if (!(mainWindow.FindName("StackPanelFloatingBar") is Panel floatingBar))
|
||||
{
|
||||
// 如果直接查找失败,则尝试遍历可视树查找
|
||||
Panel floatingBarPanelFromTree = null;
|
||||
|
||||
@@ -25,8 +25,7 @@ namespace Ink_Canvas.Helpers
|
||||
return Screen.PrimaryScreen;
|
||||
|
||||
// 获取窗口的句柄
|
||||
var hwndSource = PresentationSource.FromVisual(window) as HwndSource;
|
||||
if (hwndSource == null)
|
||||
if (!(PresentationSource.FromVisual(window) is HwndSource hwndSource))
|
||||
return Screen.PrimaryScreen;
|
||||
|
||||
// 获取窗口在屏幕上的位置
|
||||
|
||||
@@ -61,8 +61,7 @@ namespace Ink_Canvas.Helpers
|
||||
{
|
||||
if (windowTitle.Length == windowName.Length)
|
||||
{
|
||||
WindowPattern windowPattern = window.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
|
||||
if (windowPattern != null)
|
||||
if (window.GetCurrentPattern(WindowPattern.Pattern) is WindowPattern windowPattern)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -70,8 +69,7 @@ namespace Ink_Canvas.Helpers
|
||||
}
|
||||
else
|
||||
{
|
||||
WindowPattern windowPattern = window.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern;
|
||||
if (windowPattern != null)
|
||||
if (window.GetCurrentPattern(WindowPattern.Pattern) is WindowPattern windowPattern)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user