www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - copy and paste in program

reply Joel Christensen <joelcnz gmail.com> writes:
Hi,

I've got a text program I'm working on. It has game like print. But I 
want to be able to copy to the clip board and paste from it in my 
program etc. I'm using Windows 7. I have used a bit of Ubuntu in the past.

- Joelcnz
Sep 11 2011
next sibling parent reply Jimmy Cao <jcao219 gmail.com> writes:
--00151744109cf0841d04acb46132
Content-Type: text/plain; charset=ISO-8859-1

Well, it doesn't matter what you've used in the past :-)

Take a look:

http://msdn.microsoft.com/en-us/library/ms907128.aspx

On Sun, Sep 11, 2011 at 5:29 PM, Joel Christensen <joelcnz gmail.com> wrote:

 Hi,

 I've got a text program I'm working on. It has game like print. But I want
 to be able to copy to the clip board and paste from it in my program etc.
 I'm using Windows 7. I have used a bit of Ubuntu in the past.

 - Joelcnz

--00151744109cf0841d04acb46132 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Well, it doesn&#39;t matter what you&#39;ve used in the past :-)<br><div><b= r></div><div>Take a look:<br><div><br></div><div><a href=3D"http://msdn.mic= rosoft.com/en-us/library/ms907128.aspx">http://msdn.microsoft.com/en-us/lib= rary/ms907128.aspx</a><br> <br><div class=3D"gmail_quote">On Sun, Sep 11, 2011 at 5:29 PM, Joel Christ= ensen <span dir=3D"ltr">&lt;<a href=3D"mailto:joelcnz gmail.com">joelcnz gm= ail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D= "margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> Hi,<br> <br> I&#39;ve got a text program I&#39;m working on. It has game like print. But= I want to be able to copy to the clip board and paste from it in my progra= m etc. I&#39;m using Windows 7. I have used a bit of Ubuntu in the past.<br=

<br> - Joelcnz<br> </blockquote></div><br></div></div> --00151744109cf0841d04acb46132--
Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks for the reply Jimmy.

I was thinking more like SDL (has SDL_GetClipboardText, 
SDL_SetClipboardText, 
http://wiki.libsdl.org/moin.cgi/CategoryClipboard). I'm not sure I would 
be able to get Windows CE Clipboard stuff working for me.

- Joelcnz

On 12-Sep-11 1:23 PM, Jimmy Cao wrote:
 Well, it doesn't matter what you've used in the past :-)

 Take a look:

 http://msdn.microsoft.com/en-us/library/ms907128.aspx

 On Sun, Sep 11, 2011 at 5:29 PM, Joel Christensen <joelcnz gmail.com
 <mailto:joelcnz gmail.com>> wrote:

     Hi,

     I've got a text program I'm working on. It has game like print. But
     I want to be able to copy to the clip board and paste from it in my
     program etc. I'm using Windows 7. I have used a bit of Ubuntu in the
     past.

     - Joelcnz

Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
So how would I got about doing it in D?

On 12-Sep-11 1:52 PM, Vladimir Panteleev wrote:
 On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>
 wrote:

 I'm not sure I would be able to get Windows CE Clipboard stuff working
 for me.

Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx

Sep 11 2011
parent reply Joel Christensen <joelcnz gmail.com> writes:
Thanks Jimmy. Your example worked. Or though I haven't managed to get 
the other way to work.

[code]
import std.stdio;
//import core.stdc.string;
import std.c.string;
import std.string;
import std.conv;

extern(Windows) {
     bool OpenClipboard(void*);
     void* GetClipboardData(uint);
     void* SetClipboardData(uint, void*);
}

void main() {
     if (OpenClipboard(null)) {
         auto cstr = cast(char*)GetClipboardData( 1 );
         if (cstr)
             writeln(to!string(cast(char*)cstr[0..strlen(cstr)]));

		SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
     }
}
[/code]
Sep 11 2011
next sibling parent Joel Christensen <joelcnz gmail.com> writes:
I mean, I can't copy text from my program to the clipboard.

- Joelcnz

On 12-Sep-11 3:50 PM, Joel Christensen wrote:
 Thanks Jimmy. Your example worked. Or though I haven't managed to get
 the other way to work.

 [code]
 import std.stdio;
 //import core.stdc.string;
 import std.c.string;
 import std.string;
 import std.conv;

 extern(Windows) {
 bool OpenClipboard(void*);
 void* GetClipboardData(uint);
 void* SetClipboardData(uint, void*);
 }

 void main() {
 if (OpenClipboard(null)) {
 auto cstr = cast(char*)GetClipboardData( 1 );
 if (cstr)
 writeln(to!string(cast(char*)cstr[0..strlen(cstr)]));

 SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
 }
 }
 [/code]

Sep 12 2011
prev sibling parent Joel Christensen <joelcnz gmail.com> writes:
Thanks so much Jimmy. You put in a bit of effort. :-)

I just added this code to my general library:



