如何获到其它进程鼠标选中的文字

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
    public enum ShellEvents
    {
        HSHELL_WINDOWCREATED = 1,
        HSHELL_WINDOWDESTROYED = 2,
        HSHELL_ACTIVATESHELLWINDOW = 3,
        HSHELL_WINDOWACTIVATED = 4,
        HSHELL_GETMINRECT = 5,
        HSHELL_REDRAW = 6,
        HSHELL_TASKMAN = 7,
        HSHELL_LANGUAGE = 8,
        HSHELL_ACCESSIBILITYSTATE = 11
    }
    public partial class Form1 : Form
    {

       
        [DllImport("user32.dll")]
        public static extern int RegisterWindowMessageA(string lpName);

        [DllImport("user32.dll")]
        public static extern int DeregisterShellHookWindow(IntPtr handle);

        [DllImport("user32.dll")]
        public static extern int RegisterShellHookWindow(IntPtr handle);

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(IntPtr handle);

        private int uMsgNotify;
        public IntPtr lastWindows;

        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SetForegroundWindow(lastWindows);
            System.Threading.Thread.Sleep(100);
            SendKeys.Send("^c");
            System.Threading.Thread.Sleep(100);

            IDataObject oData = Clipboard.GetDataObject();
            if (oData.GetDataPresent(DataFormats.Text))
                this.textBox1.Text = (String)oData.GetData(DataFormats.Text);
            else
                this.textBox1.Text = "Not Data";

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
            uMsgNotify = RegisterWindowMessageA("SHELLHOOK");
            RegisterShellHookWindow(this.Handle);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == uMsgNotify)
            {
                switch (m.WParam.ToInt32())
                {
                    case (int)ShellEvents.HSHELL_WINDOWACTIVATED:
                        IntPtr current = GetForegroundWindow();
                        Console.WriteLine("window activated:" + current.ToString());
                        if (current != IntPtr.Zero && current != this.Handle)
                            lastWindows = current;
                        break;
                    default:
                        break;
                }
            }
            base.WndProc(ref m);
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            DeregisterShellHookWindow(this.Handle);
        }
    }

}

原文地址:https://www.cnblogs.com/whisht/p/3750126.html