C# 获取鼠标所在位置内容
比如当鼠标移动到一个文档中时,就获取该文档的文本内容;当鼠标移动到textbox时,就获取textbox的内容。这个用c#要怎么实现啊???
[解决办法]
using System;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices;namespace WindowsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("user32.dll", EntryPoint = "WindowFromPoint")] public static extern int WindowFromPoint( int xPoint, int yPoint ); [DllImport("user32.dll", EntryPoint = "GetCursorPos")] public static extern int GetCursorPos( ref System.Drawing.Point lpPoint ); [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage( int hwnd, int wMsg, int wParam, int lParam ); [DllImport("user32.dll", EntryPoint = "SendMessage")] public static extern int SendMessageString( int hwnd, int wMsg, int wParam, StringBuilder lParam ); public const int WM_GETTEXT = 0xD; public const int WM_GETTEXTLENGTH = 0xE; private void Form1_Load(object sender, EventArgs e) { timer1.Interval = 2500; timer1.Enabled = true; } // 我这里使用了时钟来获取鼠标的坐标 // 你有能力可以换成全局鼠标钩子 private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; System.Drawing.Point p = new Point(); GetCursorPos(ref p); int h=WindowFromPoint(p.X, p.Y); if (h != 0) { // 获取文本的长度 int iLegth = SendMessage(h, WM_GETTEXTLENGTH, 0, 0); if (iLegth > 0) { iLegth++; // 获取文本 StringBuilder sb = new StringBuilder(iLegth); SendMessageString(h, WM_GETTEXT, iLegth, sb); textBox1.Text = sb.ToString(); } } timer1.Enabled = true; } }}