bool OpenClipboard(void*); void* GetClipboardData(uint); void* SetClipboardData(uint, void*); bool EmptyClipboard(); bool CloseClipboard(); void* GlobalAlloc(uint, size_t); void* GlobalLock(void*); bool GlobalUnlock(void*); } string getTextClipBoard() { if (OpenClipboard(null)) { scope( exit ) CloseClipboard(); auto cstr = cast(char*)GetClipboardData( 1 ); if(cstr) return cstr[0..strlen(cstr)].idup; } return null; } string setTextClipboard( string mystr ) { if (OpenClipboard(null)) { scope( exit ) CloseClipboard(); EmptyClipboard(); void* handle = GlobalAlloc(2, mystr.length + 1); void* ptr = GlobalLock(handle); memcpy(ptr, toStringz(mystr), mystr.length + 1); GlobalUnlock(handle); SetClipboardData( 1, handle); } return mystr; } <<
Sep 12 2011
prev sibling next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>  
wrote:

 I'm not sure I would be able to get Windows CE Clipboard stuff working  
 for me.

Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx -- Best regards, Vladimir mailto:vladimir thecybershadow.net
Sep 11 2011
prev sibling next sibling parent Jimmy Cao <jcao219 gmail.com> writes:
--00151747c108eb068604acb58c60
Content-Type: text/plain; charset=ISO-8859-1

Here's an example I've quickly put together for you:

import std.stdio;
import core.stdc.string;

extern(Windows) {
    void* GetClipboardData(uint);
    bool OpenClipboard(void*);
}

void main() {
    if (OpenClipboard(null)) {
        auto cstr = cast(char*)GetClipboardData(1);
        if (cstr)
            writeln(std.conv.to!string(cast(char*)cstr[0..strlen(cstr)]));
    }
}



On Sun, Sep 11, 2011 at 9:22 PM, Joel Christensen <joelcnz gmail.com> wrote:

 So how would I got about doing it in D?


 On 12-Sep-11 1:52 PM, Vladimir Panteleev wrote:

 On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen <joelcnz gmail.com>
 wrote:

  I'm not sure I would be able to get Windows CE Clipboard stuff working
 for me.

Search engines often return Windows CE results for API searches. Here's the clipboard developer documentation for full Windows versions: http://msdn.microsoft.com/en-**us/library/ms648709(v=VS.85).**aspx<http://msdn.microsoft.com/en-us/library/ms648709(v=VS.85).aspx>


--00151747c108eb068604acb58c60 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>Here&#39;s an example I&#39;ve quickly put together for you:</div><div=
<br></div><blockquote style=3D"margin: 0 0 0 40px; border: none; padding: =

<div><br></div><div>extern(Windows) {</div><div>=A0 =A0 void* GetClipboardD= ata(uint);</div><div>=A0 =A0 bool OpenClipboard(void*);</div><div>}</div><d= iv><br></div><div>void main() {</div><div>=A0 =A0 if (OpenClipboard(null)) = {</div> <div> =A0 =A0 =A0 =A0 auto cstr =3D cast(char*)GetClipboardData(1);</div><div>=A0= =A0 =A0 =A0 if (cstr)</div><div>=A0 =A0 =A0 =A0 =A0 =A0 writeln(<a href=3D= "http://std.conv.to">std.conv.to</a>!string(cast(char*)cstr[0..strlen(cstr)= ]));</div><div>=A0 =A0 }</div><div> }</div></div><div><br></div><div><br></div></blockquote><br><div class=3D"g= mail_quote">On Sun, Sep 11, 2011 at 9:22 PM, Joel Christensen <span dir=3D"= ltr">&lt;<a href=3D"mailto:joelcnz gmail.com" target=3D"_blank">joelcnz gma= il.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> So how would I got about doing it in D?<div><div></div><div><br> <br> On 12-Sep-11 1:52 PM, Vladimir Panteleev wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> On Mon, 12 Sep 2011 04:39:52 +0300, Joel Christensen &lt;<a href=3D"mailto:= joelcnz gmail.com" target=3D"_blank">joelcnz gmail.com</a>&gt;<br> wrote:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> I&#39;m not sure I would be able to get Windows CE Clipboard stuff working<= br> for me.<br> </blockquote> <br> Search engines often return Windows CE results for API searches.<br> <br> Here&#39;s the clipboard developer documentation for full Windows versions:= <br> <br> <a href=3D"http://msdn.microsoft.com/en-us/library/ms648709(v=3DVS.85).aspx= " target=3D"_blank">http://msdn.microsoft.com/en-<u></u>us/library/ms648709= (v=3DVS.85).<u></u>aspx</a><br> <br> </blockquote> <br> </div></div></blockquote></div><br> --00151747c108eb068604acb58c60--
Sep 11 2011
prev sibling parent Jimmy Cao <jcao219 gmail.com> writes:
--00151744109c00548c04acc54979
Content-Type: text/plain; charset=ISO-8859-1

The other way is a bit more complicated.
Try this:

import std.stdio;
import core.stdc.string;
import std.string;

