实施镜头放大以放大 Windows 任务栏
Implement lens magnification to magnify the Windows Taskbar
我希望能够在 windows 任务栏顶部执行镜头放大。到目前为止,我一直未能成功实现这一目标,因为任务栏将始终在我的 window 之上打开。 Windows 内置放大镜可以做到这一点,所以我希望这确实是可能的。
我附上了两个屏幕截图,其中显示了 Windows 内置放大镜以及它如何放大任务栏以及我的应用程序将如何呈现在任务栏下方。
Windows内置放大镜:
我的申请:
有没有办法让我的应用程序呈现在任务栏上方,从而放大任务栏?
<Window x:Class="WpfNativeTesting.MagnificationWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfNativeTesting"
mc:Ignorable="d"
Title="MagnificationWindow"
Height="400"
Width="400"
WindowStyle="None"
ResizeMode="NoResize"
AllowsTransparency="true"
ShowInTaskbar="False"
Topmost="True">
<Grid x:Name="ContainerGrid">
<Grid x:Name="MagnificationGrid" />
</Grid>
</Window>
public partial class MagnificationWindow : Window
{
private IntPtr HWnd;
private IntPtr HWndMag;
private bool MagnificationInitialized = false;
private DispatcherTimer Timer = new DispatcherTimer();
private RECT MagWindowRect = new RECT();
private bool IsColorEffectSet = false;
private float magnification = 1.0f;
public float Magnification
{
get { return magnification; }
set
{
if (value < 1.0f)
{
value = 1.0f;
}
if (HWndMag != null)
{
if (magnification != value)
{
magnification = value;
Transformation matrix = new Transformation(magnification);
NativeMethods.MagSetWindowTransform(HWndMag, ref matrix);
}
}
}
}
public MagnificationWindow()
{
InitializeComponent();
Loaded += MagnificationWindow_Loaded;
Show();
}
private void MagnificationWindow_Loaded(object sender, RoutedEventArgs e)
{
HWnd = new WindowInteropHelper(this).Handle;
var exStyle = NativeMethods.GetWindowLong(HWnd, NativeMethods.GWL_EXSTYLE);
exStyle |= (int)ExtendedWindowStyles.WS_EX_TOPMOST | (int)ExtendedWindowStyles.WS_EX_LAYERED | (int)ExtendedWindowStyles.WS_EX_TRANSPARENT;
NativeMethods.SetWindowLong(HWnd, NativeMethods.GWL_EXSTYLE, exStyle);
var style = NativeMethods.GetWindowLong(HWnd, NativeMethods.GWL_STYLE);
style |= (int)WindowStyles.WS_CAPTION | (int)WindowStyles.WS_SYSMENU;
NativeMethods.SetWindowLong(HWnd, NativeMethods.GWL_STYLE, exStyle);
MagnificationInitialized = NativeMethods.MagInitialize();
if (MagnificationInitialized)
{
SetupMagnifier();
Timer.Interval = TimeSpan.FromMilliseconds(NativeMethods.USER_TIMER_MINIMUM);
Timer.Tick += Timer_Tick;
Timer.Start();
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
RemoveMagnifier();
}
private void Timer_Tick(object sender, EventArgs e)
{
UpdateMaginifier();
}
private void SetupMagnifier()
{
var hInst = NativeMethods.GetModuleHandle(null);
NativeMethods.GetClientRect(HWnd, ref MagWindowRect);
HWndMag = NativeMethods.CreateWindow((int)ExtendedWindowStyles.WS_EX_STATICEDGE, NativeMethods.WC_MAGNIFIER,
"MagnificationWindow", (int)WindowStyles.WS_CHILD | (int)MagnifierStyle.MS_SHOWMAGNIFIEDCURSOR | (int)WindowStyles.WS_VISIBLE,
MagWindowRect.left, MagWindowRect.top, MagWindowRect.right, MagWindowRect.bottom, HWnd, IntPtr.Zero, hInst, IntPtr.Zero);
NativeMethods.MagShowSystemCursor(false);
if (HWndMag == IntPtr.Zero)
{
return;
}
var matrix = new Transformation(Magnification);
NativeMethods.MagSetWindowTransform(HWndMag, ref matrix);
}
private void UpdateMaginifier()
{
if (!MagnificationInitialized || HWndMag == IntPtr.Zero)
{
return;
}
POINT mousePoint = new POINT();
RECT sourceRect = new RECT();
NativeMethods.GetCursorPos(ref mousePoint);
int width = (int)((MagWindowRect.right - MagWindowRect.left) / Magnification);
int height = (int)((MagWindowRect.bottom - MagWindowRect.top) / Magnification);
sourceRect.left = mousePoint.x - width / 2;
sourceRect.top = mousePoint.y - height / 2;
NativeMethods.MagSetWindowSource(HWndMag, sourceRect);
POINT mouse = new POINT();
NativeMethods.GetCursorPos(ref mouse);
NativeMethods.SetWindowPos(HWnd, NativeMethods.HWND_TOPMOST, mouse.x - (int)(magnification * width / 2), mouse.y - (int)(magnification * height / 2), width, height,
(int)SetWindowPosFlags.SWP_NOACTIVATE |
(int)SetWindowPosFlags.SWP_NOSIZE);
NativeMethods.InvalidateRect(HWndMag, IntPtr.Zero, true);
}
public void RemoveMagnifier()
{
if (MagnificationInitialized)
{
NativeMethods.MagUninitialize();
MagnificationInitialized = false;
}
}
// ...
}
我在 Microsoft Q/A 论坛上发布了这个问题并找到了解决方案。
We need to make it a Accessibility app by setting uiAcess=true in the
manifest, sign the executable, and placing it in a secure location
(e.g. Program Files) as described below:
Set uiAccess=true in the manifest Set this option in Visual Studio
by setting Linker | Manifest File | UAC Bypass UI Protection to Yes
Sign the executable See
https://docs.microsoft.com/en-us/previous-versions/bb756995(v=msdn.10)
Place it in a secure location See
https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/user-account-control-only-elevate-uiaccess-applications-that-are-installed-in-secure-locations
我在签名之前将应用程序放在安全位置,并使用以下命令创建证书并对应用程序签名。
makecert /n "CN=Company, O=Company, C=SE" /r /pe /h 0 /eku "1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13" /e 01/01/2021 /sv Company.pvk Company.cer
Pvk2Pfx /pvk Company.pvk /pi "password" /spc Company.cer /pfx Company.pfx /po "password" /pi "password"
最后
signtool sign /f "Company.pfx" /p "password" "application".exe
就是这样!
我希望能够在 windows 任务栏顶部执行镜头放大。到目前为止,我一直未能成功实现这一目标,因为任务栏将始终在我的 window 之上打开。 Windows 内置放大镜可以做到这一点,所以我希望这确实是可能的。
我附上了两个屏幕截图,其中显示了 Windows 内置放大镜以及它如何放大任务栏以及我的应用程序将如何呈现在任务栏下方。
Windows内置放大镜:
我的申请:
有没有办法让我的应用程序呈现在任务栏上方,从而放大任务栏?
<Window x:Class="WpfNativeTesting.MagnificationWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfNativeTesting"
mc:Ignorable="d"
Title="MagnificationWindow"
Height="400"
Width="400"
WindowStyle="None"
ResizeMode="NoResize"
AllowsTransparency="true"
ShowInTaskbar="False"
Topmost="True">
<Grid x:Name="ContainerGrid">
<Grid x:Name="MagnificationGrid" />
</Grid>
</Window>
public partial class MagnificationWindow : Window
{
private IntPtr HWnd;
private IntPtr HWndMag;
private bool MagnificationInitialized = false;
private DispatcherTimer Timer = new DispatcherTimer();
private RECT MagWindowRect = new RECT();
private bool IsColorEffectSet = false;
private float magnification = 1.0f;
public float Magnification
{
get { return magnification; }
set
{
if (value < 1.0f)
{
value = 1.0f;
}
if (HWndMag != null)
{
if (magnification != value)
{
magnification = value;
Transformation matrix = new Transformation(magnification);
NativeMethods.MagSetWindowTransform(HWndMag, ref matrix);
}
}
}
}
public MagnificationWindow()
{
InitializeComponent();
Loaded += MagnificationWindow_Loaded;
Show();
}
private void MagnificationWindow_Loaded(object sender, RoutedEventArgs e)
{
HWnd = new WindowInteropHelper(this).Handle;
var exStyle = NativeMethods.GetWindowLong(HWnd, NativeMethods.GWL_EXSTYLE);
exStyle |= (int)ExtendedWindowStyles.WS_EX_TOPMOST | (int)ExtendedWindowStyles.WS_EX_LAYERED | (int)ExtendedWindowStyles.WS_EX_TRANSPARENT;
NativeMethods.SetWindowLong(HWnd, NativeMethods.GWL_EXSTYLE, exStyle);
var style = NativeMethods.GetWindowLong(HWnd, NativeMethods.GWL_STYLE);
style |= (int)WindowStyles.WS_CAPTION | (int)WindowStyles.WS_SYSMENU;
NativeMethods.SetWindowLong(HWnd, NativeMethods.GWL_STYLE, exStyle);
MagnificationInitialized = NativeMethods.MagInitialize();
if (MagnificationInitialized)
{
SetupMagnifier();
Timer.Interval = TimeSpan.FromMilliseconds(NativeMethods.USER_TIMER_MINIMUM);
Timer.Tick += Timer_Tick;
Timer.Start();
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
RemoveMagnifier();
}
private void Timer_Tick(object sender, EventArgs e)
{
UpdateMaginifier();
}
private void SetupMagnifier()
{
var hInst = NativeMethods.GetModuleHandle(null);
NativeMethods.GetClientRect(HWnd, ref MagWindowRect);
HWndMag = NativeMethods.CreateWindow((int)ExtendedWindowStyles.WS_EX_STATICEDGE, NativeMethods.WC_MAGNIFIER,
"MagnificationWindow", (int)WindowStyles.WS_CHILD | (int)MagnifierStyle.MS_SHOWMAGNIFIEDCURSOR | (int)WindowStyles.WS_VISIBLE,
MagWindowRect.left, MagWindowRect.top, MagWindowRect.right, MagWindowRect.bottom, HWnd, IntPtr.Zero, hInst, IntPtr.Zero);
NativeMethods.MagShowSystemCursor(false);
if (HWndMag == IntPtr.Zero)
{
return;
}
var matrix = new Transformation(Magnification);
NativeMethods.MagSetWindowTransform(HWndMag, ref matrix);
}
private void UpdateMaginifier()
{
if (!MagnificationInitialized || HWndMag == IntPtr.Zero)
{
return;
}
POINT mousePoint = new POINT();
RECT sourceRect = new RECT();
NativeMethods.GetCursorPos(ref mousePoint);
int width = (int)((MagWindowRect.right - MagWindowRect.left) / Magnification);
int height = (int)((MagWindowRect.bottom - MagWindowRect.top) / Magnification);
sourceRect.left = mousePoint.x - width / 2;
sourceRect.top = mousePoint.y - height / 2;
NativeMethods.MagSetWindowSource(HWndMag, sourceRect);
POINT mouse = new POINT();
NativeMethods.GetCursorPos(ref mouse);
NativeMethods.SetWindowPos(HWnd, NativeMethods.HWND_TOPMOST, mouse.x - (int)(magnification * width / 2), mouse.y - (int)(magnification * height / 2), width, height,
(int)SetWindowPosFlags.SWP_NOACTIVATE |
(int)SetWindowPosFlags.SWP_NOSIZE);
NativeMethods.InvalidateRect(HWndMag, IntPtr.Zero, true);
}
public void RemoveMagnifier()
{
if (MagnificationInitialized)
{
NativeMethods.MagUninitialize();
MagnificationInitialized = false;
}
}
// ...
}
我在 Microsoft Q/A 论坛上发布了这个问题并找到了解决方案。
We need to make it a Accessibility app by setting uiAcess=true in the manifest, sign the executable, and placing it in a secure location (e.g. Program Files) as described below:
Set uiAccess=true in the manifest Set this option in Visual Studio by setting Linker | Manifest File | UAC Bypass UI Protection to Yes
Sign the executable See https://docs.microsoft.com/en-us/previous-versions/bb756995(v=msdn.10)
Place it in a secure location See https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/user-account-control-only-elevate-uiaccess-applications-that-are-installed-in-secure-locations
我在签名之前将应用程序放在安全位置,并使用以下命令创建证书并对应用程序签名。
makecert /n "CN=Company, O=Company, C=SE" /r /pe /h 0 /eku "1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13" /e 01/01/2021 /sv Company.pvk Company.cer
Pvk2Pfx /pvk Company.pvk /pi "password" /spc Company.cer /pfx Company.pfx /po "password" /pi "password"
最后
signtool sign /f "Company.pfx" /p "password" "application".exe
就是这样!