Sendmessage wm_paste文本框

时间:2014-06-30 21:32:55

标签: vb.net

我正在尝试从另一个窗口复制文本,然后使用SendMessage将文本粘贴到文本框中。我尝试过使用:

textBox1.Paste()

textBox1.text = Clipboard.GetText()

但似乎这些粘贴函数在sendmessage api之前被调用,因此我希望sendmessage api粘贴到文本框中,以便按需要的事件顺序进行。

SendMessage(1508866, WM_COPY, 0, 0)
SendMessage(textBox1.handle, WM_PASTE, 0, 0) ' Does not paste anything in textbox.

编辑:

这是我的代码。注意,剪贴板方法在发送消息之前触发。

AppActivate("Hyperspace")
        SetCursorPos(2271,214) ' Request
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        SetCursorPos(2726,111) ' Properties
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        SetCursorPos(2681,792) ' Get EOW
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        SetCursorPos(2853,525) ' Highlight EOW
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        SendMessage(1508866, WM_COPY, 0, 0)
        textBox2.Text = Clipboard.GetText()

        SetCursorPos(2983,719) ' Close
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

        SetCursorPos(2967,783) ' Accept
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

1 个答案:

答案 0 :(得分:0)

SendMessage()是同步的。在接收窗口处理消息之前,它不会返回:

SendMessage(1508866, WM_COPY, 0, 0)
TextBox1.Text = Clipboard.GetText()

但你为什么要涉及剪贴板呢?如果您拥有外部窗口的HWND,则可以使用WM_GETTEXT检索其文本,然后将其分配给Text的{​​{1}}属性。不需要剪贴板。