c++ - WinAPI SetWindowLongPtr - 改变 windowProc

标签 c++ windows winapi

关于 MSDN网站,我发现以下内容:

Calling SetWindowLongPtr with the GWLP_WNDPROC index creates a subclass of the window class used to create the window. An application can subclass a system class, but should not subclass a window class created by another process. 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. An application must pass any messages not processed by the new window procedure to the previous window procedure by calling CallWindowProc. This allows the application to create a chain of window procedures.

这是否意味着,每次我用 GWLP_WNDPROC 调用 SetWindowLongPtr 时,都会创建新的子类,或者 Windows 是否足够聪明,如果相同的过程只创建一次子类作为参数传递多次?

最佳答案

术语“子类”在 winapi 中是概念性的。它最初是在 1983 年设计的,目的是针对 C 语言。当然,它不支持类(class)。

1983 年是软件开发的重要一年,面向对象的编程开始腾飞。那时 Bjarne Stroustrup 将他的“C with Classes”重命名为 C++。当 Xerox 向任何想要拷贝的人发布他们的 Smalltalk-80 实现时。鉴于 Smalltalk 专注于消息传递,它可能对 Windows 设计人员产生了强烈的启发。

因此,虽然 winapi 不能完全面向对象,但它确实吸取了这种技术的味道。 RegisterClass() 函数是最明显的,它就像一个基类,为从该类创建的任何窗口“对象”提供相同的行为。用 WNDCLASS.lpfnWndProc 指定的窗口过程就像一个可以被覆盖的虚拟方法。

因此,使用 RegisterClass() 注册的类的“子类”仅仅覆盖了 lpfnWndProc 函数。你用 SetWindowLongPtr() 来做到这一点,你必须自己带来新的功能。您必须像在 C++ 程序中重写虚函数时通常所做的那样调用“基本”函数。

这不是唯一的方法。 SubWindowSubclass() function是一个帮助函数来让它正确。 DefSubclassProc() function帮助您调用“基类”。推荐。

关于c++ - WinAPI SetWindowLongPtr - 改变 windowProc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37433559/

相关文章:

windows - EmacsW32 : possible to use both "windows key" and "alt key" bindings together?

c++ - Win32 : How to create a bordless popup window

c++ - 当变量是右值引用时自动从局部变量移动

C++ 模板:返回列表::迭代器

windows - Powershell:如何在一行中输出文件夹名称、上次写入时间和文件夹大小?

c++ - 创建虚拟设备

c++ - 模拟管理员用户时使用 AddIPAddress 时出现问题

c - 为Windows实现C11线程

c++ - 链接 clang/llvm 3.0 库

c++ - 使用排序算法 C++