我在用C#做一个东西,想获取当把剪贴板数据粘贴到某个文件中时该文件的路径,请问可以怎么做呢?我在网上查到可以使用钩子,但这条路似乎走不通了,因为无法使用全局钩子捕获到WH_CALLWNDPROC消息,不知道还有没有其它的途径解决我的问题

解决方案 »

  1.   

    不知道~无法使用全局钩子捕获到WH_CALLWNDPROC消息
    全局勾子还有这个限制? 我去查查MSDN看看
      

  2.   

    WH_CALLWNDPROC and WH_CALLWNDPROCRET HooksThe WH_CALLWNDPROC and WH_CALLWNDPROCRET hooks enable you to monitor messages sent to window procedures. The system calls a WH_CALLWNDPROC hook procedure before passing the message to the receiving window procedure, and calls the WH_CALLWNDPROCRET hook procedure after the window procedure has processed the message.The WH_CALLWNDPROCRET hook passes a pointer to a CWPRETSTRUCT structure to the hook procedure. The structure contains the return value from the window procedure that processed the message, as well as the message parameters associated with the message. Subclassing the window does not work for messages set between processes.For more information, see the CallWndProc and CallWndRetProc functions. 抱歉,似乎没有 无法使用全局钩子捕获到WH_CALLWNDPROC消息  的描述 ... ..
      

  3.   

    “.NET的DLL没法被unmanaged code的进程调用,所以.NET无法写全局钩子。但在还是在KB中得到了答案,WH_KEYBOARD_LL和WH_MOUSE_LL是可以在.NET里面用的全局钩子。”我在几篇文章里都看到了类似的说法,我也做了实验,结果真的是这样,可以用全局钩子捕获WH_KEYBOARD_LL和WH_MOUSE_LL,但无法捕获其它的消息,不过在C++里是可以都捕获到的。所以现在我只好寻找其它的能用c#的解决办法,希望各位能指点一二,谢谢!
      

  4.   

    http://www.codeproject.com/KB/system/Clipboard_little_helper.aspx可以HOOK Clipboard的
    发现有对Clipboard操作时,先getforeground得到当前活动窗体的句柄HWND,
    最后枚举所有的进程进行比较,找到后显示操作Clipboard的文件的路径
               System.Diagnostics.Process[] pro=System.Diagnostics.Process.GetProcesses();
        
                foreach(System.Diagnostics.Process p1 in pro)
                {
                    if (p1.MainWindowHandle==HWND)
                    {
                    MessageBox.Show(p1.MainModule.FileName);
                    }
                }
      

  5.   

    .net 的 DLL 无法被本地代码调用?
    不会吧?
    用全局钩子,就要用要DLL,你是用 C#写 DLL 还是 C++?
      

  6.   

    可以使用Char.IsLetter()来判断是否是字母
      

  7.   

    回复only_lonely:我用c#写的。
    4楼的朋友提供的思路我正在尝试中,非常感谢你!
      

  8.   

    [DllImport("user32.dll")]        protected static extern int SetClipboardViewer(int hWndViewer);        [DllImport("user32.dll")]        protected static extern bool ChangeClipboardChain(IntPtr hWndResume, IntPtr hWndNext);        [DllImport("user32.dll")]        protected static extern int SendMessage(IntPtr hWnd, int nMsg, IntPtr wParam, IntPtr lParam);        IntPtr hNextClipboardViewer;//下一个监视的窗口        void ShowNotify()
            {            //MessageBox.Show(Clipboard.GetText());            textBox1.Text += Clipboard.GetText() + "\n";        }
            protected override void WndProc(ref Message m)
            {            if (m.Msg != 0)
                    textBox2.Text = m.Msg.ToString();            const int WM_DRAWCLPBORAD = 0x308;            const int WM_CHANGCBCHAIN = 0X030D;            //if (!textBox2.Text.Contains(m.Msg.ToString()))
                //    textBox2.Text = m.Msg.ToString();            switch (m.Msg)
                {                case WM_DRAWCLPBORAD:                    if (Clipboard.ContainsText())
                        {//包含文本格式                        ShowNotify();                    }
                        SendMessage(hNextClipboardViewer, m.Msg, m.WParam, m.LParam);                    break;                case WM_CHANGCBCHAIN:                    if (hNextClipboardViewer == m.WParam)
                        {//更新要发送消息的下一个窗口的句柄                        hNextClipboardViewer = m.LParam;                    }                    else
                        {                        SendMessage(hNextClipboardViewer, m.Msg, m.WParam, m.LParam);                    }                    break;                case 0x46:
                        //string[] strs = Clipboard.GetData(DataFormats.FileDrop) as string[];
                        //if (strs != null)
                        //textBox1.Text += (Clipboard.GetData(DataFormats.FileDrop) as string[])[0];
                        //if (strs != null)
                        //    foreach (string i in strs)
                        //    {
                        //        //if (!textBox1.Text.Contains(i))
                        //            textBox1.Text += i + "\n";
                        //    }                    break;                default:                    base.WndProc(ref m);                    break;            }        }
      

  9.   

    在case 0x46:下面的代码中
    //textBox1.Text += (Clipboard.GetData(DataFormats.FileDrop) as string[])[0];
      //if (strs != null)
      // foreach (string i in strs)
      // {
      // //if (!textBox1.Text.Contains(i))
      // textBox1.Text += i + "\n";
      // }
    取消注释可以实现部分功能。只是有些缺点。没解决。希望楼主看了帮忙解决下
      

  10.   

    错了 
      //string[] strs = Clipboard.GetData(DataFormats.FileDrop) as string[];
      //if (strs != null)
      //textBox1.Text += (Clipboard.GetData(DataFormats.FileDrop) as string[])[0];
      //if (strs != null)
      // foreach (string i in strs)
      // {
      // //if (!textBox1.Text.Contains(i))
      // textBox1.Text += i + "\n";
      // }
      string[] strs = Clipboard.GetData(DataFormats.FileDrop) as string[];
      //if (strs != null)
      //textBox1.Text += (Clipboard.GetData(DataFormats.FileDrop) as string[])[0];
      if (strs != null)
       foreach (string i in strs)
      {
      if (!textBox1.Text.Contains(i))
       textBox1.Text += i + "\n";
       }