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;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</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>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// Add event handlers for the drag &amp; 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;">&#40;</span>Form_DragEnter<span style="color: #000000;">&#41;</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;">&#40;</span>Form_DragDrop<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<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;">&#40;</span><span style="color: #FF0000;">object</span> sender, DragEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</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;">&#40;</span>e.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">GetDataPresent</span><span style="color: #000000;">&#40;</span>DataFormats.<span style="color: #0000FF;">FileDrop</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</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>
&nbsp;
<span style="color: #000000;">&#125;</span>
&nbsp;
<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;">&#40;</span><span style="color: #FF0000;">object</span> sender, DragEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</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;">&#91;</span><span style="color: #000000;">&#93;</span> FileList = <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>e.<span style="color: #0000FF;">Data</span>.<span style="color: #0000FF;">GetData</span><span style="color: #000000;">&#40;</span>DataFormats.<span style="color: #0000FF;">FileDrop</span>, <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>;
&nbsp;
    <span style="color: #008080; font-style: italic;">// Do something with the data...</span>
&nbsp;
    <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;">&#40;</span><span style="color: #FF0000;">string</span> File <span style="color: #0600FF;">in</span> FileList<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">label</span>.<span style="color: #0000FF;">Text</span> += File + <span style="color: #808080;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #000000;">&#125;</span>

以上是关于csharp 简单的拖放示例的主要内容,如果未能解决你的问题,请参考以下文章

Swift 中的拖放 - 注册拖动类型的问题?

简单的拖放代码

ActionScript 3 actionscript 3简单的拖放

iOS 中的拖放功能

带手柄的拖放

jQuery - 可拖动克隆上的拖放视觉反馈