csharp 简单的拖放示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 简单的拖放示例相关的知识,希望对你有一定的参考价值。
<span style="color: #008080; font-style: italic;">// Form load event or a similar place</span>
<span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Form_Load<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// Enable drag and drop for this form</span>
<span style="color: #008080; font-style: italic;">// (this can also be applied to any controls)</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">AllowDrop</span> = <span style="color: #0600FF;">true</span>;
<span style="color: #008080; font-style: italic;">// Add event handlers for the drag & drop functionality</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DragEnter</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DragEventHandler<span style="color: #000000;">(</span>Form_DragEnter<span style="color: #000000;">)</span>;
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DragDrop</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DragEventHandler<span style="color: #000000;">(</span>Form_DragDrop<span style="color: #000000;">)</span>;
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">// This event occurs when the user drags over the form with </span>
<span style="color: #008080; font-style: italic;">// the mouse during a drag drop operation </span>
<span style="color: #0600FF;">void</span> Form_DragEnter<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> sender, DragEventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// Check if the Dataformat of the data can be accepted</span>
<span style="color: #008080; font-style: italic;">// (we only accept file drops from Explorer, etc.)</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">(</span>e.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">GetDataPresent</span><span style="color: #000000;">(</span>DataFormats.<span style="color: #0000FF;">FileDrop</span><span style="color: #000000;">)</span><span style="color: #000000;">)</span>
e.<span style="color: #0000FF;">Effect</span> = DragDropEffects.<span style="color: #0000FF;">Copy</span>; <span style="color: #008080; font-style: italic;">// Okay</span>
<span style="color: #0600FF;">else</span>
e.<span style="color: #0000FF;">Effect</span> = DragDropEffects.<span style="color: #0000FF;">None</span>; <span style="color: #008080; font-style: italic;">// Unknown data, ignore it</span>
<span style="color: #000000;">}</span>
<span style="color: #008080; font-style: italic;">// Occurs when the user releases the mouse over the drop target </span>
<span style="color: #0600FF;">void</span> Form_DragDrop<span style="color: #000000;">(</span><span style="color: #FF0000;">object</span> sender, DragEventArgs e<span style="color: #000000;">)</span>
<span style="color: #000000;">{</span>
<span style="color: #008080; font-style: italic;">// Extract the data from the DataObject-Container into a string list</span>
<span style="color: #FF0000;">string</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span> FileList = <span style="color: #000000;">(</span><span style="color: #FF0000;">string</span><span style="color: #000000;">[</span><span style="color: #000000;">]</span><span style="color: #000000;">)</span>e.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">(</span>DataFormats.<span style="color: #0000FF;">FileDrop</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">)</span>;
<span style="color: #008080; font-style: italic;">// Do something with the data...</span>
<span style="color: #008080; font-style: italic;">// For example add all files into a simple label control:</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">(</span><span style="color: #FF0000;">string</span> File <span style="color: #0600FF;">in</span> FileList<span style="color: #000000;">)</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">label</span>.<span style="color: #0000FF;">Text</span> += File + <span style="color: #808080;">"<span style="color: #008080; font-weight: bold;">\n</span>"</span>;
<span style="color: #000000;">}</span>
以上是关于csharp 简单的拖放示例的主要内容,如果未能解决你的问题,请参考以下文章