PostMessage 似乎不起作用

发布于 2024-09-01 16:07:08 字数 1220 浏览 9 评论 0原文

我正在尝试使用 PostMessage 发送 Tab 键。

这是我的代码:

// This class allows us to send a tab key when the the enter key
//  is pressed for the mooseworks mask control.   
public class MaskKeyControl : MaskedEdit
{
//  [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
//  static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

    [return: MarshalAs(UnmanagedType.Bool)]
    // I am calling this on a Windows Mobile device so the dll is coredll.dll
    [DllImport("coredll.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, Int32 wParam, Int32 lParam);

    public const Int32 VK_TAB = 0x09;
    public const Int32 WM_KEYDOWN = 0x100;

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            PostMessage(this.Handle, WM_KEYDOWN, VK_TAB, 0);
            return;
        }
        base.OnKeyDown(e);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r') 
            e.Handled = true;
        base.OnKeyPress(e);
    }
}

当我按 Enter 时,代码被调用,但没有任何反应。然后我按 TAB 就可以了。 (所以我发送的 Tab Message 有问题。)

I am trying to use PostMessage to send a tab key.

Here is my code:

// This class allows us to send a tab key when the the enter key
//  is pressed for the mooseworks mask control.   
public class MaskKeyControl : MaskedEdit
{
//  [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
//  static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);

    [return: MarshalAs(UnmanagedType.Bool)]
    // I am calling this on a Windows Mobile device so the dll is coredll.dll
    [DllImport("coredll.dll", SetLastError = true)]
    static extern bool PostMessage(IntPtr hWnd, uint Msg, Int32 wParam, Int32 lParam);

    public const Int32 VK_TAB = 0x09;
    public const Int32 WM_KEYDOWN = 0x100;

    protected override void OnKeyDown(KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            PostMessage(this.Handle, WM_KEYDOWN, VK_TAB, 0);
            return;
        }
        base.OnKeyDown(e);
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        if (e.KeyChar == '\r') 
            e.Handled = true;
        base.OnKeyPress(e);
    }
}

When I press enter the code gets called, but nothing happens. Then I press TAB and it works fine. (So there is something wrong with my sending of the Tab Message.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

§普罗旺斯的薰衣草 2024-09-08 16:07:08

您确实不应该将与用户输入相关的 Windows 消息直接发布到 Windows 控件。相反,如果您想模拟输入,则应该依赖 SendInput API 函数 来代替发送按键。

另外,正如 Chris Taylor 在评论中提到的, SendKeys 类 可用于在您想要使用现有托管包装器的情况下将按键输入发送到应用程序(而不是通过 P/Invoke 层自行调用 SendInput 函数)。

You really shouldn't post windows messages related to user input directly to windows controls. Rather, if you want to simulate input, you should rely on the SendInput API function instead to send the key presses.

Also, as Chris Taylor mentions in his comment, the SendKeys class can be used to send key inputs to an application in the event that you want to use an existing managed wrapper (instead of calling SendInput function yourself through the P/Invoke layer).

深海里的那抹蓝 2024-09-08 16:07:08

关于关键事件的 PostMessage 确实做了非常奇怪的事情。

在这种情况下,也许使用 KEYDOWN、KEYPRESS、KEYUP(三个调用)的 SendMessage 可能效果更好。

PostMessage on key events does really really odd things.

In this case, maybe SendMessage with KEYDOWN, KEYPRESS, KEYUP (three calls) might work better.

薄荷→糖丶微凉 2024-09-08 16:07:08

作为将输入消息发送到控件的替代方法,您可以更明确地执行以下操作。

protected override void OnKeyDown(KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
  {
    if (Parent != null)
    {
      Control nextControl = Parent.GetNextControl(this, true);
      if (nextControl != null)
      {
        nextControl.Focus();
        return;
      }
    }
  }
  base.OnKeyDown(e);
}

当按下回车键时,这会将焦点设置到父级上的下一个控件。

An alternative to sending input messages to the control, you could be more explicit and do the following.

protected override void OnKeyDown(KeyEventArgs e)
{
  if (e.KeyCode == Keys.Enter)
  {
    if (Parent != null)
    {
      Control nextControl = Parent.GetNextControl(this, true);
      if (nextControl != null)
      {
        nextControl.Focus();
        return;
      }
    }
  }
  base.OnKeyDown(e);
}

This will set the focus to the next control on the parent when the enter key is pressed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文