MapBasic: how to copy a value to clipboard

382 views
Skip to first unread message

Maurice34

unread,
Feb 25, 2009, 5:24:13 PM2/25/09
to MapInfo-L
Hello
If possible, what is the way, inside MB, to copy to clipboard a value
(a character string) obtained during the program (in order to paste
this string somewhere, after the end of program) ??
Thanks

MattC

unread,
Feb 25, 2009, 6:12:07 PM2/25/09
to MapInfo-L
Rather than copy and paste, just declare a string variable and set the
value to whatever string you are interested in. Remember to keep
local/global vaiable scope in mind - will you need the string outside
of the subroutine in which in the string value is obtained from?



On Feb 25, 3:24 pm, Maurice34 <maurice.na...@agriculture.gouv.fr>
wrote:

Peter Horsbøll Møller

unread,
Feb 26, 2009, 1:43:10 AM2/26/09
to mapi...@googlegroups.com
Maurice,
 
This workaround could do the trick - at least it works thru the MapBasic window:
 
Insert you string into a temporary table with only a single column and this single record
 
Then run this statement:
Select * From testcopy
Browse * From Selection
Run Menu Command 203
Close Window FrontWindow()
 
203 is the menu command for Edit > Copy
 
Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo

2009/2/26 MattC <mfca...@gmail.com>

Maurice34

unread,
Feb 26, 2009, 7:14:38 AM2/26/09
to MapInfo-L
Your "workaround" is effiicient, thanks Peter
Perhaps will you add in MB10 a "select something"... :-)

Peter Horsbøll Møller

unread,
Feb 26, 2009, 8:06:30 AM2/26/09
to mapi...@googlegroups.com
Maurice,

The even better answer is .NET.
I just did a tiny MapBasic application that call a method in a C# assembly to copy a text value to the clipboard.

Of course this requires MapInfo and MapBasic 9.5

MapBasic code:
--------------------------------------------------------------------------------
Declare Sub Main

Sub Main

Dim    sValue As String

    Dialog
        Title "Clipboard test"
        Control EditText    Width 150
            Value "Write the text to copy to the clipboard here"
            Into sValue
        Control OKButton

    Call CopyTextToClipboard(sValue)

End Sub
--------------------------------------------------------------------------------

C# code:
--------------------------------------------------------------------------------
public static void CopyTextToClipboard(string text)
{
   Clipboard.SetData(System.Windows.Forms.DataFormats.StringFormat, text);

   //Testing that the data can be read from the clipboard
   //IDataObject iData = Clipboard.GetDataObject();
   //MessageBox.Show(string.Format("Read from clipboard: {0}",  (String)iData.GetData(DataFormats.Text)));
}
--------------------------------------------------------------------------------

And you can of course extent this to also take other value types.

So using this methodology, you don't have to wait for v10 ;-)


Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo

2009/2/26 Maurice34 <mauric...@agriculture.gouv.fr>

Spencer Simpson

unread,
Feb 26, 2009, 1:21:41 PM2/26/09
to mapi...@googlegroups.com

OK guys, simple Windows API stuff works. It doesn't cause MapInfo to crash,
or cause Windows to BSOD, because MapInfo necessarily creates a global copy
of the string when it passes it into SetClipboadData ByVal.

You can use separate compilation and a project file as below, or append
TestClipboardCopy.mb, CoypStringToClipboard.def, and
CopyStringToClipboard.mb into one file, rearranging the Include directives
appropriately.

---------------------------------------------------------------------
' TestClipboardCopy.mbp
[Link]
Application=TestClipboardCopy.mbx
Module=TestClipboardCopy.mbo
Module=CopyStringToClipboard.mb

---------------------------------------------------------------------
' TestClipboardCopy.mb
Include "CopyStringToClipboard.def"
declare sub main

sub main
if CopyStringToClipboard ("A string MapBasic placed on the ClipBoard!")
then note "copy succeeded"
else note "copy failed"
end if
end mapinfo
end sub

---------------------------------------------------------------------
' CopyStringToClipboard.def

declare function CopyStringToClipboard (ByVal s as string) as logical

---------------------------------------------------------------------
' CopyStringToClipboard.mb

include "MapBasic.def"

'
' Windows API stuff
'
define CF_TEXT 1
define BOOL smallint

declare function OpenClipboard lib "User32" (ByVal hwnd as integer) as BOOL
declare function CloseClipboard lib "User32" () as BOOL
declare function EmptyClipboard lib "User32" () as BOOL
declare function SetClipboardDataString
lib "User32" alias "SetClipboardData"
(byval fmt as smallint,
ByVal s as string) as integer


function CopyStringToClipboard (ByVal s as string) as logical
CopyStringToClipboard = FALSE
if (s <> "")
then if OpenClipboard(WindowInfo (WIN_MAPINFO, WIN_INFO_WND)) = 1
then if EmptyClipboard() = 1
then if SetClipboardDataString (CF_TEXT, s) <> 0
then CopyStringToClipboard = TRUE
end if
end if
if CloseClipboard() = 0
then note "Warning: failed to close clipboard."
end if
end if
end if
end function

________________________________

Spencer

Maurice34

unread,
Feb 26, 2009, 1:25:53 PM2/26/09
to MapInfo-L
There are some "genious at work" here !!
Thanks Peter (and thanks Tony)
I will have to open my old brain: MB can't do all, it's time for me to
learn .NET, C#, etc...

Peter Horsbøll Møller

unread,
Feb 27, 2009, 2:39:25 AM2/27/09
to mapi...@googlegroups.com
Spencer,

Good point!
I should have known that there would be Windows API for doing this as well


Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo

2009/2/26 Spencer Simpson <ssim...@baltometro.org>

chuck

unread,
Aug 24, 2017, 12:00:36 PM8/24/17
to MapInfo-L
Tried this using MB 12.5 and it works first time but crashes MI second time.
Any thoughts?
Reply all
Reply to author
Forward
0 new messages