string str = textBox1.Text;
Clipboard.SetText(str);
SendMessage((IntPtr)Win32Api.GetForegroundWindow(), WM_PASTE, 0, str);向外部程序Text传送字符,可以传到QQ的对话框,但是没法传到txt记事本和WORD。用SPY++追踪后发现,QQ对话框传递了WM_PASTE语句,但是txt记事本却什么都没有。是代码出问题了么,该怎么改
Clipboard.SetText(str);
SendMessage((IntPtr)Win32Api.GetForegroundWindow(), WM_PASTE, 0, str);向外部程序Text传送字符,可以传到QQ的对话框,但是没法传到txt记事本和WORD。用SPY++追踪后发现,QQ对话框传递了WM_PASTE语句,但是txt记事本却什么都没有。是代码出问题了么,该怎么改
至于Word,不会响应WM_PASTE,你需要模拟发送Ctrl+V。
我只是想让他传到光标所在的地方,手动按Ctrl+v,就可以。
static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui); 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))
return null;
return guiThreadInfo;
}
return null;
}
const int SW_RESTORE = 9; const int WM_PASTE = 0x302;
const int WM_CUT = 0x300;
const int WM_COPY = 0x301; [StructLayout(LayoutKind.Sequential)]
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;
} [StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
IntPtr hwnd = GetForegroundWindow();
GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
if (guiInfo.HasValue)
SendMessage(guiInfo.Value.hwndFocus, WM_PASTE, 0, 0);