Shoutbox

Trying to copy some text to the clipboard - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Trying to copy some text to the clipboard (/showthread.php?tid=62754)

Trying to copy some text to the clipboard by segosa on 07-07-2006 at 04:27 PM

I'm trying to make a function that will copy text to the clipboard. I'm using the Windows API. This is the code I have so far:

code:
function set_clipboard_text()
{
    var tmp = "This is a test";
    var l = tmp.length * 2 + 2;

    if (Interop.Call("User32", "OpenClipboard", 0))
    {
            Interop.Call("User32", "EmptyClipboard");
            var h = Interop.Call("Kernel32", "GlobalAlloc", 0x2000, l);
            var p = Interop.Allocate( l );
            p.WriteDWORD(0, Interop.Call("Kernel32", "GlobalLock", h));
            Interop.Call("Kernel32", "lstrcpyW", p, tmp);
            Interop.Call("Kernel32", "GlobalUnlock", h);
            Interop.Call("User32", "SetClipboardData", 1, h)
            Interop.Call("User32", "CloseClipboard");
    }
}


I'm obviously going to make it set_clipboard_text(str), the way the function is now is just while I test it and try and get it to work.

I'm basing it off some C++ code that I wrote a while ago:

code:
    OpenClipboard(NULL);
    EmptyClipboard();
    HGLOBAL h = GlobalAlloc(GMEM_DDESHARE, strlen(source)+1);
    char* p = (char*)GlobalLock( h );
    lstrcpy(p, source);
    GlobalUnlock( h );
    SetClipboardData(CF_TEXT,h);
    CloseClipboard();


The function as it is now works fine, there are no errors. And text is copied to the clipboard. The problem is, this text consists of several spaces and random characters. I have a feeling it's a unicode problem but I can't see where I've gone wrong in my function, so does anyone have any ideas?
RE: Trying to copy some text to the clipboard by J-Thread on 07-07-2006 at 10:48 PM

How about using the Clipboard object? (following code copied directly from the MSDN without testing):

code:
private function button1_Click(sender : Object, e : System.EventArgs) {
    //Take the selected text from a text box and put it on the clipboard.
    if(textBox1.SelectedText != "")
       Clipboard.SetDataObject(textBox1.SelectedText);
    else
       textBox2.Text = "No text selected in textBox1";
}

private function button2_Click(sender : Object, e : System.EventArgs) {
    //Declare an IDataObject to hold the data returned from the clipboard.
    //Then retrieve the data from the clipboard.
    var iData : IDataObject = Clipboard.GetDataObject();

    //Determine whether the data is in a format you can use.
    if(iData.GetDataPresent(DataFormats.Text)) {
       //Yes it is, so display it in a text box.
       textBox2.Text = String(iData.GetData(DataFormats.Text));
    }
    else {
       //No it is not.
       textBox2.Text = "Could not retrieve data off the clipboard.";
    }
}


RE: Trying to copy some text to the clipboard by RaceProUK on 07-07-2006 at 10:55 PM

That looks a little .NET to me... will it actually work?


RE: Trying to copy some text to the clipboard by J-Thread on 07-07-2006 at 11:00 PM

I noticed that... But this page is really talking about JScript... It seems like this is different from the language we are using for scripts...^o)


RE: Trying to copy some text to the clipboard by Dhaya on 07-07-2006 at 11:28 PM

I tested using this class which is inherited from System.Windows.Forms, and i didn't found a way to make it work :/


RE: Trying to copy some text to the clipboard by Eljay on 07-08-2006 at 07:43 AM

quote:
Originally posted by J-Thread
I noticed that... But this page is really talking about JScript... It seems like this is different from the language we are using for scripts...^o)

thats because its JScript.net 8-)
RE: Trying to copy some text to the clipboard by J-Thread on 07-08-2006 at 11:02 AM

Right... I think I get it now...

So let's go back to topic, I don't see why that wouldn't work... But I guess there is a better way to acces the clipboard. Maybe with some ActiveX object?


RE: Trying to copy some text to the clipboard by andrey on 07-12-2006 at 02:26 PM

Solution for copying text to/from the clipboard:
[UPDATED] Clipboard Functions (that WORK)