概述
[DllImport("kernel32.dll")]
public static extern IntPtr GlobalSize(IntPtr handle);
[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);
[DllImport("user32.dll")]
static extern IntPtr GetClipboardData(uint uFormat);
[DllImport("user32.dll", SetLastError = true)]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll", SetLastError = true)]
static extern bool CloseClipboard();
const uint CF_TEXT = 1;
const uint CF_UNICODETEXT = 13;
const uint CF_OEMTEXT = 7;
/// <summary>
/// Gets the data on the clipboard in the format specified by
/// </summary>
public static string GetClipboardData()
{
OpenClipboard(IntPtr.Zero);
//Get pointer to clipboard data in the selected format
IntPtr ClipboardDataPointer = GetClipboardData(CF_TEXT);
//Do a bunch of crap necessary to copy the data from the memory
//the above pointer points at to a place we can access it.
IntPtr Length = GlobalSize(ClipboardDataPointer);
IntPtr gLock = GlobalLock(ClipboardDataPointer);
//Init a buffer which will contain the clipboard data
byte[] Buffer = new byte[(int)Length];
//Copy clipboard data to buffer
Marshal.Copy(gLock, Buffer, 0, (int)Length);
CloseClipboard();
return System.Text.UnicodeEncoding.Default.GetString(Buffer);
}
最后
以上就是洁净鸵鸟为你收集整理的WIN32API获取剪切板文本的全部内容,希望文章能够帮你解决WIN32API获取剪切板文本所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复