Last active
November 18, 2019 12:18
-
-
Save orange-in-space/564c7f839da641fa1c084a78ec29dc98 to your computer and use it in GitHub Desktop.
C# コンソールアプリでwin32apiでクリップボードにテキスト送るやつ><(間違ってたらごめんなさい><;)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace ConsoleClipboard | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string text = "てすと!><"; | |
SimpleClipboardSetText(text); | |
} | |
//クリップボードにテキスト送るやつ | |
private static void SimpleClipboardSetText(string text) | |
{ | |
string nullTerminatedStr = text + "\0"; | |
byte[] str = System.Text.Encoding.Unicode.GetBytes(nullTerminatedStr); | |
IntPtr hglobal = Marshal.AllocHGlobal(str.Length); | |
Marshal.Copy(str, 0, hglobal, str.Length); | |
OpenClipboard(IntPtr.Zero); // 0でもいいらしい・・・?>< | |
EmptyClipboard(); | |
SetClipboardData(CF_UNICODETEXT, hglobal); | |
CloseClipboard(); | |
Marshal.FreeHGlobal(hglobal); | |
} | |
// | |
private const uint CF_UNICODETEXT = 13; | |
[DllImport("user32.dll")] | |
static extern bool OpenClipboard(IntPtr hWndNewOwner); | |
[DllImport("user32.dll")] | |
static extern bool EmptyClipboard(); | |
[DllImport("user32.dll")] | |
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem); | |
[DllImport("user32.dll")] | |
static extern bool CloseClipboard(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment