public class MyForm : Form
{
[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;
}
}
博客展示了一段.NET代码,定义了MyForm类,通过DllImport引入user32.dll中的GetFocus方法,实现获取焦点控件的功能。若焦点控件不是.NET控件,返回值为null。
1168

被折叠的 条评论
为什么被折叠?



