-2

I want to capture Clipboard changes in win-forms. Only Capture Hyper-Links for My Program..

I am Creating a Download Manager. so I have to implement this Feature in That.
(When ever i copy any Hyper-Link to clipboard My Down-loader shows me the New Download Dialog Containing that link).

Sajjad Javed
  • 139
  • 2
  • 14
  • Yeah well, we are not going to do your work for you. That's a really lazy question. – JK. Jul 07 '15 at 03:15
  • We're not a code writing service. Please show us what you've written and we can help with that. – Enigmativity Jul 07 '15 at 04:11
  • I have tried Codeplex for that & a little google about that... – Sajjad Javed Jul 07 '15 at 05:04
  • There i have found some DLL's i am implemented that In my Project He's working (Monitoring Changes) but Did't filtering Http, Https Links for my Requirements... I appreciate your Effort But i am really did't understand that...(I'm not getting). :( – Sajjad Javed Jul 07 '15 at 05:07

2 Answers2

0

To read data from clipboard read:

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

or

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

To check if a string is valid Url read: How to check whether a string is a valid HTTP URL?

To monitor clipboard for any changes read: Clipboard event C#

Now you can write your code as per your need.

Community
  • 1
  • 1
ATP
  • 553
  • 1
  • 3
  • 16
0

Here's the URL checker linked by Ashutosh Pandey plugged into my existing implementation of the ClipBoard chain 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);
    }

}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40