使用C#进行剪贴板操作 [英] Clipboard Operation with C#

查看:149
本文介绍了使用C#进行剪贴板操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我正在运行一个用C#制作的隐藏窗口应用程序。

如果用户从任何地方复制一个号码,一个窗口Popup将显示Call ######?使用呼叫和取消按钮....



Hi friends,

I am running a hidden windows application made in C#.
If the user copies a number from anywhere, A window Popup will come and show "Call ######?" with a Call and Cancel Button....

public partial class Form1 : Form
 {
     string LastClipBoardText = "";
 private void timer2_Tick(object sender, EventArgs e)
     {


         List<string> phone_nums = new List<string>();
         if (Clipboard.ContainsText())
         {

             if (LastClipBoardText != Clipboard.GetText(TextDataFormat.Text))
             {
                     LastClipBoardText =  Clipboard.GetText(TextDataFormat.Text);
         //Some code here
     }
      }
 }
   }



我正在使用LastClipboardText来避免一次又一次地检查相同的数据。它运作良好...

问题来了,当客户再次使用鼠标复制相同的文本时......:D他希望窗口再次出现;但不会,因为它是存储在LastCLipboardText中的相同数据....



有什么想法可以解决这个问题吗?我可以从剪贴板中获取复制的时间???


I am using LastClipboardText to avoid checking again and again for the same data. and It works well...
Problem comes, when a customer again copies the same text with mouse... :D He expects the window again to show up; But won't, as it is the same data stored in LastCLipboardText....

Is there any idea to solve this issue? Can I get the copied time from clipboard ???

推荐答案

我认为你必须使用一些p / invoke:



I think you'll have to use some p/invoke:

[DllImport("User32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);





请参阅 [ ^ ]有关如何在c#中设置剪贴板监视器的文章。基本上你使用





See this [^] article on how to set up a clipboard monitor in c#. Basically you register your app as a clipboard viewer using

_ClipboardViewerNext = SetClipboardViewer(this.Handle);



然后您将收到WM_DRAWCLIPBOARD消息,您可以通过覆盖WndProc来处理:




and then you will recieve the WM_DRAWCLIPBOARD message, which you can handle by overriding WndProc:

protected override void WndProc(ref Message m)
{
    switch ((Win32.Msgs)m.Msg)
    {
        case Win32.Msgs.WM_DRAWCLIPBOARD:
        // Handle clipboard changed
        break;
        // ... 
   }
}


我建​​议您了解.NET中的GlobalHooks:[ ^ ]。



如果结束 - 用户通过复制一些文本,然后按某些键盘组合来触发您的GlobalHook操作:您有一个很大的优势,即您不必解析每个数字用户副本,而您的代码不会无论要复制什么对象,都必须将每个副本都响应到剪贴板。
I suggest you learn about GlobalHooks in .NET: [^].

If the end-user triggers your GlobalHook action by copying some text, and then pressing some keyboard combination: you have the great advantage that you don't have to parse every single number the user copies, and your code doesn't have to respond to every single copy to the clipboard, no matter what object is being copied.


这篇关于使用C#进行剪贴板操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