{"id":121,"date":"2010-03-01T15:03:06","date_gmt":"2010-03-01T09:33:06","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=121"},"modified":"2011-01-26T15:03:24","modified_gmt":"2011-01-26T09:33:24","slug":"applications-using-the-system-clipboard-in-windows-mobile","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2010\/03\/01\/applications-using-the-system-clipboard-in-windows-mobile\/","title":{"rendered":"Applications: Using the System Clipboard in Windows Mobile"},"content":{"rendered":"<p style=\"text-align: justify;\">Recently, someone on the MSDN forum <a href=\"http:\/\/social.msdn.microsoft.com\/Forums\/en-US\/windowsmobiledev\/thread\/23090170-c1b2-4ede-be19-d035ad15ba52?prof=required\">asked<\/a> about using &#8216;Cut, Copy and Paste&#8217; operation in their application and I thought it makes for a worthy post. Using the system clipboard is pretty easy. <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms838306.aspx\">This article<\/a> has everything you need know. Basically, the clipboard operations are performed in the following order:<\/p>\n<p>Copy<br \/>\n1. Open Clipboard<br \/>\n2. Empty Clipboard&#8217;s contents<br \/>\n3. Set the Clipboard&#8217;s data<br \/>\n4. Close Clipboard<\/p>\n<p>Paste<br \/>\n1. Open Clipboard<br \/>\n2. Check if Clipboard data is available<br \/>\n3. Get the Clipboard data<br \/>\n4. Close Clipboard<\/p>\n<p style=\"text-align: justify;\">Cut operation is the same as Copy but you also delete the data from the source. It is upto your application to do that.<\/p>\n<p style=\"text-align: justify;\">The sample application that I built handles only &#8216;text&#8217; clipboard data and consists of two multiline edit boxes, the top one serving as the source and the bottom as destination for all clipboard operations. Lets look at how the application works, and then we&#8217;ll look at the code:<\/p>\n<p><object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http:\/\/www.youtube.com\/v\/mbQG87iyxmM&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999\" \/><param name=\"allowFullScreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><\/object><\/p>\n<p>\nFor Copy, Paste and Cut, the following handlers are called:<\/p>\n<p><span style=\"color: rgb(51, 153, 102);\"><span style=\"font-family: Verdana;\">\/\/DoCopy<\/span><\/span><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\"><br \/>\nvoid DoCopy(HWND hDlg)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; BOOL ret;<br \/>\n&nbsp;&nbsp;&nbsp; HWND hTemp;<br \/>\n&nbsp;&nbsp;&nbsp; TCHAR *pData = NULL;<br \/>\n&nbsp;&nbsp;&nbsp; int size = 0;<\/p>\n<p>&nbsp;&nbsp;&nbsp; hTemp = GetDlgItem(hDlg, IDC_EDIT_TOP);<\/p>\n<p>&nbsp;&nbsp;&nbsp; _ASSERT(hTemp != NULL);<br \/>\n&nbsp;&nbsp;&nbsp; if (!hTemp)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<\/p>\n<p>&nbsp;&nbsp;&nbsp; <span style=\"color: rgb(51, 153, 102);\">\/\/get the size of text in the edit box<\/span><br \/>\n&nbsp;&nbsp;&nbsp; size = SendMessage(hTemp, WM_GETTEXTLENGTH, 0, 0);<\/p>\n<p>&nbsp;&nbsp;&nbsp; pData = (TCHAR*)LocalAlloc(LPTR, size*sizeof(TCHAR) + 1);<\/p>\n<p>&nbsp;&nbsp;&nbsp; _ASSERT(pData != NULL);<br \/>\n&nbsp;&nbsp;&nbsp; if (!pData)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<\/p>\n<p>&nbsp;&nbsp;&nbsp; SendMessage(hTemp, WM_GETTEXT, (WPARAM)size+1, (LPARAM)pData);<\/p>\n<p>&nbsp;&nbsp;&nbsp; ret = OpenClipboard(hDlg);<\/p>\n<p>&nbsp;&nbsp;&nbsp; _ASSERT(ret != 0);<br \/>\n&nbsp;&nbsp;&nbsp; if (ret == 0)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<\/p>\n<p>&nbsp;&nbsp;&nbsp; EmptyClipboard();<\/p>\n<p>&nbsp;&nbsp;&nbsp; if (!SetClipboardData(CF_UNICODETEXT, pData))<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _ASSERT(FALSE);<br \/>\n&nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; CloseClipboard();<br \/>\n}<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: rgb(51, 153, 102);\"><span style=\"font-family: Verdana;\">\/\/DoPaste<\/span><\/span><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\"><br \/>\nvoid DoPaste(HWND hDlg)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; BOOL ret;<br \/>\n&nbsp;&nbsp;&nbsp; TCHAR *pData = NULL;<\/p>\n<p>&nbsp;&nbsp;&nbsp; ret = OpenClipboard(hDlg);<br \/>\n&nbsp;&nbsp;&nbsp; _ASSERT(ret != 0);<br \/>\n&nbsp;&nbsp;&nbsp; if (ret == 0)<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<\/p>\n<p>&nbsp;&nbsp;&nbsp; ret = IsClipboardFormatAvailable(CF_UNICODETEXT);<br \/>\n&nbsp;&nbsp;&nbsp; if (ret == 0)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; CloseClipboard();<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return;<br \/>\n&nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; pData = (TCHAR*)GetClipboardData(CF_UNICODETEXT);<\/p>\n<p>&nbsp;&nbsp;&nbsp; if (pData)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SendMessage(GetDlgItem(hDlg, IDC_EDIT_BOTTOM), WM_SETTEXT, 0, (LPARAM)pData);<br \/>\n&nbsp;&nbsp;&nbsp; }<\/p>\n<p>&nbsp;&nbsp;&nbsp; CloseClipboard();<br \/>\n}<br \/>\n<\/span><\/span><\/p>\n<p><span style=\"color: rgb(51, 153, 102);\"><span style=\"font-family: Verdana;\">\/\/DoCut<\/span><\/span><span style=\"font-family: Verdana;\"><span style=\"color: rgb(0, 0, 128);\"><br \/>\nvoid DoCut(HWND hDlg)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; DoCopy(hDlg);<\/p>\n<p>&nbsp;&nbsp;&nbsp; SendMessage(GetDlgItem(hDlg, IDC_EDIT_TOP), WM_SETTEXT, 0, (LPARAM)(TEXT(&quot;&quot;)));<br \/>\n}<\/span><\/span><\/p>\n<p style=\"text-align: justify;\">The code is simple enough to understand. All the Clipboard functions can be found <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa924514.aspx\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, someone on the MSDN forum asked about using &#8216;Cut, Copy and Paste&#8217; operation in their application and I thought it makes for a worthy post. Using the system clipboard is pretty easy. This article has everything you need know. Basically, the clipboard operations are performed in the following order: Copy 1. Open Clipboard 2. &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2010\/03\/01\/applications-using-the-system-clipboard-in-windows-mobile\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Applications: Using the System Clipboard in Windows Mobile<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-1X","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/121"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":1,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/121\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}