gpt4 book ai didi

c# - 如何将事件添加到 Windows 消息泵队列中?

转载 作者:太空宇宙 更新时间:2023-11-03 20:06:19 24 4
gpt4 key购买 nike

使用 VB.NET/C# - .Net Framework 3.5。我目前在 WinForms 事件的处理程序中(例如)。在处理程序中,我想为 Windows 消息泵放置我自己的事件,但在其队列的末尾(假设它泵消息 FIFO)。我希望我的处理程序和当前事件的任何其他处理程序都可以执行,然后选择我插入到队列中的自定义事件并调用它的处理程序。关于如何将事件插入 Windows 事件队列的任何示例代码(不关心什么语言)?

最佳答案

如果你想添加一条消息到队列,那么你只需要P/Invoke PostMessage function .如文档所述,此功能:

Places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

示例 P/调用声明:

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
  • hWnd 是您窗口的句柄,它的窗口过程应该接收消息。在 WinForms 中,可以使用 Handle 属性检索它。

  • msg 是您要发布的消息。当然,您需要弄清楚在这里使用哪一个。如果其中一条标准消息适合您的目的,那么您可以使用它。否则,只需定义一个用户定义的消息。用户定义的消息的值范围 >= WM_USER。所以:

    const uint WM_USER = 0x0400;
    const uint UWM_MYGREATMESSAGE = WM_USER + 1;
    const uint UWM_MYOTHERMESSAGE = WM_USER + 2;
  • 显然,wParamlParam 包含特定于消息的信息。如果您使用的是标准消息之一,它们的文档将告诉您在此处指定的内容。如果您使用的是用户定义的消息,那么您几乎可以在此处传递您想要的任何内容,包括 IntPtr.Zero(如果您不需要任何自定义数据)。

关于c# - 如何将事件添加到 Windows 消息泵队列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23255180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com