欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-03-19 09:44:01|栏目:.NET代码|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:jQuery结合C#实现上传文件的方法

栏    目:.NET代码

下一篇:Winform窗口实现多显示屏显示的2种方法

本文标题:C#读取或设置ScrollLock状态的方法

本文地址:http://www.codeinn.net/misctech/83561.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有