当前位置:编程学习 > C#/ASP.NET >>

请问下C#控制台命令程序如何实现Control + V粘贴功能

请问下C#控制台命令程序如何实现Control + V粘贴功能

突然发现没分了T。T C# 控制台 --------------------编程问答-------------------- control+v?
还是 ctrl+v? --------------------编程问答--------------------

using System;
using System.Runtime.InteropServices;

namespace clipboard
{
    class Program
    {
        public static void Main(string[] args)
        {
            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);
        }
    }

    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;

        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 = 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;
        }
    }
}
--------------------编程问答-------------------- 选中-右击-标记
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,