{"id":392,"date":"2014-12-10T12:03:44","date_gmt":"2014-12-10T11:03:44","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=392"},"modified":"2014-12-14T18:34:28","modified_gmt":"2014-12-14T17:34:28","slug":"dragdrop-from-wpfwinforms-to-desktop","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=392","title":{"rendered":"Drag\/Drop from WPF\/WinForms to Desktop"},"content":{"rendered":"<p>I was surprised how easy it is to enable your application to allow dragging things (eg. text or images) from the application to the desktop.<\/p>\n<p>Basically, you create a temporary file with the content in your application, create a DataObject out of it, perform the DoDragDrop and then delete the temporary file.<\/p>\n<p>Here are two snippets (one for WPF, one for WinForms) to show you this. Just call them somewhere (for example in a MousDown event of on a button click).<\/p>\n<p><strong>WPF<\/strong><\/p>\n<pre class=\"lang:c# decode:true\">private void DragText()\r\n{\r\n    \/\/ Create temp file\r\n    var tempFilePath = Path.Combine(Path.GetTempPath(), String.Format(\"{0}.txt\", \"test\"));\r\n    File.WriteAllText(tempFilePath, \"Hello world!\");\r\n    \/\/ Prepare the data object\r\n    string[] files = { tempFilePath };\r\n    var data = new DataObject(DataFormats.FileDrop, files);\r\n    data.SetData(DataFormats.Text, files[0]);\r\n    \/\/ Perform dragdrop\r\n    DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);\r\n    \/\/ Delete the temp file\r\n    File.Delete(tempFilePath);\r\n}<\/pre>\n<p><strong>WinForms<\/strong><\/p>\n<pre class=\"lang:c# decode:true\">private void DragImage()\r\n{\r\n    \/\/ Create temp file\r\n    var tempFilePath = Path.Combine(Path.GetTempPath(), String.Format(\"{0}.jpg\", \"test\"));\r\n    pictureBox1.Image.Save(tempFilePath, ImageFormat.Jpeg);\r\n    \/\/ Prepare the data object\r\n    string[] files = { tempFilePath };\r\n    var data = new DataObject(DataFormats.FileDrop, files);\r\n    data.SetData(DataFormats.Text, files[0]);\r\n    \/\/ Perform dragdrop\r\n    DoDragDrop(data, DragDropEffects.Copy);\r\n    \/\/ Delete the temp file\r\n    File.Delete(tempFilePath);\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was surprised how easy it is to enable your application to allow dragging things (eg. text or images) from the application to the desktop. Basically, you create a temporary file with the content in your application, create a DataObject out of it, perform the DoDragDrop and then delete the temporary file. Here are two &hellip; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4,3],"tags":[136,135],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-roemer","4":"post-392","6":"format-standard","7":"category-csharp","8":"category-programming","9":"post_tag-dataobject","10":"post_tag-dragdrop"},"acf":[],"views":4275,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/392"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=392"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}