111,088
社区成员




[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);
[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;
}
public static GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
{
if (hwnd != IntPtr.Zero)
{
//Mbox.Info(GetTitle(hwnd), "O");
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 static void SendText(string text)
{
IntPtr hwnd = GetForegroundWindow();
if (String.IsNullOrEmpty(text))
return;
Hwindow.GUITHREADINFO? guiInfo = Hwindow.GetGuiThreadInfo(hwnd);
if (guiInfo != null)
{
IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret;
if (ptr != IntPtr.Zero)
{
for (int i = 0; i < text.Length; i++)
{
SendMessage(ptr, Message.WM_CHAR, (IntPtr)(int)text[i], IntPtr.Zero);
}
}
}
}