Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C#
Tip/Trick

How to get clipboard text in XNA

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Feb 2013CPOL 8K   2  
Here we get clipboard text to use in an XNA app.

Introduction

Here we want to get clipboard text in XNA, but as i did this similar to windows forms getting clipboard it gave me some error, that I should use STA thread. so here I'm explaining how to get clipboard text using System.Windows.Forms.

The clipboard is very important for users to paste their cheats and stuff in a game.

Using the code

We create a class with name clipboard and we put this code below in it.

Here I used another thread for my STA purposes, so I'm here avoiding conflict in multi threaded applications like games. The thread name is t. we wait until our thread get clipboard text and put it into clipboard.

After checking if there is any text in clipboard by Clipboard.ContainsText() we set clipboard string variable using the Clipboard.GetText() function.

C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Language_Learning_Application
{
    public class clsClipBoard
    {
        string clipboard="";
        public String GetClipboardText()
        {
            Thread t = new Thread(getClipboard);
            t.SetApartmentState(ApartmentState.STA);
            t.Start();
            while(t.IsAlive)
            {
            }
            return clipboard;
        }

        [STAThread]
        private void getClipboard()
        {
            if (Clipboard.ContainsText())
            {
                clipboard = Clipboard.GetText(TextDataFormat.UnicodeText);
                //Clipboard.SetText(replacementHtmlText, TextDataFormat.UnicodeText);
            }
        }
    }
}

So wherever we need clipboard text we do it like this:

C#
public clsClipBoard myclsClipBoard = new clsClipBoard()
string clipboard  =myclsClipBoard.GetClipboardText();

It was easy more explanation would be given if there may be any question.

Points of Interest

Here we learned how to get clipboard text cause it's important in giving our application product keys or cheats or many other tasks.

History

  • Started it on 2/9/2013.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Italy Italy
Education:

NODET 2003-2007
B.Sc. Electrical Power Engineering 2007–2012

Work:

T.G.D. Co.(Power,Software) July 2010 -February 2012
Artnous Co.(Power) July 2010 -February 2012
Savay Co.(Software) September 2011- July 2012

Comments and Discussions

 
-- There are no messages in this forum --