Re: How to write graphics to MS Windows clipboard
- Posted by Guillermo Bonvehi <gbonvehi at gmail.com> May 15, 2007
- 740 views
I made a mini library, it's not tested and allows you to only paste DIB and BITMAPs. -- File: clipboard.ew
include dll.e include msgbox.e constant -- Borrowed from win32lib (file w32user.ew) user32 = open_dll("user32.dll"), cOpenClipboard = define_c_func(user32,"OpenClipboard",{C_POINTER},C_INT), cCloseClipboard = define_c_func(user32,"CloseClipboard",{},C_INT), cEmptyClipboard = define_c_func(user32,"EmptyClipboard",{},C_INT), cSetClipboardData = define_c_func(user32,"SetClipboardData",{C_UINT,C_POINTER},C_POINTER) -- End borrow atom IsClipboardOpen -- Clipboard handle -- window must be a valid handle to a window, from win32lib, use getHWND(window) to get the real window handle -- hDIB must be a handle to a dib, as a example, the value returned by createDIB or loadBitmapFromFile in win32lib global procedure ClipboardPasteDIB(atom window, atom hDIB) IsClipboardOpen = c_func(cOpenClipboard, {window}) if not IsClipboardOpen then if message_box("Failed to open clipboard.","Error",MB_ICONERROR+MB_OK) then end if end if if c_func(cEmptyClipboard,{}) then end if if c_func(cSetClipboardData,{CF_DIB,hDIB}) != hDIB then if c_func(cSetClipboardData,{CF_BITMAP,hDIB}) != hDIB then if message_box("Failed to paste DIB in clipboard.","Error",MB_ICONERROR+MB_OK) then end if end if end if if c_func(cCloseClipboard,{}) then end if end procedure
-- File: test.exw
include win32lib.ew include clipboard.ew constant win = create( Window, "Test Clipboard", 0, Default, Default, 400, 400, 0 ), btn = create(PushButton, "Copy to clipboard", win, 1, 1, 40, 30, 0) atom hBit hBit = loadBitmapFromFile("test.bmp")} -- Code borrowed from a win32lib example -- create a bitmap, and display it atom hBitmap sequence pixels, pal -- the pixels data pixels = { { 0,0,0,0 }, -- scan line 1 { 0,1,1,0 }, -- scan line 2 { 0,1,1,0 }, -- scan line 3 { 0,0,0,0 } } -- scan line 4 -- the pal data (color tuples) pal = { { 255, 0, 0 }, -- color 0 is bright red { 0, 0, 255 } } -- color 1 is bright blue -- create the DIB hBitmap = createDIB( {pal, pixels} ) -- End borrow procedure onClickbtn(integer s, integer e, sequence p) ClipboardPasteDIB(getHWND(win),hBitmap) -- ClipboardPasteDIB(getHWND(win),hBit) -- Create a test.bmp file to test this end procedure setHandler(btn,w32HClick,routine_id("onClickbtn")) WinMain(win,Normal)
Any suggestions, questions, just do it :) Best regards, Guillermo Bonvehi