The .Net framework libraries does not provide you an API to query for the focused Control. You have to invoke a windows API to do so: |
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);
|
转载于:https://www.cnblogs.com/rickiedu/archive/2009/05/19/1460357.html