向用户禁用屏幕保护程序

发布于 2024-10-18 12:53:49 字数 69 浏览 2 评论 0原文

我想禁用另一个用户的屏幕保护程序。怎样才能做到呢?

我有管理权限。我有一个不能被屏幕保护程序中断的应用程序。

I want to disable a screen saver to another user. How it can be done?

I have administrative privileges. I have an application that can't be interrupted by the screen saver.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱已欠费 2024-10-25 12:53:49

如果您自己编写应用程序,请考虑调用非托管 API SetThreadExecutionState (PInvoke 参考< /a>)。从我的答案复制到 我该怎么办在程序执行期间阻止屏幕保护程序和睡眠?

不要弄乱屏幕保护程序设置,请使用 SetThreadExecutionState。这是用于通知 Windows 您的应用程序处于活动状态这一事实的 API:

使应用程序能够通知
系统正在使用它,从而
阻止系统进入
睡眠或关闭显示器时
应用程序正在运行。

, 和

多媒体应用程序,例如视频
播放器和演示应用程序,
必须使用 ES_DISPLAY_REQUIRED 时
长时间显示视频
无需用户输入

如果您无法控制应用程序,但屏幕保护程序启动会导致问题,则将此信息推送回开发人员。

禁用屏幕保护程序几乎总是是错误的解决方案,因为它会影响用户的整个体验,而不仅仅是在应用程序运行时。

If you're writing the application yourself, look into calling the unmanaged API SetThreadExecutionState (PInvoke reference). Copying from my answer to how do i prevent screen-savers and sleeps during my program execution?:

Don't mess with the screensaver settings, use SetThreadExecutionState. This is the API for informing windows on the fact that your application is active:

Enables an application to inform the
system that it is in use, thereby
preventing the system from entering
sleep or turning off the display while
the application is running.

, and

Multimedia applications, such as video
players and presentation applications,
must use ES_DISPLAY_REQUIRED when they
display video for long periods of time
without user input

If you don't have control over the application, but the screen saver kicking in causes problems, then push this information back to the developers.

Disabling the screensaver is almost always the wrong solution to the problem, since it affects the users whole experience, not just when the application is running.

羁〃客ぐ 2024-10-25 12:53:49

用 C# 控制屏幕保护程序

public static class ScreenSaver
{
   // Signatures for unmanaged calls

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool SystemParametersInfo( 
      int uAction, int uParam, ref int lpvParam, 
      int flags );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool SystemParametersInfo( 
      int uAction, int uParam, ref bool lpvParam, 
      int flags );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern int PostMessage( IntPtr hWnd, 
      int wMsg, int wParam, int lParam );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern IntPtr OpenDesktop( 
      string hDesktop, int Flags, bool Inherit, 
      uint DesiredAccess );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool CloseDesktop( 
      IntPtr hDesktop );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool EnumDesktopWindows( 
      IntPtr hDesktop, EnumDesktopWindowsProc callback, 
      IntPtr lParam );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool IsWindowVisible( 
      IntPtr hWnd );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   public static extern IntPtr GetForegroundWindow( );

   // Callbacks

   private delegate bool EnumDesktopWindowsProc( 
      IntPtr hDesktop, IntPtr lParam );

   // Constants

   private const int SPI_GETSCREENSAVERACTIVE = 16;
   private const int SPI_SETSCREENSAVERACTIVE = 17;
   private const int SPI_GETSCREENSAVERTIMEOUT = 14;
   private const int SPI_SETSCREENSAVERTIMEOUT = 15;
   private const int SPI_GETSCREENSAVERRUNNING = 114;
   private const int SPIF_SENDWININICHANGE = 2;

   private const uint DESKTOP_WRITEOBJECTS = 0x0080;
   private const uint DESKTOP_READOBJECTS = 0x0001;
   private const int WM_CLOSE = 16;


   // Returns TRUE if the screen saver is active 

   // (enabled, but not necessarily running).

   public static bool GetScreenSaverActive( )
   {
      bool isActive = false;

      SystemParametersInfo( SPI_GETSCREENSAVERACTIVE, 0, 
         ref isActive, 0 );
      return isActive;
   }

   // Pass in TRUE(1) to activate or FALSE(0) to deactivate

   // the screen saver.

