【问题标题】:How does DispatchMessage decide which WndProc to use?DispatchMessage 如何决定使用哪个 WndProc?
【发布时间】:2011-10-11 10:30:21
【问题描述】:

我正在研究一个开源项目的一些代码,这是我第一次处理 Win32 应用程序(主要是做控制台的东西)。我看到代码中的几个函数是 Windows 过程,因为它们返回 LRESULT CALLBACK。

程序员是否需要做一些事情来确保 DispatchMessage 调用正确的 WndProc 或者 DispatchMessage 的编码方式可以自行确定正确的 WndProc?


编辑:

我猜 DispatchMessage(const MSG*) 在应用程序中选择不同 WndProc 的方式是基于传递给它的 MSG 对象的窗口句柄成员 (HWND)。[希望前面的句子不会太混乱.] 例如,如果消息是针对应用程序的编辑窗口(而不是针对其主窗口),那么 DispatchMessage 将为编辑窗口作为其对象的特定类选择 WndProc 成员函数。如果消息是针对主窗口的,那么它将为主窗口作为对象的类选择 WndProc 成员函数(对我来说,这个类将是 Notepad_plus_Window 类)。不过,这只是一个想法。我没有找到太多的支持,所以如果有人能证实这一点,我会很高兴。

【问题讨论】:

    标签: window wndproc procedures


    【解决方案1】:

    是的,你是对的,它使用 MSG.hwnd 将消息发送到正确的窗口。需要注意的一点是,它将使用与窗口关联的 WndProc,而不是窗口类。窗口是窗口类的一个实例。

    Microsoft 的“Under The Hood”文章中的This 用相当详细的 DispatchMessage() 的pseudo-code 说明了这一点,可能也值得一看)

    【讨论】:

    • 你说:it will use the WndProc associated with the window, not the window class。但是我们如何将 WndProc 与 window 关联起来:通过 window 类,即wc.lpfnWndProc = WndProc;
    • 是的,但您始终可以通过调用 SetWindowLongPtr 来修改特定窗口的 WndProc。在这种情况下,窗口的 WndProc 将不同于给定的窗口类。
    • SetWindowLongPtr 描述说:Calling SetWindowLongPtr with the GWLP_WNDPROC index creates a subclass of the window class used to create the window...The SetWindowLongPtr function creates the window subclass by changing the window procedure associated with a particular window class, causing the system to call the new window procedure instead of the previous one.这阐明了一个 WndProc 总是关联一个 Window Class。不是吗?
    • 您好@Vishal,您答案中的两个链接都已损坏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多