欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#读取或设置ScrollLock状态的方法

程序员文章站 2022-06-14 17:26:08
本文实例讲述了c#读取或设置scrolllock状态的方法。分享给大家供大家参考。具体如下: c#读取或者设置scrolllock状态,允许滚动状态读取或者设置...

本文实例讲述了c#读取或设置scrolllock状态的方法。分享给大家供大家参考。具体如下:

c#读取或者设置scrolllock状态,允许滚动状态读取或者设置

using system;
using system.runtime.interopservices;
using system.windows.forms;
namespace robvanderwoude
{
 public class scrolllock
 {
  [dllimport( "user32.dll" )]
  static extern void keybd_event(byte bvk, byte bscan, uint dwflags, uintptr dwextrainfo);
  const int keyeventf_extendedkey = 0x1;
  const int keyeventf_keyup = 0x2;
  public static int main(string[] args)
  {
   try
   {
    bool scrolllock;
    bool newstate = false;
    bool setstate = false;
    bool setverbose = false;
    bool verbose = true;
    if ( args.length > 2 )
    {
     return writeerror("invalid command line argument(s).");
    }
    else
    {
     foreach ( string arg in args )
     {
      switch ( arg.toupper( ) )
      {
       case "/?":
       case "-h":
       case "/help":
       case "-help":
       case "--help":
        return writeerror(string.empty);
       case "/q":
       case "-q":
       case "/quiet":
       case "-quiet":
       case "--quiet":
        if ( setverbose )
        {
         return writeerror("duplicate switch not allowed.");
        }
        setverbose = true;
        verbose = false;
        break;
       case "/v":
       case "-v":
       case "/verbose":
       case "-verbose":
       case "--verbose":
        if ( setverbose )
        {
         return writeerror("duplicate switch not allowed.");
        }
        setverbose = true;
        verbose = true;
        break;
       case "0":
       case "off":
        if ( setstate )
        {
         return writeerror("duplicate argument not allowed.");
        }
        setstate = true;
        newstate = false;
        break;
       case "1":
       case "on":
        if ( setstate )
        {
         return writeerror("duplicate argument not allowed.");
        }
        setstate = true;
        newstate = true;
        break;
       default:
        return writeerror("invalid command line argument "+arg);
      }
     }
    }
    if ( control.iskeylocked( keys.scroll ) )
    {
     scrolllock = true;
     if ( setstate && !newstate )
     {
      keybd_event(0x91, 0x45, keyeventf_extendedkey, (uintptr) 0);
      keybd_event(0x91, 0x45, keyeventf_extendedkey | keyeventf_keyup, (uintptr) 0);
     }
    }
    else
    {
     scrolllock = false;
     if ( setstate && newstate )
     {
      keybd_event(0x91,0x45,keyeventf_extendedkey,(uintptr) 1);
      keybd_event(0x91,0x45,keyeventf_extendedkey | keyeventf_keyup,(uintptr) 1);
     }
    }
    scrolllock = control.iskeylocked( keys.scroll );
    if ( verbose )
    {
     console.write( "scrolllock = " );
     console.foregroundcolor = consolecolor.white;
     console.writeline( ( scrolllock ? "on" : "off" ) );
     console.resetcolor( );
    }
    return ( scrolllock ? 2 : 0 );
   }
   catch ( exception e )
   {
    return writeerror( e );
   }
  }
  public static int writeerror( exception e )
  {
   return writeerror( e == null ? null : e.message );
  }
  public static int writeerror( string errormessage )
  {
   if ( string.isnullorempty( errormessage ) == false )
   {
    console.error.writeline( );
    console.foregroundcolor = consolecolor.red;
    console.error.write( "error: " );
    console.foregroundcolor = consolecolor.white;
    console.error.writeline( errormessage );
    console.resetcolor( );
   }
   console.error.writeline( );
   console.error.writeline( "scrolllock, version 1.01" );
   console.error.writeline( "read or set scrolllock key state" );
   console.error.writeline( );
   console.error.write( "usage: " );
   console.foregroundcolor = consolecolor.white;
   console.error.writeline("scrolllock [ 0 | 1 | on | off ] [ /quiet | /verbose ]");
   console.resetcolor( );
   console.error.writeline( );
   console.error.write( "where: " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "0" );
   console.resetcolor( );
   console.error.write( " or " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "off" );
   console.resetcolor( );
   console.error.writeline( "  set scrolllock off" );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "    1" );
   console.resetcolor( );
   console.error.write( " or " );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "on" );
   console.resetcolor( );
   console.error.writeline( "  set scrolllock on" );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "    /q" );
   console.resetcolor( );
   console.error.writeline( "uiet   won't display anything on screen" );
   console.foregroundcolor = consolecolor.white;
   console.error.write( "    /v" );
   console.resetcolor( );
   console.error.writeline("erbose displays the (new) scrolllock key state on screen (default)");
   console.error.writeline();
   console.error.writeline("notes: an \"errorlevel\" 0 is returned if scrolllock is off, 2 if");
   console.error.writeline(" scrolllock is on, or 1 in case of (command line) errors.");
   console.error.writeline();
   console.error.writeline("written by rob van der woude");
   return 1;
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。