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

作者:work24 时间:2023-03-14 21:41:24 

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

标签:C#,ScrollLock
0
投稿

猜你喜欢

  • C#中多态、重载、重写区别分析

    2022-06-27 22:49:55
  • Java ArrayList实现删除指定位置的元素

    2023-11-25 05:34:13
  • Android使用Intent传递组件大数据

    2023-09-30 12:21:46
  • 详解记录Java Log的几种方式

    2022-11-10 16:38:19
  • C#生成word记录实例解析

    2023-09-10 00:17:33
  • C#读取csv格式文件的方法

    2023-08-28 22:38:46
  • 浅谈Java内存区域与对象创建过程

    2022-08-31 17:56:25
  • Java反射获取实例的速度对比分析

    2021-06-14 09:22:29
  • 解决MyEclipse中的Building workspace问题的三个方法

    2023-10-25 09:00:51
  • Java用BigDecimal类解决Double类型精度丢失的问题

    2022-01-07 16:49:24
  • MybatisPlus #{param}和${param}的用法详解

    2023-02-02 13:08:10
  • SpringMvc框架的简介与执行流程详解

    2022-10-15 18:49:00
  • Android Build类的详解及简单实例

    2023-07-02 21:12:45
  • SpringBoot集成Redis的实现示例

    2022-10-22 14:50:33
  • Android自定义View之自定义评价打分控件RatingBar实现自定义星星大小和间距

    2023-03-14 04:30:16
  • Java实现的Base64加密算法示例

    2023-10-29 00:37:08
  • 浅谈JAVA如何生成UUID唯一标识

    2023-08-12 20:45:03
  • Java中的字符串常量池详细介绍

    2023-03-08 09:16:41
  • Java 阻塞队列和线程池原理分析

    2022-04-19 11:57:45
  • WinForm中KeyDown,KeyPress和KeyUp的顺序与区别解析

    2023-06-30 22:34:36
  • asp之家 软件编程 m.aspxhome.com