Clipboard in Windows Store App


In this section we will discuss the clipboard; how to copy and paste various text formats, HTML, URLs and rich text. Text formats are encapsulated in a data package. To put things into the clipboard, set the data package. Data packages are also used to share or send content between applications.

By using bitmaps we can select any file to copy and paste to the clipboard. The data package can also hold a bitmap. Select an image using the picker, copy it to the clipboard and then paste it back to the application. The DataPackage can also hold files. Select files using the picker, copy it to the clipboard, and then paste the files. This will copy the files into this app's local state folder.

Step 1 : First of all you will create a new Metro Style Application; let us see the description with images of how you will create it.

  • Open Visual Studio 2011
  • File -> New -> Project
  • Choose Template -> JavaScript-> Windows Metro Style -> Application
  • Rename this Application

Step 2 :  In the Solution Explorer there are two files that we will primarily work with, default.js and default.html.


Step 3 :  Open the default.html page and add the following code. The code will look as shown below.

Code :

<!DOCTYPE html>





    <!-- WinJS references -->

    <link href="winjs/css/ui-light.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript" src="WinJS/js/base.js"></script>

    <script type="text/javascript" src="WinJS/js/ui.js"></script>

    <script type="text/javascript" src="WinJS/js/wwaapp.js"></script>


    <!-- Base references -->

    <link rel="stylesheet" type="text/css" href="css/base-sdk.css" />

    <script type="text/javascript" src="base-sdk.js"></script>


    <!-- Application references -->

    <link rel="stylesheet" type="text/css" href="css/program.css" />

    <script type="text/javascript" src="program.js"></script>


<body role="application" style="background-color:#ff6a00">

    <div id="rootGrid">

        <div id="header" role="contentinfo">


        <div id="content">

            <h1 id="featureLabel">


            <h2 id="inputLabel">


            <div id="input" role="main" aria-labelledby="inputLabel">

                <div class="options">

                    <h3 id="listLabel">

                        Select Option</h3>

                    <select size="3" id="Sections" aria-labelledby="listLabel" style="background-color:#ffd800">

                        <option selected="selected" value="1" style="color:#f00">1. Copy and cut text </option>

                        <option value="2" style="color:#001cff">2. Using bitmaps</option>

                        <option value="3" style="color:#300202">3. Using storage items</option>



                <div class="details" role="region" aria-labelledby="descLabel" aria-live="assertive">

                    <h3 id="descLabel">


                    <!-- Section 1 Input -->

                    <div class="item" id="Section1Input">


                            Use Clipboard to copy and paste different text formats, including unicode, HTML,

                            URIs, and rich text. Text formats are encapsulated in a DataPackage. To put things

                            into clipboard, set the DataPackage. To get things out of the clipboard, get the

                            DataPackage. You can also register to know when the contents of the clipboard have


                            <br />

                            DataPackages are also used to share or send content between applications.


                        <div id="text">

                            Enter text

                            <input id="Section1Text" value="regular ol' unicode" />


                        <div id="HTML">

                            Enter HTML

                            <input id="Section1Html" value="<b>copy me!</b>" />


                        <div id="URI">

                            Enter a URI

                            <input id="Section1URI" value="" />


                        <div id="RTF">

                            Enter rich text

                            <input id="Section1Rtf" value="rich text format" />


                        <br />

                        <button class="action" id="Section1Copy">


                        <button class="action" id="Section1Cut">


                        <button class="action" id="Section1Cancel">


                        <button id="Section1RegisterContentChanged">


                        <br />

                        <br />

                        <button class="action" id="Section1Paste">



                    <!-- Section 2 Input -->

                    <div class="item" id="Section2Input">


                            The DataPackage can also hold a bitmap. Select image using the picker, copy it to

                            the clipboard, and then paste it back into the application.</p>

                        <button id="Section2Copy" class="action">

                            Select and copy</button>

                        <button id="Section2Paste" class="action">



                    <!-- Section 3 Input -->

                    <div class="item" id="Section3Input">


                            The DataPackage can also hold files. Select files using the picker, copy it to  the

                            clipboard, and then paste the files. This will copy the files into this app's local

                            state folder.</p>

                        <button id="Section3Copy" class="action">

                            Select and copy</button>

                        <button id="Section3Paste" class="action">



                    <!-- Section 4 Input -->

                    <div class="item" id="Section4Input">


                            Using clipboard means you can get whatever is currently in it, including content

                            set by a different app. In this Section, go to different apps installed on your

                            system, then switch back to this app and click Paste. The clipboard will be inspected

                            and display everything currently in it.</p>

                        <button id="Section4Paste" class="action">





            <h2 id="outputLabel">


            <div id="output" role="region" aria-labelledby="outputLabel" aria-live="assertive">

                <div id="statusMessage">


                <!-- Section 1 Output -->

                <div class="item" id="Section1Output">

                    <div id="Section1TextOutput">


                    <div id="Section1HtmlOutput">


                    <div id="Section1UriOutput">


                    <div id="Section1RtfOutput">



                <!-- Section 2 Output -->

                <div class="item" id="Section2Output">

                    <p id="Section2text">


                    <img class="imageHolder" id="Section2ImageHolder" alt="image holder" src="images/placeholder-sdk.png"

                        style="visibility: hidden" />


                <!-- Section 3 Output -->

                <div class="item" id="Section3Output">

                    <p id="Section3Output">



                <!-- Section 4 Output -->

                <div class="item" id="Section4Output">

                    <div id="Section4RequestedOperation">


                    <div id="Section4TextOutput">


                    <div id="Section4HtmlOutput">


                    <div id="Section4UriOutput">


                    <div id="Section4RtfOutput">




                        <img class="imageHolder" id="Section4ImageHolder" alt="image holder" src="" style="visibility: hidden" />


                    <div id="Section4FilesOutput">





        <div id="footer" role="contentinfo">





Step 4 :  After applying this code, run this application and then we get the following output:


Copy and paste onto clipboard as shown below.


In the second point, using the bitmap we have selected and copied an image; after that paste onto the clipboard.


Select an image and click on the open button.


The selected image is shown onto the clipboard as output.


In the third point, by using the storage item we can select and copy these items.


Here we can choose an item from the given list.


After selecting and copying an item, paste it here and then give an output.


Similar Articles