how to copy a string to the windows buffer

Started by Hardcoal, August 20, 2017, 18:10:37

Previous topic - Next topic

Hardcoal

hi.. i want to copy a string of characters to the windows buffer so i can do paste on external notepad
can anyone tell me how?
cheers
Code

Henri

Hi hc,

if you mean clipboard functionality, then there was a previous post by Grisu in the old forum, which I've slightly modified to work in NG also.
SuperStrict

Import MaxGui.Drivers

AppTitle = "Clpboard Example"

?Win32

Extern "Win32"

'?Not bmxng 'uncomment this and the ending ? when used in NG

Function OpenClipboard:Int(hwnd:Byte Ptr = Null)
Function CloseClipboard()
Function EmptyClipboard()
Function SetClipboardData(format:Int, hMem:Byte Ptr)
Function GetClipboardData:Byte Ptr(format:Int)
'Function GlobalFree(buf:Byte Ptr)
Function IsClipboardFormatAvailable:Int(format:Int)
Function GlobalAlloc:Byte Ptr(uflags:Int, bytes:Int)

'?  'uncomment this when used in NG

End Extern 


Function GetClipboardText:String()
If OpenClipboard(Null) And IsClipboardFormatAvailable(1)
Local str:String = String.FromCString(GetClipboardData(1))
CloseClipboard()
Return str
Else
Return Null
EndIf
End Function

Function SetClipboardText:Int(str:String)
If OpenClipboard(Null)
EmptyClipboard()
Local hbuf:Byte Ptr = GlobalAllocString(str)
SetClipboardData(1, hbuf)
CloseClipboard()
Return True
Else
Return False
EndIf
End Function

Function GlobalAllocString:Byte Ptr(text:String)

?Not bmxng
Local p:Byte Ptr = GlobalAlloc(0, text.length + 1)
?
?bmxng
Local p:Byte Ptr = GlobalAlloc(0, Size_T(text.length + 1))
?

For Local i:Int = 0 Until text.length
Assert text[i] > 0
p[i] = text[i]
Next
p[text.length+1] = 0
Return p
End Function

?

'EXAMPLE
Global window:TGadget = CreateWindow( AppTitle, 400, 100, 400, 160, Null, WINDOW_TITLEBAR|WINDOW_STATUS )

Global listbox:TGadget = CreateListBox( 0, 0, ClientWidth(window), ClientHeight(window), window )
SetGadgetLayout listbox, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED


AddGadgetItem listbox, "This is a Clipboard Test", False, 0, "Right click me!"
AddGadgetItem listbox, "Press Right Mouse to" , False , 1 ,"Right click me!"
AddGadgetItem listbox, "Copy the selected item", False, 2, "Right click me!"
AddGadgetItem listbox, "to the Clipboard - Win32 only!", False, -1, "Right click me!"

While WaitEvent()
' Print CurrentEvent.ToString()
Select EventID()
Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE;End
'ListBox Event(s)
'EventData() holds the index of the corresponding listbox item.
Case EVENT_GADGETSELECT
'SetStatusText window, "Selected Item Index: " + EventData()
Case EVENT_GADGETMENU
     ?win32
      SetClipboardText(GadgetItemText(Listbox , SelectedGadgetItem(Listbox) ))
SetStatusText window, "Copied item to Clipboard!" + EventData()
                Print "Text from Clipboard: " + GetClipboardText()
?
End Select
Wend



Only thing missing is the unicode support (and different formats). There is an example of this in the win32maxguiex.bmx .

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Hardcoal

#2
Thanks henry God bless you..
You have just made the world better :)

I just used it again ;) 12.10.19
Code