好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Winform控件Picture实现图片拖拽显示效果

最近做了一个小工具,在winform中对picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片!

首先你需要对你的整个fom窗口的allowdrop设置ture

?

//函数从动态链接库中倒入(模拟鼠标事件)

 

[system.runtime.interopservices.dllimport( "user32" )]

  private static extern int mouse_event( int dwflags, int dx, int dy, int cbuttons, int dwextrainfo);

  const int mouseeventf_leftdown = 0x0002; //模拟鼠标左键按下

 

   const int mouseeventf_leftup = 0x0004; //模拟鼠标左键抬起

     //设置静态字段传递图片路径参数

     public static string path_url;

     //获取鼠标拖入图片的绝对路径

     private void form1_dragdrop( object sender, drageventargs e)

     {

       //获取当前推拽图片的路径

       string path1 = ((system.array)e.data.getdata(dataformats.filedrop)).getvalue(0).tostring(); ;

       path_url = path1;

       //模拟鼠标释放鼠标左键的时事件

       mouse_event(mouseeventf_leftup | mouseeventf_leftup, cursor.position.x, cursor.position.y, 0, 0);

     }

     //判断鼠标拖入文件的类型判断是不是文件类型

     private void form1_dragenter( object sender, drageventargs e)

     {

       if (e.data.getdatapresent(dataformats.filedrop))

         //需求有一需要从qq的聊天记录中拖拽图片到winform窗体中,用all会出现qq的聊天信息中的图片丢失

         //link和move不能从qq的聊天记录中拖拽图片到winform窗体中,copy和scroll都可以实现,推荐使用copy

         e.effect = dragdropeffects.copy;                             

       else

         e.effect = dragdropeffects.none;

}

在来设置picturebox的事件

?

//当鼠标在当前控释放的时候触发控件

private void pic_1_mouseup( object sender, mouseeventargs e)

{

    //给picturebox设置图片路径

    pic_1.imagelocation = path_url;

}

以上就可以完成推拽图片显示图片(无论是本地还是qq消息框中的图片都可以实现)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://HdhCmsTestcnblogs测试数据/2828sea/archive/2018/09/20/9682937.html

dy("nrwz");

查看更多关于Winform控件Picture实现图片拖拽显示效果的详细内容...

  阅读:57次