using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace Key { public partial class Form1 : Form { IntPtr ptr = (IntPtr)null; [DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("user32.dll")] static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui); [DllImport("user32.dll")] static extern IntPtr SetFocus(IntPtr hWnd); [StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rectCaret; } bool Find = false; public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd) { if (hwnd != IntPtr.Zero) { uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero); GUITHREADINFO guiThreadInfo = new GUITHREADINFO(); guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo); if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false) return null; return guiThreadInfo; } return null; } public Form1() { InitializeComponent();
不要用this.ActiveControl哦
一般人我不告诉他!
哈哈你这样做肯定不行的,你点击button的时候,焦点就到了button上了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Key
{
public partial class Form1 : Form
{ IntPtr ptr = (IntPtr)null;
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("user32.dll")] static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui); [DllImport("user32.dll")]
static extern IntPtr SetFocus(IntPtr hWnd); [StructLayout(LayoutKind.Sequential)] public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rectCaret; }
bool Find = false;
public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd) { if (hwnd != IntPtr.Zero) {
uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero); GUITHREADINFO guiThreadInfo = new GUITHREADINFO(); guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo); if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false) return null; return guiThreadInfo; } return null; }
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 100;
timer1.Tick += timer1_Tick;
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
if (Find == false)
{
IntPtr hwnd = GetForegroundWindow(); GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd); if (guiInfo != null)
{ ptr = (IntPtr)guiInfo.Value.hwndCaret; if (ptr != IntPtr.Zero && ptr != this.textBox1.Handle)
{
this.textBox1.Focus();
this.Visible = true;
Find = true;
SetFocus(this.Handle);
textBox1.Text = "";
} }
}
} private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
} private void button1_Click(object sender, EventArgs e)
{
this.Visible = false;
SetFocus(this.Handle); string text = textBox1.Text;
for (int i = 0; i < text.Length; i++)
{
SendMessage(ptr, 0x0102, (IntPtr)(int)text[i], IntPtr.Zero); }
Find = false;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
// 记录最后一次获得焦点的Textbox
private TextBox _lastFocusTextBox;
然后可以在所有的Textbox的enter事件里写上:
_lastFocusTextBox = (TextBox)sender;
这样在Button_click里,就可以通过_lastFocusTextBox知道,刚刚是在哪一个TextBox呆过了。
ActiveControl我没测试过不敢说,如果用户点击的是按钮,那ActiveControl还会是输入框吗,应该会变成按钮了吧?未测试过,纯路过好奇问一下。