cscの作法 その508

# 概要
cscの作法、調べてみた。
githubでc#見つけたのでやってみた。

# 参考にしたページ

https://gist.github.com/emoacht/c75bab17a4584c77cf64


# サンプルコード

```
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;

public static class ClipboardHelper {
	[DllImport("User32.dll", SetLastError = true)]
	private static extern IntPtr GetOpenClipboardWindow();
	[DllImport("User32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool IsClipboardFormatAvailable(uint format);
	[DllImport("User32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool OpenClipboard(IntPtr hWndNewOwner);
	[DllImport("User32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool CloseClipboard();
	[DllImport("User32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool EmptyClipboard();
	[DllImport("User32.dll", SetLastError = true)]
	private static extern IntPtr GetClipboardData(uint uFormat);
	[DllImport("User32.dll", SetLastError = true)]
	private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
	[DllImport("Kernel32.dll", SetLastError = true)]
	private static extern IntPtr GlobalAlloc(uint uFlags, int dwBytes);
	[DllImport("Kernel32.dll", SetLastError = true)]
	private static extern IntPtr GlobalLock(IntPtr hMem);
	[DllImport("Kernel32.dll", SetLastError = true)]
	[return: MarshalAs(UnmanagedType.Bool)]
	private static extern bool GlobalUnlock(IntPtr hMem);
	[DllImport("Kernel32.dll", SetLastError = true)]
	private static extern int GlobalSize(IntPtr hMem);
	private const uint CF_TEXT = 1U;
	private const uint CF_BITMAP = 2U;
	private const uint CF_UNICODETEXT = 13U;
	private const uint GHND = 0x0042;
	public static bool IsFree() {
		return (GetOpenClipboardWindow() == IntPtr.Zero);
	}
	public static bool ContainsText() {
		return IsClipboardFormatAvailable(CF_UNICODETEXT);
	}
	public static string GetText() {
		if (!IsFree())
			return null;
		var data = GetData(CF_UNICODETEXT, IntPtr.Zero);
		if (data == null)
			return null;
		return Encoding.Unicode.GetString(data).Trim(char.MinValue);
	}
	private static byte[] GetData(uint format, IntPtr windowHandle) {
		try
		{
			if (!OpenClipboard(windowHandle))
				return null;
			var handle = GetClipboardData(format);
			if (handle == IntPtr.Zero)
				throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(GetClipboardData));
			try
			{
				var pointer = GlobalLock(handle);
				if (pointer == IntPtr.Zero)
					throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(GlobalLock));
				int size = GlobalSize(handle);
				if (size == 0)
					throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(GlobalSize));
				var data = new byte[size];
				Marshal.Copy(pointer, data, 0, size);
				return data;
			}
			finally
			{
				GlobalUnlock(handle);
			}
		}
		finally
		{
			CloseClipboard();
		}
	}
	public static bool SetText(string text) {
		if (!IsFree())
			return false;
		var data = Encoding.Unicode.GetBytes(text + char.MinValue);
		return SetData(CF_UNICODETEXT, IntPtr.Zero, data);
	}
	private static bool SetData(uint format, IntPtr windowHandle, byte[] data) {
		try
		{
			if (!OpenClipboard(windowHandle))
				return false;
			if (!EmptyClipboard())
				throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(EmptyClipboard));
			var handle = GlobalAlloc(GHND, data.Length);
			if (handle == IntPtr.Zero)
				throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(GlobalAlloc));
			try
			{
				var pointer = GlobalLock(handle);
				if (pointer == IntPtr.Zero)
					throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(GlobalLock));
				Marshal.Copy(data, 0, pointer, data.Length);
				if (SetClipboardData(format, handle) == IntPtr.Zero)
					throw new Win32Exception(Marshal.GetLastWin32Error(), "");//nameof(SetClipboardData));
				return true;
			}
			finally
			{
				GlobalUnlock(handle);
			}
		}
		finally
		{
			CloseClipboard();
		}
	}
	static void Main(string[] args) {
		string str = GetText();
		Console.WriteLine(str);
	}
}





```

以上。