zoukankan      html  css  js  c++  java
  • C# 控制台程序实现 Ctrl + V 粘贴功能

      代码主要分为两部分,首先调用系统API注册剪切板相关的事件,然后监控用户的按键操作。完整代码如下:

       class ClipBoard
        {
            [DllImport("user32.dll", SetLastError = true)]
            private static extern Int32 IsClipboardFormatAvailable(uint format);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern Int32 OpenClipboard(IntPtr hWndNewOwner);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr GetClipboardData(uint uFormat);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern Int32 CloseClipboard();
    
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern Int32 GlobalLock(IntPtr hMem);
    
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern Int32 GlobalUnlock(IntPtr hMem);
    
            [DllImport("kernel32.dll")]
            public static extern UIntPtr GlobalSize(IntPtr hMem);
    
            const uint CF_TEXT = 1;
    
            /// <summary>
            /// 实现控制台自动粘贴板功能
            /// </summary>
            /// <returns></returns>
            public static string PasteTextFromClipboard()
            {
                string result = "";
                if (IsClipboardFormatAvailable(CF_TEXT) == 0)
                {
                    return result;
                }
                if (OpenClipboard((IntPtr)0) == 0)
                {
                    return result;
                }
    
                IntPtr hglb = GetClipboardData(CF_TEXT);
                if (hglb != (IntPtr)0)
                {
                    UIntPtr size = GlobalSize(hglb);
                    IntPtr s = (IntPtr)GlobalLock(hglb);
                    byte[] buffer = new byte[(int)size];
                    Marshal.Copy(s, buffer, 0, (int)size);
                    if (s != null)
                    {
                        result = ASCIIEncoding.ASCII.GetString(buffer);
                        GlobalUnlock(hglb);
                    }
                }
    
                CloseClipboard();
                return result;
            }
    
            /// <summary>
            /// 监控用户输入的按键
            /// </summary>
            public void KeyPress()
            {
                ConsoleKeyInfo ki = Console.ReadKey(true);
                if ((ki.Key == ConsoleKey.V) && (ki.Modifiers == ConsoleModifiers.Control))
                {
                    Console.WriteLine("Ctrl+V pressed");
                    string s = ClipBoard.PasteTextFromClipboard();
                    Console.WriteLine(s);
                }
    
                Console.Write("Press any key to continue . . . ");
                Console.ReadKey(true);
            }
        }
  • 相关阅读:
    题解-CF1375E Inversion SwapSort
    寒门再难出贵子
    js获取链接中的内容方法
    MySQL添加用户、删除用户、授权及撤销权限
    Ubuntu保存退出vim编辑器
    最全!Linux服务器下安装SVN,并添加SVN项目,自动更新项目文件到web目录
    php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别以及REQUEST_URI的讲解
    RESTful API 最佳实践----转载阮一峰
    PHP图像处理(GD库)
    nginx.conf配置
  • 原文地址:https://www.cnblogs.com/feiyuhuo/p/5077220.html
Copyright © 2011-2022 走看看