Clipboard

  • Thread starter Thread starter Andrei P.
  • Start date Start date
A

Andrei P.

Is Clipboard available in CF?
I need to paste a text to TextBox.
Any ideas?

Andrei
 
I've found Clipboard on http://www.opennetcf.org/ (Thanks, guys) but can't
make it working.
Seems static method GetClipboardText() must be available right away but it
returms MissingMethodException.
Could someone give me an example of retrieving text from Clipboard.
Thanks

Andrei
 
You have to fake it....

Whenever you implement 'Cut' or 'Copy' add the value to
an accessable value. When they paste, use that value to
populate the control you are interested in.

It's not pretty, but it's the only way I've found to
implement this....

Good Luck,

Bill
(e-mail address removed)
 
Thanks, Bill.
It would be a perfect solution in a frame of one program.
In contrast with that I'd like to Copy text from say Pocket Word and paste
into my TextBox.
There is API functions like OpenClipboard etc. and I'm sure Word's using it.
I tried OpenNETCF.org library but could'n make it working.

Andrei
 
The OpenNETCF Windows.Forms library contains Clipboard functionality using
both
GetClipboardText and SetClipboardText static methods and also methods which
are named to match the desktop framework.
http://www.opennetcf.org/forms.asp

I just tested it with this code which worked as expected:-
OpenNETCF.Windows.Forms.Clipboard.SetClipboardText("Clipboard Test");

MessageBox.Show(OpenNETCF.Windows.Forms.Clipboard.GetClipboardText());

Peter


--
Peter Foot
Windows Embedded MVP

In The Hand
http://www.inthehand.com
 
