如何监视C#中的剪贴板更改以捕获仅超链接

时间:2015-07-07 02:46:16

标签: c# winforms

我想捕获win-forms中的剪贴板更改。只有捕获超链接才能获得我的程序..

我正在创建下载管理器。所以我必须在那里实现这个功能 (当我将任何Hyper-Link复制到剪贴板时,我的Down-loader向我显示包含该链接的New Download Dialog)。

2 个答案:

答案 0 :(得分:0)

从剪贴板读取数据:

https://msdn.microsoft.com/en-us/library/kz40084e(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/c2thcsx4(v=vs.110).aspx

检查字符串是否有效Url阅读: How to check whether a string is a valid HTTP URL?

要监控剪贴板是否有任何更改: Clipboard event C#

现在您可以根据需要编写代码。

答案 1 :(得分:0)

这是由Ashutosh Pandey链接的URL检查器插入到我现有的ClipBoard链API实现中:

public partial class Form1 : Form
{

    private ClipBoardMonitor cbm = null;

    public Form1()
    {
        InitializeComponent();
        cbm = new ClipBoardMonitor();
        cbm.NewUrl += cbm_NewUrl;
    }

    private void cbm_NewUrl(string txt)
    {
        this.label1.Text = txt;
    }

}

public class ClipBoardMonitor : NativeWindow
{

    private const int WM_DESTROY = 0x2;
    private const int WM_DRAWCLIPBOARD = 0x308;
    private const int WM_CHANGECBCHAIN = 0x30d;

    [DllImport("user32.dll")]
    private static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
    [DllImport("user32.dll")]
    private static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    public event NewUrlHandler NewUrl;
    public delegate void NewUrlHandler(string txt);

    private IntPtr NextClipBoardViewerHandle;

    public ClipBoardMonitor()
    {
        this.CreateHandle(new CreateParams());
        this.NextClipBoardViewerHandle = SetClipboardViewer(this.Handle);
    }

    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_DRAWCLIPBOARD:
                if (Clipboard.ContainsText())
                {
                    string txt = Clipboard.GetText();
                    if (this.NewUrl != null && this.IsValidUrl(txt))
                    {
                        this.NewUrl(txt);
                    }
                }
                SendMessage(this.NextClipBoardViewerHandle, m.Msg, m.WParam, m.LParam);

                break;

            case WM_CHANGECBCHAIN:
                if (m.WParam.Equals(this.NextClipBoardViewerHandle))
                {
                    this.NextClipBoardViewerHandle = m.LParam;
                }
                else if (!this.NextClipBoardViewerHandle.Equals(IntPtr.Zero))
                {
                    SendMessage(this.NextClipBoardViewerHandle, m.Msg, m.WParam, m.LParam);
                }
                break;

            case WM_DESTROY:
                ChangeClipboardChain(this.Handle, this.NextClipBoardViewerHandle);
                break;

        }

        base.WndProc(ref m);
    }

    private bool IsValidUrl(string txt)
    {
        Uri uriResult;
        return Uri.TryCreate(txt, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
    }

}