C# 如何获取鼠标下的控件?

king_827 2006-12-04 02:52:48
C# 如何获取鼠标下的控件?
...全文
238 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京的雾霾天 2006-12-07
  • 打赏
  • 举报
回复
那就用API了:
[StructLayout(LayoutKind.Sequential)]
private struct POINTSTRUCT
{
public int x;
public int y;
public POINTSTRUCT(int x, int y);
}


[DllImport("user32.dll", EntryPoint="WindowFromPoint", CharSet=CharSet.Auto, ExactSpelling=true)]
private static extern IntPtr _WindowFromPoint(UnsafeNativeMethods.POINTSTRUCT pt);

king_827 2006-12-07
  • 打赏
  • 举报
回复
谢谢,不过,它只能获取主容器,多级子容器就获取不了,还是我操作出错.

请多指教
北京的雾霾天 2006-12-04
  • 打赏
  • 举报
回复
应该是这样的:
Point msPoint = Control.MousePosition;
Control tmpControl = this.GetChildAtPoint(this.PointToClient(msPoint));
if (tmpControl != null)
{

}

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