improve:窗口置顶
This commit is contained in:
@@ -24,23 +24,11 @@
|
||||
Margin="0,0,0,0" Visibility="Collapsed"
|
||||
Foreground="#5B5D5F"
|
||||
Text="00" FontSize="26"/>
|
||||
<ItemsControl Name="TextControlHour" ItemsSource="{Binding ElementName=TextBlockHour, Path=Text}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal" />
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid Width="14">
|
||||
<TextBlock Text="{Binding}" HorizontalAlignment="Center" FontWeight="SemiBold"
|
||||
Foreground="{Binding ElementName=TextBlockHour, Path=Foreground}"
|
||||
FontFamily="{Binding ElementName=TextBlockHour, Path=FontFamily}"
|
||||
FontSize="{Binding ElementName=TextBlockHour, Path=FontSize}"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
<TextBlock Name="TextControlHour" Text="{Binding ElementName=TextBlockHour, Path=Text}"
|
||||
FontFamily="{Binding ElementName=TextBlockHour, Path=FontFamily}"
|
||||
Foreground="{Binding ElementName=TextBlockHour, Path=Foreground}"
|
||||
FontSize="{Binding ElementName=TextBlockHour, Path=FontSize}"
|
||||
FontWeight="SemiBold" HorizontalAlignment="Center"/>
|
||||
<Grid Name="GridAdjustHour" Visibility="Visible" Margin="-29,-30,0,-30" Width="29">
|
||||
<ui:SimpleStackPanel Spacing="2">
|
||||
<Button Height="13" Width="{Binding ElementName=GridAdjustHour, Path=ActualWidth}" Click="Button_Click_1">
|
||||
@@ -71,23 +59,11 @@
|
||||
Margin="0,0,0,0" Visibility="Collapsed"
|
||||
Foreground="{Binding ElementName=TextBlockHour, Path=Foreground}"
|
||||
Text="01" FontSize="26"/>
|
||||
<ItemsControl Name="TextControlMinute" ItemsSource="{Binding ElementName=TextBlockMinute, Path=Text}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal" />
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid Width="14">
|
||||
<TextBlock Text="{Binding}" HorizontalAlignment="Center" FontWeight="SemiBold"
|
||||
Foreground="{Binding ElementName=TextBlockMinute, Path=Foreground}"
|
||||
FontFamily="{Binding ElementName=TextBlockMinute, Path=FontFamily}"
|
||||
FontSize="{Binding ElementName=TextBlockMinute, Path=FontSize}"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
<TextBlock Name="TextControlMinute" Text="{Binding ElementName=TextBlockMinute, Path=Text}"
|
||||
FontFamily="{Binding ElementName=TextBlockMinute, Path=FontFamily}"
|
||||
Foreground="{Binding ElementName=TextBlockMinute, Path=Foreground}"
|
||||
FontSize="{Binding ElementName=TextBlockMinute, Path=FontSize}"
|
||||
FontWeight="SemiBold" HorizontalAlignment="Center"/>
|
||||
<Grid Visibility="{Binding ElementName=GridAdjustHour, Path=Visibility}" Margin="-29,-30,0,-30" Width="29">
|
||||
<ui:SimpleStackPanel Spacing="2">
|
||||
<Button Height="13" Width="{Binding ElementName=GridAdjustHour, Path=ActualWidth}" Click="Button_Click_5">
|
||||
@@ -130,23 +106,11 @@
|
||||
Margin="0,0,0,0" Visibility="Collapsed"
|
||||
Foreground="{Binding ElementName=TextBlockHour, Path=Foreground}"
|
||||
Text="00" FontSize="26"/>
|
||||
<ItemsControl Name="TextControlSecond" ItemsSource="{Binding ElementName=TextBlockSecond, Path=Text}">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<StackPanel Orientation="Horizontal" />
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<Grid Width="14">
|
||||
<TextBlock Text="{Binding}" HorizontalAlignment="Center" FontWeight="SemiBold"
|
||||
Foreground="{Binding ElementName=TextBlockSecond, Path=Foreground}"
|
||||
FontFamily="{Binding ElementName=TextBlockSecond, Path=FontFamily}"
|
||||
FontSize="{Binding ElementName=TextBlockSecond, Path=FontSize}"/>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
<TextBlock Name="TextControlSecond" Text="{Binding ElementName=TextBlockSecond, Path=Text}"
|
||||
FontFamily="{Binding ElementName=TextBlockSecond, Path=FontFamily}"
|
||||
Foreground="{Binding ElementName=TextBlockSecond, Path=Foreground}"
|
||||
FontSize="{Binding ElementName=TextBlockSecond, Path=FontSize}"
|
||||
FontWeight="SemiBold" HorizontalAlignment="Center"/>
|
||||
<Grid Margin="-96.8,0,0,0" Width="96.8" Background="Transparent" MouseUp="Grid_MouseUp"/>
|
||||
<Grid Visibility="{Binding ElementName=GridAdjustHour, Path=Visibility}" Margin="-29,-30,0,-30" Width="29">
|
||||
<ui:SimpleStackPanel Spacing="2">
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
</Viewbox>
|
||||
</ui:SimpleStackPanel>
|
||||
</ScrollViewer>
|
||||
<Viewbox Visibility="{Binding ElementName=BigViewController, Path=Visibility}" Margin="20,20,20,20" HorizontalAlignment="Right">
|
||||
<Viewbox Margin="20,20,20,20" HorizontalAlignment="Right">
|
||||
<ui:SimpleStackPanel Height="180" Orientation="Horizontal">
|
||||
<Border x:Name="BtnFullscreen" MouseUp="BtnFullscreen_MouseUp" HorizontalAlignment="Right" VerticalAlignment="Bottom"
|
||||
Margin="5"
|
||||
|
||||
@@ -5,11 +5,14 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
using System.Windows.Threading;
|
||||
using MessageBox = iNKORE.UI.WPF.Modern.Controls.MessageBox;
|
||||
|
||||
namespace Ink_Canvas
|
||||
@@ -35,6 +38,9 @@ namespace Ink_Canvas
|
||||
|
||||
// 添加窗口关闭事件处理
|
||||
Closed += RandWindow_Closed;
|
||||
|
||||
// 添加窗口显示事件处理,确保置顶
|
||||
Loaded += RandWindow_Loaded;
|
||||
}
|
||||
|
||||
private void LoadBackground(Settings settings)
|
||||
@@ -86,6 +92,9 @@ namespace Ink_Canvas
|
||||
|
||||
// 添加窗口关闭事件处理
|
||||
Closed += RandWindow_Closed;
|
||||
|
||||
// 添加窗口显示事件处理,确保置顶
|
||||
Loaded += RandWindow_Loaded;
|
||||
|
||||
new Thread(() =>
|
||||
{
|
||||
@@ -349,6 +358,51 @@ namespace Ink_Canvas
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 窗口加载事件处理
|
||||
/// </summary>
|
||||
private void RandWindow_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// 使用延迟确保窗口完全加载后再应用置顶
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
// 强制激活窗口
|
||||
Activate();
|
||||
Focus();
|
||||
|
||||
// 设置置顶
|
||||
Topmost = true;
|
||||
|
||||
// 使用Win32 API强制置顶
|
||||
var hwnd = new WindowInteropHelper(this).Handle;
|
||||
if (hwnd != IntPtr.Zero)
|
||||
{
|
||||
const int WS_EX_TOPMOST = 0x00000008;
|
||||
const int GWL_EXSTYLE = -20;
|
||||
const int SWP_NOMOVE = 0x0002;
|
||||
const int SWP_NOSIZE = 0x0001;
|
||||
const int SWP_SHOWWINDOW = 0x0040;
|
||||
const int SWP_NOOWNERZORDER = 0x0200;
|
||||
var HWND_TOPMOST = new IntPtr(-1);
|
||||
|
||||
// 设置窗口样式为置顶
|
||||
int exStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
|
||||
SetWindowLong(hwnd, GWL_EXSTYLE, exStyle | WS_EX_TOPMOST);
|
||||
|
||||
// 强制置顶
|
||||
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
|
||||
SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOOWNERZORDER);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"RandWindow置顶失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}), DispatcherPriority.Loaded);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 窗口关闭事件处理
|
||||
/// </summary>
|
||||
@@ -357,5 +411,16 @@ namespace Ink_Canvas
|
||||
// 窗口关闭时的清理工作
|
||||
// 这里可以添加必要的清理代码
|
||||
}
|
||||
|
||||
#region Win32 API 声明
|
||||
[DllImport("user32.dll")]
|
||||
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user