跟踪拖放处理的全局鼠标事件

时间:2012-09-25 12:20:12

标签: c# events mouse hook

  

可能重复:
  Drag Drop from .NET application to Explorer

在我的应用程序中,要求用户具有拖动和放置的能力。从文件从ListView上删除到Windows资源管理器。这个文件位于服务器上,所以我需要下载它们来放置哪个用户点。为此,我决定设置钩子WH_MOUSE_LL来跟踪全局鼠标事件,以获取用户放下文件的文件夹的句柄,然后使用它来获取加载文件的绝对路径。这是带有HOOK函数的id DLL:

        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }


        private enum MouseMessages
        {
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205
        }


        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode,
        IntPtr wParam, IntPtr lParam);

        public int LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            //Marshall the data from the callback.
            //MouseHookStruct MyMouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct));

            MSLLHOOKSTRUCT MyMouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

            if (nCode >= 0 &&
            MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
            {
                String strCaption = "x = " +
                        MyMouseHookStruct.pt.x.ToString("d") +
                        "  y = " +
                MyMouseHookStruct.pt.y.ToString("d");
                //You must get the active form because it is a static function.
                Console.WriteLine(strCaption);

            }
            return CallNextHookEx(hHook, nCode, wParam, lParam);


        }
    }
}

然后我用SetWindowsHookEx注册它:

[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
IntPtr pDll = LoadLibrary(@"C:\Users\вова\Documents\visual studio    2010\Projects\MouseHook\MouseHook\bin\Debug\MouseHook.dll");
MouseHookProcedure = new HookProc(msHookObj.LowLevelMouseProc);

hHook = SetWindowsHookEx(WH_MOUSE,
            MouseHookProcedure,
            pDll, 
            0);

结果我的应用程序不跟踪全局事件。请帮忙,我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

我决定使用你的建议重新拖放,所以我开始使用Shell API。但我有问题:由于我的文件不在系统中,我不能使用CF_HDROP格式进行传输。 所以我理解我必须使用CFSTR_FILECONTENTS。

以下是我的代码:

    public class DataObjectEx :
        DataObject, System.Runtime.InteropServices.ComTypes.IDataObject
    {
        private static readonly TYMED[] ALLOWED_TYMEDS =
            new TYMED[] { 
            TYMED.TYMED_ENHMF,
            TYMED.TYMED_GDI,
            TYMED.TYMED_HGLOBAL,
            TYMED.TYMED_ISTREAM, 
            TYMED.TYMED_MFPICT};
        public void FormDataObject(ListViewItem item)
        {
            var formatmumber=(Int16)DataFormats.GetFormat(NativeMethods.CFSTR_FILECONTENTS).Id;

            //формируем структуру для shell
            formatetc = new FORMATETC();
            formatetc.cfFormat = formatmumber;
            formatetc.ptd = IntPtr.Zero;
            formatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;
            formatetc.lindex = -1;
            formatetc.tymed = TYMED.TYMED_ISTREAM;

            //формируем структуру для передачи данных в shell
            medium = new STGMEDIUM();
            medium.tymed = TYMED.TYMED_ISTREAM;
            MemoryStream = GetFileContents(item);
            //IStream ob1;
            medium.unionmember = Marshal.GetComInterfaceForObject(iStream, typeof(MemoryStream));
            //medium.unionmember = iStream;


            ((System.Runtime.InteropServices.ComTypes.IDataObject)ob).SetData(ref formatetc, ref medium, true);

        }

在行中:IStream iStream =(IStream)GetFileContents(item); - 函数GetFileContents(item) - 连接到服务器并发送文件下载请求

public static MemoryStream  get_file_Istream(string file_id)
    {
        string data = "cmd=get_file&id=" + file_id + "&token=" + Token.curent_token;
        string Uri = URL.address;

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri);

        request.Method = "POST";
        request.AllowAutoRedirect = true;
        request.ContentType = "application/x-www-form-urlencoded";
        byte[] EncodedPostParams = Encoding.UTF8.GetBytes(data);
        request.ContentLength = EncodedPostParams.Length;
        request.GetRequestStream().Write(EncodedPostParams, 0, EncodedPostParams.Length);
        request.GetRequestStream().Close();


        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        MemoryStream mm = new MemoryStream();
        response.GetResponseStream().CopyTo(mm);

        return mm;
        //ResponseStream = response.GetResponseStream();
    }

我从未做过这样的事情,只是大致了解它是如何运作的。 我接下来的错误:

-Type传递必须是一个接口。 参数名称:t。在这一行:medium.unionmember = Marshal.GetComInterfaceForObject(iStream,typeof(MemoryStream));

- 未实施方法或操作。在这一行:((System.Runtime.InteropServices.ComTypes.IDataObject)ob).SetData(ref formatetc,ref medium,true);

从我理解的最后一个错误 - 我的实施方法SetDate,但我没有找到任何互联网信息。至于第一个错误,我认为它与错误的参数类型相关联(MemoryStram insted Istream)

有人有想法如何解决它们吗?

Thenaks