Created
February 7, 2014 11:49
-
-
Save whiteleaf7/8861339 to your computer and use it in GitHub Desktop.
Rubyでクリップボードの読み込み・書き込み
This file contains 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
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2014 whiteleaf. All rights reserved. | |
# | |
require_relative "winapi" | |
module Clipboard | |
class NotOpenClipboardError < StandardError; end | |
def set_data(text) | |
if WinAPI.OpenClipboard(0) == 0 | |
raise NotOpenClipboardError, "don't open clipboard" | |
end | |
WinAPI.EmptyClipboard | |
buffer_size = text.bytesize + 10 | |
hg = WinAPI.GlobalAlloc(WinAPI::GHND | WinAPI::GMEM_SHARE, buffer_size) | |
str_mem = WinAPI.GlobalLock(hg) | |
WinAPI.memcpy_s(str_mem, buffer_size, text + "\0", text.bytesize + 1) | |
WinAPI.GlobalUnlock(hg) | |
WinAPI.SetClipboardData(WinAPI::CF_TEXT, hg) | |
WinAPI.CloseClipboard | |
end | |
def get_data | |
if WinAPI.IsClipboardFormatAvailable(WinAPI::CF_TEXT) == 0 | |
return nil | |
end | |
if WinAPI.OpenClipboard(0) == 0 | |
raise NotOpenClipboardError, "don't open clipboard" | |
end | |
hg = WinAPI.GetClipboardData(WinAPI::CF_TEXT) | |
buffer_size = WinAPI.GlobalSize(hg) | |
buffer = " " * buffer_size | |
WinAPI.memcpy_s(buffer, buffer_size, WinAPI.GlobalLock(hg), buffer_size) | |
WinAPI.GlobalUnlock(hg) | |
WinAPI.CloseClipboard | |
buffer.gsub(/\0+\z/, "") | |
end | |
module_function :set_data, :get_data | |
end |
This file contains 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
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2014 whiteleaf. All rights reserved. | |
# | |
module WinAPI | |
begin | |
require "fiddle/import" | |
extend Fiddle::Importer | |
rescue LoadError | |
require "dl/importer" | |
extend DL::Importer | |
end | |
begin | |
dlload "msvcrt", "kernel32", "user32" | |
rescue DL::DLError | |
dlload "crtdll", "kernel32", "user32" | |
end | |
extern "unsigned long GlobalAlloc(unsigned int, unsigned long)" | |
extern "unsigned long GlobalSize(unsigned long)" | |
extern "unsigned long GlobalFree(unsigned long)" | |
extern "void* GlobalLock(unsigned long)" | |
extern "int GlobalUnlock(unsigned long)" | |
extern "int OpenClipboard(unsigned long)" | |
extern "int EmptyClipboard()" | |
extern "int CloseClipboard()" | |
extern "unsigned long SetClipboardData(unsigned int, unsigned long)" | |
extern "int IsClipboardFormatAvailable(unsigned int)" | |
extern "unsigned long GetClipboardData(unsigned int)" | |
extern "int GetSystemMetrics(int)" | |
extern "int memcpy_s(void*, size_t, void*, size_t)" | |
SWP_NOSIZE = 1 | |
SM_CXSCREEN = 0 | |
SM_CYSCREEN = 1 | |
SM_CXFULLSCREEN = 16 | |
SM_CYFULLSCREEN = 17 | |
GMEM_MOVEABLE = 2 | |
GMEM_ZEROINIT = 0x40 | |
GMEM_SHARE = 0x200 | |
GHND = GMEM_MOVEABLE | GMEM_ZEROINIT | |
CF_TEXT = 1 | |
CF_OEMTEXT = 7 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment