Re: How to write graphics to MS Windows clipboard

new topic     » goto parent     » topic index » view thread      » older message » newer message

Andrew Katz wrote:
> 
> Does win32Lib support the writing of graphics to MS Windows clipboard?
> 
> In the documentation, I saw some functions dealing with DIBs, and I saw some
> functions dealing with text to the clipboard.
> 
> I am looking for a way for my program, which generates graphics, to copy it
> so that Paint can paste it.
> 
> Andy Katz
> 
> B.S. Computer Science, 1978
> Rensselaer Polytechnic Institute (RPI)

I assume your graphics are displayed on some window.

include win32lib.ew
--  first a simplified version of Guillermo's code
constant
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),
xGetWindowDC        =
    define_c_func(user32,"GetWindowDC",{C_POINTER},C_POINTER)


-- hBmp must be a bitmap handle

integer void
global procedure bmpToClipboard(atom hBmp)
        if not c_func(cOpenClipboard, {NULL}) then
void=message_box("Failed to open
            clipboard.","Error",MB_ICONERROR+MB_OK)
            return
	end if
        -- current thread now has exclusive access to clipboard
	c_proc(cEmptyClipboard,{})
	if c_func(cSetClipboardData,{CF_BITMAP,hBmp}) != hBmp then
void=message_box("Failed to copy bitmap to
clipboard.","Error",MB_ICONERROR+MB_OK)
	end if
	c_proc(cCloseClipboard,{})
end procedure

global procedure captureToClipboard(integer id,integer x,integer y,integer
cx,integer cy,integer client)
-- id is the surce window; copy rectangle at {x,y}, cx pixel wide and cy poxel
high
-- if client is true, x and y are client coordinates, else absolute window
coordinates
atom hDC,hMemDC,hOldBmp,hBmp,hWnd
-- where's the pic?
hWnd=getHandle(id)
if client then
    hDC=getDC(id)
else
    hDC=c_func(xGetWindowDC,{hWnd})
end if
        if not hDC then
            void=message_box("Not a valid window.","Error",MB_ICONERROR+MB_OK)
            return
        end if
-- create a memory target DC
hMemDC=w32Func(xCreateCompatiblzDC,{hDC})
        if not hMemDC then
void=message_box("Failed to create temporary
            bitmap.","Error",MB_ICONERROR+MB_OK)
            return
        end if
-- create the bitmap we'll send to clipboard, with supplied dimensions.
-- It will be written to using hMemDC.
hBmp=w32Func(xCreateCompatibleBitmap,{hMemDC,cx,cy})
        if not hBmp then
void=message_box("Failed to create temporary
            bitmap.","Error",MB_ICONERROR+MB_OK)
            return
        end if
-- use hMemDC o write to it.
hOldBmp=w32Func(xSelectObject,{hMemDC,hBmp})
-- now copy source rectangle to it
bitBlt(hMemDC,0,0,hDC,x,y,cx,cy,SrcCopy)
-- deselect the bitmap so as to own it
hBmp=w32Func(xSelectObject,{hMemDC,hOldBmp})
-- send bitmap to clipboard
bmpToClipBoard(id,hBmp)
-- now we no longer own it
-- clean up
deleteObject(hOldBmp)
hMemDC=w32Func(xDeleteDC,{hMemDC})
hDC=w32Func(xReleaseDC,{hWnd,hDC})
end procedure

Didn't test it; part of cleanup may be redundant.

CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu