string str = textBox1.Text;
     Clipboard.SetText(str);
     SendMessage((IntPtr)Win32Api.GetForegroundWindow(), WM_PASTE, 0, str);向外部程序Text传送字符,可以传到QQ的对话框,但是没法传到txt记事本和WORD。用SPY++追踪后发现,QQ对话框传递了WM_PASTE语句,但是txt记事本却什么都没有。是代码出问题了么,该怎么改

解决方案 »

  1.   

    GetForegroundWindow()获取的是什么的句柄,你用spy++看看是记事本文字录入对象的句柄吗
      

  2.   

    http://download.csdn.net/detail/wawd74520/5004669这里面有3个  除了  spy++其它可以抓QQ的窗体
      

  3.   

    当然,你应该往记事本的TextBox里面发,而不是往记事本主窗口发。
    至于Word,不会响应WM_PASTE,你需要模拟发送Ctrl+V。
      

  4.   

    我我要应该怎么改呢?你说的没错(IntPtr)Win32Api.GetForegroundWindow() 这句话是获取主窗口的句柄
      

  5.   

    继续往下,GetChildWindow(),找编辑器的窗口啊。
      

  6.   


    我只是想让他传到光标所在的地方,手动按Ctrl+v,就可以。
      

  7.   

            [DllImport("user32.dll")]
            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);
      

  8.   

    因为我要传递的那个窗口有不只一个Text,所以打算是根据光标焦点来传。直接复制过去