/* GUIBEGIN WINDOW , 133, 377, 197, 50, POPUP|CAPTION|SYSMENU|MINBOX|MAXBOX|THICK, , Clipboard Exchange FONT 8, 400, MS Shell Dlg PUSH 42, 9, 50, 14, TABSTOP, , SendButton, , Send text PUSH 103, 9, 50, 14, TABSTOP, , ReceiveButton, , Receive text DEND GUIEND */ OPTIONS "C_CALL LABELCHECK WINFUNC NOSOURCE" LIBRARY rexxgui DO /* Register the Windows OS function OpenClipboard() */ FUNCDEF("OpenClipboard", "32u, void", "user32") /* Register the Windows OS function GetClipboardData() */ FUNCDEF("GetClipboardData", "void, 32u", "user32") /* Register the Windows OS function CloseClipboard() */ FUNCDEF("CloseClipboard", "32u", "user32") /* Register the Windows OS function EmptyClipboard() */ FUNCDEF("EmptyClipboard", "32u", "user32") /* Register the Windows OS function SetClipboardData() */ FUNCDEF("SetClipboardData", "void, 32u, void", "user32") /* Register the Windows OS function GlobalLock() */ FUNCDEF("GlobalLock", "void, void", "kernel32") /* Register the Windows OS function GlobalUnlock() */ FUNCDEF("GlobalUnlock", "32u, void", "kernel32") /* Register the Windows OS function GlobalAlloc() to allocate memory. */ FUNCDEF("GlobalAlloc", "void, 32u, 32u", "kernel32") /* Register the Windows OS function GlobalFree() to free memory. */ FUNCDEF("GlobalFree", "void, void", "kernel32") CATCH FAILURE CONDITION("M") RETURN END GuiErr = "SYNTAX" GuiHeading = 1 GuiCreateWindow('NORMAL') /* Initialize our count to 1 */ count = 1 Again: DO FOREVER GuiGetMsg() CATCH SYNTAX CONDITION('M') SIGNAL Again CATCH HALT FINALLY GuiDestroyWindow() END RETURN /* Called when the user clicks the "Send text" button. */ WM_CLICK_SendButton: /* Let's create a "Test xxx" string where xxx is an incrementing 'count' */ SendClip("Test " || count) count = count + 1 IF count = 10 THEN count = 1 RETURN /* Called when the user clicks the "Receive text" button. */ WM_CLICK_ReceiveButton: clip = ReceiveClip() IF clip \== "" THEN GuiSay(clip) RETURN /* Returns some text that has been placed upon the Windows clipboard. */ ReceiveClip: PROCEDURE DO /* Assume no text */ clip = "" /* Open the clipboard */ err = OpenClipboard(0) IF err == 1 THEN DO /* See if there is text in the clipboard and get * a handle to it. */ hData = GetClipboardData(1) IF hData \== 0 THEN DO /* Lock that text down */ str = GlobalLock(hData) IF str \== 0 THEN DO /* Convert to a format that REXX can use */ IF CONVERTDATA(str, 'clip', 'str') == "" THEN GuiSay("Conversion error!") END ELSE GuiSay("Can't lock down text!") END ELSE GuiSay("There's no text on the clipboard!") END ELSE GuiSay("Can't open clipboard!") FINALLY /* Free up some resources */ IF EXISTS(str) THEN GlobalUnlock(hData) IF err == 1 THEN CloseClipboard() END /* Return the text */ RETURN clip /* Places the passed argument upon the Windows clipboard. */ SendClip: PROCEDURE USE ARG text DO /* Open the clipboard */ err = OpenClipboard(0) IF err == 1 THEN DO /* Remove any current text */ EmptyClipboard() /* We need to allocate new memory (of the type GMEM_MOVEABLE + GMEM_DDESHARE) * and copy the text (nul-terminated), in order to give it to the clipboard */ data = GlobalAlloc(8194, LENGTH(text) + 1) IF data \== 0 THEN DO /* Lock that text down */ str = GlobalLock(data) IF str \== 0 THEN DO /* Convert/copy the text to the memory buffer */ IF CONVERTDATA(str, 'text', 'str', 'FROM') \== "" THEN DO /* Place text on the clipboard, and if not an error, then don't free the memory */ IF SetClipboardData(1, str) \== 0 THEN DROP data ELSE GuiSay("Error placing text on the clipboard!") END ELSE GuiSay("Conversion error!") END ELSE GuiSay("Error locking text!") END ELSE GuiSay("Can't get memory!") END ELSE GuiSay("Can't open clipboard!") FINALLY /* Free up some resources */ IF EXISTS(data) & data \== 0 THEN GlobalFree(data) IF err == 1 THEN CloseClipboard() END RETURN