I found some code (don't remember where now) for a class that allows
accessing the actual device clipboard by using the device's API DLL's
(P/Invoke?). Just copy this code into it's own class file, reference the
class in your form and you can then use it to manipulate the clipboard. It
does, however, use unsafe code so you'll have to use the compiler switch to
allow that.

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace My.Namespace.Util
{
/// <summary>
/// Summary description for Clipboard.
/// </summary>
public class Clipboard
{
#region Win32
API ------------------------------------------------------------
private const uint CF_UNICODETEXT = 13;

[DllImport("Coredll.dll")]
private static extern bool OpenClipboard(IntPtr hWndNewOwner);

[DllImport("Coredll.dll")]
private static extern bool CloseClipboard();

[DllImport("Coredll.dll")]
private static extern bool EmptyClipboard();

[DllImport("Coredll.dll")]
private static extern bool IsClipboardFormatAvailable(uint uFormat);

[DllImport("Coredll.dll")]
private static extern IntPtr GetClipboardData(uint uFormat);

[DllImport("Coredll.dll")]
private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

[DllImport("Coredll.dll")]
private static extern IntPtr LocalAlloc(int uFlags, int uBytes);


#endregion -----------------------------------------------------------------
--

#region
Clipboard ------------------------------------------------------------
private static IntPtr hMem = IntPtr.Zero;

public static bool IsDataAvailable
{
get
{
return IsClipboardFormatAvailable(CF_UNICODETEXT);
}
}

public unsafe static bool SetData(string strText)
{
if (OpenClipboard(IntPtr.Zero) == false)
{
return false;
}

hMem = LocalAlloc(0, ((strText.Length + 1) * sizeof(char)));
if (hMem.ToInt32() == 0)
{
return false;
}

CopyToPointer(hMem, strText);

EmptyClipboard();

SetClipboardData(CF_UNICODETEXT, hMem);

CloseClipboard();

return true;
}

public static string GetData()
{
IntPtr hData;
string strText;

if (IsDataAvailable == false)
{
return null;
}

if (OpenClipboard(IntPtr.Zero) == false)
{
return null;
}

hData = GetClipboardData(CF_UNICODETEXT);
if (hData.ToInt32() == 0)
{
return null;
}

strText = ConvertToString(hData);

CloseClipboard();

return strText;
}

private unsafe static void CopyToPointer(IntPtr dest, string src)
{
int x;
char* pDest = (char*) dest.ToPointer();

for (x = 0; x < src.Length; x++)
{
pDest[x] = src[x];
}
pDest[x] = '\0'; // Add the null terminator.
}

private unsafe static string ConvertToString(IntPtr src)
{
int x;
char* pSrc = (char*) src.ToPointer();
StringBuilder sb = new StringBuilder();

for (x = 0; pSrc[x] != '\0'; x++)
{
sb.Append(pSrc[x]);
}
return sb.ToString();
}


#endregion -----------------------------------------------------------------
 
It works!
Thanks Mark!


Mark Reddick said:
I found some code (don't remember where now) for a class that allows
accessing the actual device clipboard by using the device's API DLL's
(P/Invoke?). Just copy this code into it's own class file, reference the
class in your form and you can then use it to manipulate the clipboard. It
does, however, use unsafe code so you'll have to use the compiler switch to
allow that.

using System;
using System.Text;
using System.Runtime.InteropServices;

namespace My.Namespace.Util
{
/// <summary>
/// Summary description for Clipboard.
/// </summary>
public class Clipboard
{
#region Win32
API ------------------------------------------------------------
private const uint CF_UNICODETEXT = 13;

[DllImport("Coredll.dll")]
private static extern bool OpenClipboard(IntPtr hWndNewOwner);

[DllImport("Coredll.dll")]
private static extern bool CloseClipboard();

[DllImport("Coredll.dll")]
private static extern bool EmptyClipboard();

[DllImport("Coredll.dll")]
private static extern bool IsClipboardFormatAvailable(uint uFormat);

[DllImport("Coredll.dll")]
private static extern IntPtr GetClipboardData(uint uFormat);

[DllImport("Coredll.dll")]
private static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);

[DllImport("Coredll.dll")]
private static extern IntPtr LocalAlloc(int uFlags, int uBytes);
#endregion -----------------------------------------------------------------
--

#region
Clipboard ------------------------------------------------------------
private static IntPtr hMem = IntPtr.Zero;

public static bool IsDataAvailable
{
get
{
return IsClipboardFormatAvailable(CF_UNICODETEXT);
}
}

public unsafe static bool SetData(string strText)
{
if (OpenClipboard(IntPtr.Zero) == false)
{
return false;
}

hMem = LocalAlloc(0, ((strText.Length + 1) * sizeof(char)));
if (hMem.ToInt32() == 0)
{
return false;
}

CopyToPointer(hMem, strText);

EmptyClipboard();

SetClipboardData(CF_UNICODETEXT, hMem);

CloseClipboard();

return true;
}

public static string GetData()
{
IntPtr hData;
string strText;

if (IsDataAvailable == false)
{
return null;
}

if (OpenClipboard(IntPtr.Zero) == false)
{
return null;
}

hData = GetClipboardData(CF_UNICODETEXT);
if (hData.ToInt32() == 0)
{
return null;
}

strText = ConvertToString(hData);

CloseClipboard();

return strText;
}

private unsafe static void CopyToPointer(IntPtr dest, string src)
{
int x;
char* pDest = (char*) dest.ToPointer();

for (x = 0; x < src.Length; x++)
{
pDest[x] = src[x];
}
pDest[x] = '\0'; // Add the null terminator.
}

private unsafe static string ConvertToString(IntPtr src)
{
int x;
char* pSrc = (char*) src.ToPointer();
StringBuilder sb = new StringBuilder();

for (x = 0; pSrc[x] != '\0'; x++)
{
sb.Append(pSrc[x]);
}
return sb.ToString();
}
#endregion -----------------------------------------------------------------
 
Peter, i paste your exact code and i get MissingMethodException!

What's up with that?

Thanks,

-Tim
 
I thought I'm the only person doing something wrong ...
In the Clipboard class they have at least a reference to WinAPI class from
different OpenNETCF project.
I got that too but it didn't help.

Andrei
 
I'll check and see what is up with the posted code (I'm using the clipboard
class from our source library but it should be the same as the current
windows.forms release).

Peter

--
Peter Foot, eMVP

Advisory Board Member
http://www.OpenNETCF.org
 
Back
Top