WNDCLASSEX的概念、良好的编程习惯和系统类的WndProc

发布于 2024-07-14 17:03:31 字数 1044 浏览 9 评论 0原文

我知道 Windows API 使用“类”,依赖于 WNDCLASS/WNDCLASSEX 结构。

我已经成功浏览了windows API Hello World应用程序,了解到这个类是我们自己的windows使用的,也是Windows核心控件使用的,比如“EDIT”、“BUTTON”等。 我明白它在某种程度上与WndProc相关(它允许我为它定义一个函数)

虽然我可以找到有关此类的文档,但我找不到任何解释概念< /强>。

到目前为止,我唯一发现的是:

窗口类与以下内容无关 C++ 类。

确实没有帮助(它告诉我它不是什么,但不告诉我它是什么)。 事实上,这只会让我更加困惑,因为我很想将 WNDCLASSEX 与 C++ 类关联起来,并认为“WNDCLASSEX”代表一个控件类型 。 所以,我的第一个问题是这是什么?

其次,我知道可以在类中定义 WndProc。 但是,窗口也可以从子控件(或窗口,或 Windows API 中调用的任何名称)获取消息。 怎么会这样?

最后,什么时候定义新类是一个好的编程习惯? 每个应用程序(对于主框架),每个框架,每个我定义的控件(例如,如果我创建自己的进度栏类)?

我了解 Java/Swing、C#/Windows.Form、C/GTK+ 和 C++/wxWidgets,因此我可能会了解与这些工具包的比较。

I understand that the Windows API uses "classes", relying to the WNDCLASS/WNDCLASSEX structures.

I have successfully gone through windows API Hello World applications and understand that this class is used by our own windows, but also by Windows core controls, such as "EDIT", "BUTTON", etc.
I also understand that it is somehow related to WndProc(it allows me to define a function for it)

Although I can find documentation about this class, I can't find anything explaining the concept.

So far, the only thing I found about it was this:

A Window Class has NOTHING to do with
C++ classes.

Which really doesn't help(it tells me what it isn't but doesn't tellme what it is). In fact, this only confuses me more, since I'd be tempted to associate WNDCLASSEX to C++ classes and think that "WNDCLASSEX" represents a control type
.
So, my first question is What is it?

In second place, I understand that one can define a WndProc in a class. However, a window can also get messages from the child controls(or windows, or whatever they are called in the Windows API). How can this be?

Finally, when is it a good programming practise to define a new class? Per application(for the main frame), per frame, one per control I define(if I create my own progress bar class, for example)?

I know Java/Swing, C#/Windows.Form, C/GTK+ and C++/wxWidgets, so I'll probably understand comparisons with these toolkits.

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

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

发布评论

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

评论(2

じее 2024-07-21 17:03:31

窗口类描述了将用于该类的所有实例的属性。 除了颜色、图标等之外,窗口过程也是这些属性之一。 这是回调函数,负责处理来自系统的所有消息并根据需要进行处理。

虽然不相关,但该概念类似于 C++ 类 - 一段代码定义了类的数据和功能,并且该类可能有许多实例。

作为一个非常粗略的示例,“BUTTON”类 WndProc 将 WM_LBUTTONDOWN/WM_LBUTTONUP 序列转换为“单击”。 在此序列期间,WndProc 还将绘制处于“按下”状态的按钮,作为对 WM_PAINT 消息的响应。

当窗口需要向父窗口传达操作(例如单击)时,它会以“通知”或“命令”的形式发送消息。 这些消息由子控件手动创建,并包含标识符和任何相关数据。

所有这一切的好处是,只需创建按钮类的窗口(使用 CreateWindow),我就可以自动获得所有这些行为。

除了 Windows 提供的窗口之外,我还可以创建自己的窗口,可以在我的应用程序中重新使用它们。 您必须为主窗口注册至少一个类(否则它将没有任何功能,因此不会很有趣),但除此之外,这取决于您。

很有可能应用程序只包含常规控件(按钮、列表视图、进度条),但有时您可能希望创建自定义类。 这可能是为了封装特定的行为,或者因为您希望通过程序使用该控件的许多实例。

例如,如果我的应用程序的 UI 需要当鼠标悬停在长颈鹿上方时旋转的长颈鹿图像,我可以在 WindowProcedure 中编写代码来执行此操作,并将其注册为新类(“SPINNYGIRAFFE”)。 我的应用程序的其余部分只是创建“SPINNYGIRAFFE”类的窗口,一切正常。

A window class describes the properties that will be used for all instances of that class. As well as colors, icons and so on, one of these properties is the Window Procedure. This is the callback function that is responsible for handling all messages from the system and processing them as necessary.

While unrelated the concept is analogous to C++ classes - One piece of code defines the data and functionality of a class and there may be many instances of that class.

As a very rough example the "BUTTON" class WndProc translates a WM_LBUTTONDOWN/WM_LBUTTONUP sequence as a "click". During this sequence the WndProc will also paint the button in a "pressed" state as a response to the WM_PAINT message.

Where a window needs to communicate an action to a parent (e.g. a click) it sends messages as "notifications" or "commands". These messages are manually created by the child control and contain an identifier and any pertinent data.

The benefit of all this just by creating window (using CreateWindow) of the button class I get all this behavior automatically.

As well as the ones provided by windows I can also create my own windows that can be resued throughout my app. You must register at least one class for your main window (otherwise it would have no functionality so wouldn't be very interesting) but beyond that it is up to you.

It's quite possible to have applications that contain only the regular controls (buttons, listviews, progress bars) but there are times where you may want to create a custom class. This could either be to encapsulate specific behavior, or because you wish to use many instances of that control through your program.

For example if the UI of my application should need images of giraffes that spin when the mouse is over them I can write the code to do this in a WindowProcedure and register that as a new class ("SPINNYGIRAFFE"). The rest of my application just creates windows of the "SPINNYGIRAFFE" class and everything just works.

青朷 2024-07-21 17:03:31

怎么可能?

子控件(这些也是窗口)向其父窗口发送消息,Windows(注意大写字母,这是操作系统)知道该窗口的当前 WndProc 并调用它。

什么时候定义新类是一个好的编程习惯?

每个窗口都必须有一个类,因此除非您的窗口存在现有类,否则您必须定义一个新类。 您可能需要为您自己的所有顶级窗口定义类,但不需要为任何控件(这些控件的内置类)或对话框定义类,因为 Windows 中的对话框 API 使用另一个内置类。

How can this be?

The child controls(these are also windows) send a message to their parent window, Windows (note the capitilization, this is the OS) knows the current WndProc of that window and calls it.

When is it a good programming practise to define a new class?

Every window must have a class, thus unless an existing class exists for your window, you must define a new class. You likely will need to define classes for all your own top level windows, but not for any controls (built in classes for these) or for dialogs since the dialog box APIs in Windows use another buildin class.

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