欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Winform中Picture控件图片的拖拽显示

程序员文章站 2023-04-06 22:35:43
注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! //函数从动态链接库中倒入(模拟鼠标事件) 4.在来设置PictureBox的事件 5.以上就可以完成推拽图片显示图片(无论是本地还是QQ消息框中的图片都可以实现) ......

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

Winform中Picture控件图片的拖拽显示

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

         Winform中Picture控件图片的拖拽显示

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

       [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; }

  Winform中Picture控件图片的拖拽显示

     4.在来设置picturebox的事件

    Winform中Picture控件图片的拖拽显示

//当鼠标在当前控释放的时候触发控件
private void pic_1_mouseup(object sender, mouseeventargs e) {
//给picturebox设置图片路径 pic_1.imagelocation = path_url; }

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