extern(Windows) {
   bool OpenClipboard(void*);
   void* GetClipboardData(uint);
   void* SetClipboardData(uint, void*);
   bool EmptyClipboard();
   bool CloseClipboard();
   void* GlobalAlloc(uint, size_t);
   void* GlobalLock(void*);
   bool GlobalUnlock(void*);
}

void main() {
   if (OpenClipboard(null)) {
       auto cstr = cast(char*)GetClipboardData( 1 );
       if(cstr)
           writeln(cstr[0..strlen(cstr)]);
       EmptyClipboard();

       auto mystr = "my data";
       void* handle = GlobalAlloc(2, mystr.length + 1);
       void* ptr = GlobalLock(handle);
       memcpy(ptr, toStringz(mystr), mystr.length + 1);
       GlobalUnlock(handle);

       SetClipboardData( 1, handle);
       CloseClipboard();
   }
}



On Sun, Sep 11, 2011 at 10:50 PM, Joel Christensen <joelcnz gmail.com>wrote:

 Thanks Jimmy. Your example worked. Or though I haven't managed to get the
 other way to work.

 [code]

 import std.stdio;
 //import core.stdc.string;
 import std.c.string;
 import std.string;
 import std.conv;

 extern(Windows) {
    bool OpenClipboard(void*);
    void* GetClipboardData(uint);
    void* SetClipboardData(uint, void*);

 }

 void main() {
    if (OpenClipboard(null)) {
        auto cstr = cast(char*)GetClipboardData( 1 );
        if (cstr)
            writeln(to!string(cast(char*)**cstr[0..strlen(cstr)]));

                SetClipboardData( 1, cast(char*)toStringz( "data set" ) );
    }
 }
 [/code]

--00151744109c00548c04acc54979 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div>The other way is a bit more complicated.</div><div>Try this:</div><div=
<br></div><blockquote style=3D"margin: 0 0 0 40px; border: none; padding: =

<div>import std.string;</div><div><br></div><div>extern(Windows) {</div><di= v>=A0 =A0bool OpenClipboard(void*);</div><div>=A0 =A0void* GetClipboardData= (uint);</div><div>=A0 =A0void* SetClipboardData(uint, void*);</div><div>=A0= =A0bool EmptyClipboard();</div> <div>=A0 =A0bool CloseClipboard();</div><div>=A0 =A0void* GlobalAlloc(uint,= size_t);</div><div>=A0 =A0void* GlobalLock(void*);</div><div>=A0 =A0bool G= lobalUnlock(void*);</div><div>}</div><div><br></div><div>void main() {</div=
<div>=A0 =A0if (OpenClipboard(null)) {</div>

<div>=A0 =A0 =A0 =A0auto cstr =3D cast(char*)GetClipboardData( 1 );</div><d= iv>=A0 =A0 =A0 =A0if(cstr)</div><div>=A0 =A0 =A0 =A0 =A0 =A0writeln(cstr[0.= .strlen(cstr)]);</div><div>=A0 =A0 =A0 =A0EmptyClipboard();</div><div>=A0 = =A0 =A0 =A0</div><div>=A0 =A0 =A0 =A0auto mystr =3D &quot;my data&quot;;</d= iv> <div>=A0 =A0 =A0 =A0void* handle =3D GlobalAlloc(2, mystr.length + 1);</div=
<div>=A0 =A0 =A0 =A0void* ptr =3D GlobalLock(handle);</div><div>=A0 =A0 =

=A0 =A0GlobalUnlock(handle);</div> <div> =A0 =A0 =A0 =A0</div><div>=A0 =A0 =A0 =A0SetClipboardData( 1, handle);</div=
<div>=A0 =A0 =A0 =A0CloseClipboard();</div><div>=A0 =A0}</div><div>}</div>=

at 10:50 PM, Joel Christensen <span dir=3D"ltr">&lt;<a href=3D"mailto:joelc= nz gmail.com">joelcnz gmail.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex;">Thanks Jimmy. Your example worked. Or thoug= h I haven&#39;t managed to get the other way to work.<br> <br> [code]<div class=3D"im"><br> import std.stdio;<br> //import core.stdc.string;<br></div> import std.c.string;<br> import std.string;<br> import std.conv;<br> <br> extern(Windows) {<br> =A0 =A0bool OpenClipboard(void*);<br> =A0 =A0void* GetClipboardData(uint);<br> =A0 =A0void* SetClipboardData(uint, void*);<div class=3D"im"><br> }<br> <br> void main() {<br> =A0 =A0if (OpenClipboard(null)) {<br> =A0 =A0 =A0 =A0auto cstr =3D cast(char*)GetClipboardData( 1 );<br> =A0 =A0 =A0 =A0if (cstr)<br></div> =A0 =A0 =A0 =A0 =A0 =A0writeln(to!string(cast(char*)<u></u>cstr[0..strlen(= cstr)]));<br> <br> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0SetClipboardData( 1, cast(char*)toStringz( = &quot;data set&quot; ) );<br> =A0 =A0}<br> }<br> [/code]<br> </blockquote></div><br> --00151744109c00548c04acc54979--
Sep 12 2011