   public static void SetScreenSaverActive( int Active )
   {
      int nullVar = 0;

      SystemParametersInfo( SPI_SETSCREENSAVERACTIVE, 
         Active, ref nullVar, SPIF_SENDWININICHANGE );
   }

   // Returns the screen saver timeout setting, in seconds

   public static Int32 GetScreenSaverTimeout( )
   {
      Int32 value = 0;

      SystemParametersInfo( SPI_GETSCREENSAVERTIMEOUT, 0, 
         ref value, 0 );
      return value;
   }

   // Pass in the number of seconds to set the screen saver

   // timeout value.

   public static void SetScreenSaverTimeout( Int32 Value )
   {
      int nullVar = 0;

      SystemParametersInfo( SPI_SETSCREENSAVERTIMEOUT, 
         Value, ref nullVar, SPIF_SENDWININICHANGE );
   }

   // Returns TRUE if the screen saver is actually running

   public static bool GetScreenSaverRunning( )
   {
      bool isRunning = false;

      SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0, 
         ref isRunning, 0 );
      return isRunning;
   }

   // From Microsoft's Knowledge Base article #140723: 

   // http://support.microsoft.com/kb/140723

   // "How to force a screen saver to close once started 

   // in Windows NT, Windows 2000, and Windows Server 2003"


   public static void KillScreenSaver( )
   {
      IntPtr hDesktop = OpenDesktop( "Screen-saver", 0, 
         false,DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
      if( hDesktop != IntPtr.Zero )
      {
         EnumDesktopWindows( hDesktop, new 
            EnumDesktopWindowsProc( KillScreenSaverFunc ),
            IntPtr.Zero );
         CloseDesktop( hDesktop );
      }
      else
      {
         PostMessage( GetForegroundWindow( ), WM_CLOSE, 
            0, 0 );
      }
   }

   private static bool KillScreenSaverFunc( IntPtr hWnd, 
      IntPtr lParam )
   {
      if( IsWindowVisible( hWnd ) )
         PostMessage( hWnd, WM_CLOSE, 0, 0 );
      return true;
   }
}

KillScreenSaver( )

private void KillTimer_Elapsed( object state )
{
   // Toggle kill state to indicate activity

   killState = ( killState == 1 ) ? 0 : 1;
   this.SetText( killState.ToString( ) );

   // Stop the screen saver if it's active and running, 

   // otherwise reset the screen saver timer.

   // Apparently it's possible for GetScreenSaverRunning()

   // to return TRUE before the screen saver has time to 

   // actually become the foreground application. So...

   // Make sure we're not the foreground window to avoid 

   // killing ourself.


   if( ScreenSaver.GetScreenSaverActive( ) )
   {
      if( ScreenSaver.GetScreenSaverRunning( ) )
      {
         if( ScreenSaver.GetForegroundWindow() != hThisWnd)
            ScreenSaver.KillScreenSaver( );
      }
      else
      {
         // Reset the screen saver timer, so the screen 

         // saver doesn't turn on until after a full

         // timeout period. If killPeriod is less than 

         // ssTimeout the screen saver should never 

         // activate.

         ScreenSaver.SetScreenSaverActive( TRUE );
      }
   }
}

Controlling The Screen Saver With C#

public static class ScreenSaver
{
   // Signatures for unmanaged calls

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool SystemParametersInfo( 
      int uAction, int uParam, ref int lpvParam, 
      int flags );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool SystemParametersInfo( 
      int uAction, int uParam, ref bool lpvParam, 
      int flags );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern int PostMessage( IntPtr hWnd, 
      int wMsg, int wParam, int lParam );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern IntPtr OpenDesktop( 
      string hDesktop, int Flags, bool Inherit, 
      uint DesiredAccess );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool CloseDesktop( 
      IntPtr hDesktop );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool EnumDesktopWindows( 
      IntPtr hDesktop, EnumDesktopWindowsProc callback, 
      IntPtr lParam );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   private static extern bool IsWindowVisible( 
      IntPtr hWnd );

   [DllImport( "user32.dll", CharSet = CharSet.Auto )]
   public static extern IntPtr GetForegroundWindow( );

   // Callbacks

   private delegate bool EnumDesktopWindowsProc( 
      IntPtr hDesktop, IntPtr lParam );

   // Constants

