Windows in realtà ha un flag per abilitare focus-follow-mouse ("tracking attivo della finestra"), che può essere abilitato facilmente tramite la mostruosa chiamata API Win32 "SystemParametersInfo" . Esistono programmi di terze parti per abilitare il flag, come i controlli X-Mouse , oppure è possibile eseguire la chiamata direttamente tramite PowerShell.
La documentazione non è sempre molto chiara su come pvParamviene utilizzato l' argomento e alcuni frammenti di PowerShell passano erroneamente un puntatore al valore, piuttosto che al valore stesso, quando si imposta questo particolare flag. Questo finisce per essere sempre interpretato come true, cioè lavorano accidentalmente per abilitare la bandiera, ma non per disabilitarla di nuovo.
Di seguito è riportato un frammento di PowerShell che esegue correttamente la chiamata. Include anche un corretto controllo degli errori, e ho cercato di optare per la pulizia piuttosto che per la brevità, per facilitare anche l'aggiunta di wrapper per altre funzionalità di SystemParametersInfo, nel caso ne trovassi qualcuno che ti interessa.
Grida a pinvoke.net per essere una risorsa utile per cose come questa.
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
public static class Spi {
[System.FlagsAttribute]
private enum Flags : uint {
None = 0x0,
UpdateIniFile = 0x1,
SendChange = 0x2,
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, UIntPtr pvParam, Flags flags );
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SystemParametersInfo(
uint uiAction, uint uiParam, out bool pvParam, Flags flags );
private static void check( bool ok ) {
if( ! ok )
throw new Win32Exception( Marshal.GetLastWin32Error() );
}
private static UIntPtr ToUIntPtr( this bool value ) {
return new UIntPtr( value ? 1u : 0u );
}
public static bool GetActiveWindowTracking() {
bool enabled;
check( SystemParametersInfo( 0x1000, 0, out enabled, Flags.None ) );
return enabled;
}
public static void SetActiveWindowTracking( bool enabled ) {
// note: pvParam contains the boolean (cast to void*), not a pointer to it!
check( SystemParametersInfo( 0x1001, 0, enabled.ToUIntPtr(), Flags.SendChange ) );
}
}
'@
# check if mouse-focus is enabled
[Spi]::GetActiveWindowTracking()
# disable mouse-focus (default)
[Spi]::SetActiveWindowTracking( $false )
# enable mouse-focus
[Spi]::SetActiveWindowTracking( $true )