Clipboard persistence
Posted: Mon Jul 23, 2018 3:47 pm
Hi,
I am trying to add cut/copy/paste to my program...which is going to be interesting for macOS and Linux/Qt, certainly. I'm not even sure I'll be able to do what I need to with Qt, but anyway, I digress.
On Windows, I am able to copy a structure to the clipboard and paste it back. However, if you try to paste when the program is restarted, it crashes. How can I ensure the data remains on the clipboard after the program ends, or at least ensure the pasted data is valid so it doesn't crash?
(Clipboard functions by Danilo: viewtopic.php?p=395527#p395527)
I am trying to add cut/copy/paste to my program...which is going to be interesting for macOS and Linux/Qt, certainly. I'm not even sure I'll be able to do what I need to with Qt, but anyway, I digress.
On Windows, I am able to copy a structure to the clipboard and paste it back. However, if you try to paste when the program is restarted, it crashes. How can I ensure the data remains on the clipboard after the program ends, or at least ensure the pasted data is valid so it doesn't crash?
(Clipboard functions by Danilo: viewtopic.php?p=395527#p395527)
Code: Select all
Procedure SetClipboardData(Type.s, *theData, theDataLen)
If OpenClipboard_(0)
format = RegisterClipboardFormat_(@Type)
If format
hMem = GlobalAlloc_(#GMEM_MOVEABLE|#GMEM_DDESHARE,theDataLen)
If hMem
mem = GlobalLock_(hMem)
If mem
CopyMemory(*theData,mem,theDataLen)
If SetClipboardData_(format,hMem)
result = format
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
EndIf
CloseClipboard_()
EndIf
ProcedureReturn result
EndProcedure
Procedure GetClipboardFormat(Type.s)
ProcedureReturn RegisterClipboardFormat_(@Type)
EndProcedure
Procedure GetClipboardData(format)
If OpenClipboard_(0)
hMem = GetClipboardData_(format)
If hMem
dataSize = GlobalSize_(hMem)
src = GlobalLock_(hMem)
If src
mem = AllocateMemory( dataSize )
If mem
CopyMemory(src,mem,dataSize)
EndIf
GlobalUnlock_(hMem)
EndIf
EndIf
CloseClipboard_()
EndIf
ProcedureReturn mem
EndProcedure
Structure test
name.s
EndStructure
format = GetClipboardFormat("MyClipboardFormat")
OpenWindow(0, 0, 0, 500, 400, "", #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 96, 25, "Copy")
ButtonGadget(1, 10, 40, 96, 25, "Paste")
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Gadget
Select EventGadget()
Case 0
Define *data.Test = AllocateStructure(Test)
*data\name = "Hello"
SetClipboardData("MyClipboardFormat", *data, SizeOf(Test))
Case 1
If OpenClipboard_(0)
If IsClipboardFormatAvailable_(format)
Define *clipData.Test = GetClipboardData(format)
Debug *clipData\name
EndIf
EndIf
EndSelect
EndSelect
Until event = #PB_Event_CloseWindow