   private const int SPI_GETSCREENSAVERACTIVE = 16;
   private const int SPI_SETSCREENSAVERACTIVE = 17;
   private const int SPI_GETSCREENSAVERTIMEOUT = 14;
   private const int SPI_SETSCREENSAVERTIMEOUT = 15;
   private const int SPI_GETSCREENSAVERRUNNING = 114;
   private const int SPIF_SENDWININICHANGE = 2;

   private const uint DESKTOP_WRITEOBJECTS = 0x0080;
   private const uint DESKTOP_READOBJECTS = 0x0001;
   private const int WM_CLOSE = 16;


   // Returns TRUE if the screen saver is active 

   // (enabled, but not necessarily running).

   public static bool GetScreenSaverActive( )
   {
      bool isActive = false;

      SystemParametersInfo( SPI_GETSCREENSAVERACTIVE, 0, 
         ref isActive, 0 );
      return isActive;
   }

   // Pass in TRUE(1) to activate or FALSE(0) to deactivate

   // the screen saver.

   public static void SetScreenSaverActive( int Active )
   {
      int nullVar = 0;

      SystemParametersInfo( SPI_SETSCREENSAVERACTIVE, 
         Active, ref nullVar, SPIF_SENDWININICHANGE );
   }

   // Returns the screen saver timeout setting, in seconds

   public static Int32 GetScreenSaverTimeout( )
   {
      Int32 value = 0;

      SystemParametersInfo( SPI_GETSCREENSAVERTIMEOUT, 0, 
         ref value, 0 );
      return value;
   }

   // Pass in the number of seconds to set the screen saver

   // timeout value.

   public static void SetScreenSaverTimeout( Int32 Value )
   {
      int nullVar = 0;

      SystemParametersInfo( SPI_SETSCREENSAVERTIMEOUT, 
         Value, ref nullVar, SPIF_SENDWININICHANGE );
   }

   // Returns TRUE if the screen saver is actually running

   public static bool GetScreenSaverRunning( )
   {
      bool isRunning = false;

      SystemParametersInfo( SPI_GETSCREENSAVERRUNNING, 0, 
         ref isRunning, 0 );
      return isRunning;
   }

   // From Microsoft's Knowledge Base article #140723: 

   // http://support.microsoft.com/kb/140723

   // "How to force a screen saver to close once started 

   // in Windows NT, Windows 2000, and Windows Server 2003"


   public static void KillScreenSaver( )
   {
      IntPtr hDesktop = OpenDesktop( "Screen-saver", 0, 
         false,DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
      if( hDesktop != IntPtr.Zero )
      {
         EnumDesktopWindows( hDesktop, new 
            EnumDesktopWindowsProc( KillScreenSaverFunc ),
            IntPtr.Zero );
         CloseDesktop( hDesktop );
      }
      else
      {
         PostMessage( GetForegroundWindow( ), WM_CLOSE, 
            0, 0 );
      }
   }

   private static bool KillScreenSaverFunc( IntPtr hWnd, 
      IntPtr lParam )
   {
      if( IsWindowVisible( hWnd ) )
         PostMessage( hWnd, WM_CLOSE, 0, 0 );
      return true;
   }
}

KillScreenSaver( )

private void KillTimer_Elapsed( object state )
{
   // Toggle kill state to indicate activity

   killState = ( killState == 1 ) ? 0 : 1;
   this.SetText( killState.ToString( ) );

   // Stop the screen saver if it's active and running, 

   // otherwise reset the screen saver timer.

   // Apparently it's possible for GetScreenSaverRunning()

   // to return TRUE before the screen saver has time to 

   // actually become the foreground application. So...

   // Make sure we're not the foreground window to avoid 

   // killing ourself.


   if( ScreenSaver.GetScreenSaverActive( ) )
   {
      if( ScreenSaver.GetScreenSaverRunning( ) )
      {
         if( ScreenSaver.GetForegroundWindow() != hThisWnd)
            ScreenSaver.KillScreenSaver( );
      }
      else
      {
         // Reset the screen saver timer, so the screen 

         // saver doesn't turn on until after a full

         // timeout period. If killPeriod is less than 

         // ssTimeout the screen saver should never 

         // activate.

         ScreenSaver.SetScreenSaverActive( TRUE );
      }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文