为什么C#PostMessage不会发送VK_Return iff窗口被聚焦?

时间:2016-07-03 15:55:04

标签: c# windows sendmessage postmessage

我一直在做一个程序来发送文本并在正在运行的后台程序上输入密钥,sendmessage (string)工作正常,但是在我调用postmessage(VK_ENTER)之后,它只有在窗口聚焦时才有效

这是C#中的代码(Visual Studio 2016)

    [return: MarshalAs(UnmanagedType.Bool)]
    [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
    private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);

    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, uint wParam, uint lParam);

    private static string SetText(IntPtr handle)
    {
        const int VK_RETURN = 0x0D;
        const int WM_KEYDOWN = 0x100;

        SendMessage(handle, 0x000C, 0, textSend);

        PostMessage(handle, WM_KEYDOWN, VK_RETURN, IntPtr.Zero);


    }

此外,我已尝试使用Sendmessage并且结果相同(仅当窗口未聚焦时才会发送VK_RETURN)。

0 个答案:

没有答案