1、是SetCapture,以便鼠标在窗口外的点击也能发送到本窗口,
2、是用WindowFromPoint找到目标窗口,
3、也就是GetWindowText了。
简单例子如下: public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Text = "Start Capture";
} private void button1_Click(object sender, EventArgs e)
{
this.button1.Text = "Capturing...";
this.Text = "Now click on the target window";
this.Capture = true;
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.button1.Text = "Start Capture"; if (this.Capture)
{
IntPtr target = WindowFromPoint(this.PointToScreen(e.Location));
if (target != IntPtr.Zero)
{
StringBuilder sb = new StringBuilder(256);
GetWindowTextW(target, sb, 256);
this.Text = sb.ToString();
}
}
}
[DllImport("User32")]
static extern IntPtr WindowFromPoint(Point point); [DllImport("User32", CharSet = CharSet.Unicode)]
static extern int GetWindowTextW(IntPtr hWnd, StringBuilder sb, int maxCount);
}
2、是用WindowFromPoint找到目标窗口,
3、也就是GetWindowText了。
简单例子如下: public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Text = "Start Capture";
} private void button1_Click(object sender, EventArgs e)
{
this.button1.Text = "Capturing...";
this.Text = "Now click on the target window";
this.Capture = true;
} protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.button1.Text = "Start Capture"; if (this.Capture)
{
IntPtr target = WindowFromPoint(this.PointToScreen(e.Location));
if (target != IntPtr.Zero)
{
StringBuilder sb = new StringBuilder(256);
GetWindowTextW(target, sb, 256);
this.Text = sb.ToString();
}
}
}
[DllImport("User32")]
static extern IntPtr WindowFromPoint(Point point); [DllImport("User32", CharSet = CharSet.Unicode)]
static extern int GetWindowTextW(IntPtr hWnd, StringBuilder sb, int maxCount);
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货