请问下C#控制台命令程序如何实现Control + V粘贴功能 请问下C#控制台命令程序如何实现Control + V粘贴功能突然发现没分了T。TC#控制台 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } }} 【开源】博客导出工具(支持M个博客N个格式) 请问,这个正则表达式该怎么写? 请求帮助!关于在Form1上画图怎么保存以及最小化不消失问题! C# 在word指定位置插入图面 求代码。。 FTP PORT实现遇到一个问题 请高手指点 内附代码 请问怎么把文件里里面的内容读出来 打印预览某个iframe问题???? 大家看看.... 关于WCF服务 学习看板制作 在线等,关于MSMQ接收消息的问题 100分 关于键值对遇到的问题
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;
}
}
}