【问题标题】:Where are the rest of the TMessage missing params?其余的 TMessage 缺少参数在哪里?
【发布时间】:2020-10-23 20:56:04
【问题描述】:

VCL TMessage 类提供Message、WParam 和LParam 成员,但窗口消息有更多成员:

typedef struct tagMSG {
  HWND   hwnd;
  UINT   message;
  WPARAM wParam;
  LPARAM lParam;
  DWORD  time;
  POINT  pt;
  DWORD  lPrivate;
} MSG, *PMSG, *NPMSG, *LPMSG;

hwnd、time、pt 和 lPrivate 成员在哪里?我特别需要time 参数。

有没有办法访问构造 TMessage 的原始消息(或任何其他获取 time 参数的方法)?

我正在处理我在TComponent::WndProc(Winapi::Messages::TMessage &Message) 中的消息。

【问题讨论】:

    标签: winapi message vcl c++builder-10.4-sydney


    【解决方案1】:

    hwnd、time、pt 和 lPrivate 成员在哪里?

    TMessage 中没有这样的成员。

    MSG 是 Win32 API 在message loop 中使用的结构,用于通过GetMessage() 和PeekMessage() 函数从message queue 检索消息,然后再通过@987654327 将它们分派到window procedures @。 time、pt 和 lPrivate 值不会传递给窗口过程,但是窗口过程可以分别通过 GetMessageTime() 和 GetMessagePos() 函数检索 time 和 pt 值( lPrivate 值不可访问)。

    TMessage 是 VCL 在由 RTL 的 MakeObjectInstance() 函数创建的窗口过程中使用的结构。此函数允许类(如 VCL 的 TWinControl 和 TTimer)使用非静态虚拟 WndProc() 方法作为 Win32 窗口过程。

    在 standard Win32 window procedure 中,只有 4 个参数可用 - hWnd、uMsg、wParam 和 lParam。基于 RTL 的窗口过程忽略 hWnd(因为它已经知道要调用哪个对象方法),将 uMsg、wParam 和 lParam 值复制到 TMessage 中,调用目标 WndProc()方法将TMessage 传递给它,然后将TMessage::Result 值返回给操作系统。

    我特别需要time 参数。有没有办法访问构造 TMessage 的原始消息(或任何其他获取时间参数的方法)?

    如果消息来自调用WndProc() 的线程的消息队列,则可以使用Win32 API GetMessageTime() 函数。或者,您可以使用 Win32 API SetWindowsHookEx() 函数将 WH_GETMESSAGE 挂钩安装到线程的消息队列中。

    如果您的组件的 WndProc() 专门在主 UI 线程中调用,您可以选择使用 VCL 的 TApplication::OnMessage 或 TApplicationEvents::OnMessage 事件,它们接收原始 MSG 结构的副本。您的组件可以使用私有 TApplicationEvents 对象来挂钩 OnMessage 事件。

    然而,一个窗口过程可以同时接收queued messages和non-queued messages,所以如果消息根本不是来自调用线程的消息队列,那么根本就没有time(或pt)值可用于检索它,因为非排队消息不会通过 MSG 结构开始。

    我在 TComponent::WndProc(Winapi::Messages::TMessage &Message) 中处理我的消息。

    TComponent 没有WndProc() 方法。也许您正在考虑TWinControl::WndProc()?

    【讨论】:

    • 它已将其添加到TComponent。我定义了virtual void __fastcall WndProc(Winapi::Messages::TMessage &Message);,然后用AllocateHWnd(WndProc);注册它来获取窗口句柄。它是一种魅力。我正在发布来自不同线程的消息,我只需要这些消息的时间戳。
    • @MaxKielland 只要您通过PostMessage()发布消息,而不是通过SendMessage()发送消息,然后@987654398 @ 会起作用的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多