欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > C# >内容正文

C#

C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上...

发布时间:2025/3/21 C# 52 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

转:/**********************

 * 课题:将窗体中鼠标所在控件名显示在窗体标题上
 * 作者:ZAXX
 * QQ : 94027486
 * 本课题可简单扩展:获取屏幕上鼠标所在位置的窗口句柄与类名
 **********************/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Zaxx
{
  public partial class MouseOnControl: Form
  {
  [DllImport("User32.dll")]
  private static extern IntPtr WindowFromPoint(Point p);

  Timer timer;

  public MouseOnControl()
  {
  InitializeComponent();
  timer = new Timer();
  timer.Interval = 200;
  timer.Enabled = true;
  timer.Tick += new EventHandler(timer_Tick);
  }

  void timer_Tick(object sender, EventArgs e)
  {
  Point p = Cursor.Position;
  IntPtr h = WindowFromPoint(p);
  foreach (Control con in this.Controls)
  if (con.Handle == h)
  this.Text = con.Name;
  }
  }
}

转载于:https://www.cnblogs.com/08shiyan/archive/2011/04/14/2015758.html

总结

以上是生活随笔为你收集整理的C# WinForm中 获得当前鼠标所在控件 或 将窗体中鼠标所在控件名显示在窗体标题上...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。