C# 获取当前窗口中有焦点的控件

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]

internal static extern IntPtr GetFocus();

private Control GetFocusedControl()
{
Control focusedControl = null;
// To get hold of the focused control:
IntPtr focusedHandle = GetFocus();
if (focusedHandle != IntPtr.Zero)
// Note that if the focused Control is not a .Net control, then this will return null.
focusedControl = Control.FromHandle(focusedHandle);
return focusedControl;
}

  

posted @ 2021-05-11 16:30  奥兰王子  阅读(1348)  评论(0编辑  收藏  举报