Linux 与 Windows 中的窗口消息程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/525926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Window message procedures in Linux vs Windows
提问by Mizipzor
In Windows when you create a window, you must define a (c++)
在 Windows 中创建窗口时,必须定义一个 (c++)
LRESULT CALLBACK message_proc(HWND Handle, UINT Message, WPARAM WParam, LPARAM LParam);
to handle all the messages sent from the OS to the window, like keypresses and such.
处理从操作系统发送到窗口的所有消息,如按键等。
Im looking to do some reading on how the same system works in Linux. Maybe it is because I fall a bit short on the terminology but I fail to find anything on this through google (although Im sure there must be plenty!).
我想阅读一些关于同一系统在 Linux 中如何工作的书。也许是因为我在术语上有点欠缺,但我没有通过谷歌找到任何关于这方面的东西(尽管我确定一定有很多!)。
- Is it still just one single C function that handles all the communication?
- Does the function definition differ on different WMs (Gnome, KDE) or is it handled on a lower level in the OS?
- 它仍然只是一个处理所有通信的 C 函数吗?
- 函数定义在不同的 WM(Gnome、KDE)上是否有所不同,还是在操作系统的较低级别上处理?
Edit: Ive looked into tools like QT and WxWidgets, but those frameworks seems to be geared more towards developing GUI extensive applications. Im rather looking for a way to create a basic window (restrict resize, borders/decorations) for my OGL graphics and retrieve input on more than one platform. And according to my initial research, this kind of function is the only way to retrieve that input.
编辑:我研究过 QT 和 WxWidgets 之类的工具,但这些框架似乎更适合开发 GUI 广泛的应用程序。我宁愿寻找一种方法来为我的 OGL 图形创建一个基本窗口(限制调整大小、边框/装饰)并在多个平台上检索输入。根据我最初的研究,这种函数是检索该输入的唯一方法。
What would be the best route? Reading up, learning and then use QT or WxWidgets? Or learning how the systems work and implement those few basic features I want myself?
最好的路线是什么?阅读、学习然后使用 QT 或 WxWidgets?或者学习系统如何工作并实现我自己想要的那些基本功能?
采纳答案by MarkR
In principle it is absolutely the same. However, it has nothing to do with communication with the OS (nor does it on win32, using user32.dll is entirely optional)
原则上是完全一样的。但是,它与与操作系统的通信无关(在 win32 上也没有,使用 user32.dll 完全是可选的)
A GUI application has an event loop somewhere, which processes messages from a queue at some level.
GUI 应用程序在某处有一个事件循环,它在某个级别处理来自队列的消息。
There are a lot of libraries typically used to "hide" this behaviour - you can use them (and indeed, you should). If anything, the Xlib event system is even more perverse than Win32's user32.dll one, and is less widely understood, therefore fewer people use it directly.
有很多库通常用于“隐藏”这种行为——您可以使用它们(实际上,您应该使用它们)。如果有的话,Xlib 事件系统甚至比 Win32 的 user32.dll 更反常,并且没有被广泛理解,因此很少有人直接使用它。
In Linux or in Windows, applications can use the low-level GUI, or can use a library. Most use a library. Applications can also choose to do neither and operate without a GUI (server applications typically do this). Applications can create multiple threads, one of which sits in an event loop, and others work differently. This is a popular approach too.
在 Linux 或 Windows 中,应用程序可以使用低级 GUI,也可以使用库。大多数使用图书馆。应用程序也可以选择两者都不做,也可以在没有 GUI 的情况下运行(服务器应用程序通常会这样做)。应用程序可以创建多个线程,其中一个位于事件循环中,而其他线程的工作方式不同。这也是一种流行的方法。
- Most GUI applications use a higher level library for their GUI
- Non-interactive applications, e.g. server applications, don't use the GUI at all and don't use the libraries (e.g. XLib, user32.dll)
- Applications which don't lend themselves to an "Event loop" (e.g. Games) typically use a separate thread to process their event loop.
- These things are largely true on Win32 and Linux.
- 大多数 GUI 应用程序为其 GUI 使用更高级别的库
- 非交互式应用程序,例如服务器应用程序,根本不使用 GUI,也不使用库(例如 XLib、user32.dll)
- 不适合“事件循环”的应用程序(例如游戏)通常使用单独的线程来处理它们的事件循环。
- 这些事情在 Win32 和 Linux 上基本上是正确的。
回答by Ismael
Well at the very basic level you have the X Window protocol http://en.wikipedia.org/wiki/X_Window_System_core_protocol, which we can be pretty complex to handle if you want to do any application. Next on the stack there's Xlib http://en.wikipedia.org/wiki/Xlibwhich is a "convenient" wrapper around the X protocol, but still is complex for "real life" applications. It's on top of Xlib that most other frameworks are built, trying to simplify application development. The most know are: Xt, Gtk, Qt, etc.
那么在非常基本的层面上,您拥有 X Window 协议http://en.wikipedia.org/wiki/X_Window_System_core_protocol,如果您想做任何应用程序,我们可能会非常复杂地处理它。堆栈中的下一个是 Xlib http://en.wikipedia.org/wiki/Xlib,它是围绕 X 协议的“方便”包装器,但对于“现实生活”应用程序来说仍然很复杂。大多数其他框架都是在 Xlib 之上构建的,试图简化应用程序开发。最了解的有:Xt、Gtk、Qt等。
Like in window you have a "event loop", and if you want you can implement on top of it a GetMessage/DispachMessage metaphor to mimic the windows behavior. That way you may have a WNDPROC, but natively X doesn't provide such thing.
就像在窗口中一样,您有一个“事件循环”,如果您愿意,可以在它之上实现一个 GetMessage/DispachMessage 比喻来模仿 Windows 行为。这样你可能有一个 WNDPROC,但 X 本身不提供这样的东西。
Before reinventing the wheel is preferable to take a look at similar applications, what they are using.
在重新发明轮子之前最好先看看类似的应用程序,它们正在使用什么。
If you need something simple you can try SDL http://www.libsdl.org/, which is a cross platform library to aimed to develop games/simple applications. Another alternative is Allegro game library http://www.talula.demon.co.uk/allegro/.
如果您需要一些简单的东西,您可以尝试 SDL http://www.libsdl.org/,这是一个旨在开发游戏/简单应用程序的跨平台库。另一种选择是 Allegro 游戏库http://www.talula.demon.co.uk/allegro/。
回答by Mark Ingram
It's totally and utterly different. That window procedure is 100% specific to the Windows OS. For linux, it will depend on the window manager (gnome, kde - as you've already mentioned). If you wish to do cross-platform development, you might want to look at things like QT.
这是完全不同的。该窗口过程 100% 特定于 Windows 操作系统。对于 linux,它将取决于窗口管理器(gnome、kde - 正如您已经提到的)。如果你想做跨平台开发,你可能想看看 QT 之类的东西。
You may wish to take a look at the following URLs:
您可能希望查看以下 URL:
http://www.qtsoftware.com/products/appdev
http://en.wikipedia.org/wiki/Qt_toolkit
http://www.qtsoftware.com/products/appdev
http://en.wikipedia.org/wiki/Qt_toolkit
回答by MarkR
As stated by xhantt, what transport the equivalent messages you are looking for is the X Window System. Which, indeed, can be a bit complex.
正如 xhantt 所说,传输您正在寻找的等效消息的是 X 窗口系统。这确实可能有点复杂。
With XLib you will need to handle the events registering and dequeuing in your main loop. See the XLib manualfor a complete description on how to proceed. But don't forget that you will only catch window and inputs events this way. Not every OS messages.
使用 XLib,您将需要处理主循环中的事件注册和出列。有关如何进行的完整说明,请参阅XLib 手册。但是不要忘记,您只会以这种方式捕获窗口和输入事件。不是每个操作系统消息。
You can also look for XCBwhich is a newer, and probably easier, library.
您还可以查找XCB,它是一个更新的、可能更简单的库。
If you build your application on top of those two library, it will run smoothly under (almost, we can never be too sure) every WM. And you won't require any dependency that most linux user don't already have on their installation. If you go with Qt, GTK, etc... It will be easier and work under any WM, but they may not have library installed.
如果您在这两个库之上构建您的应用程序,它将在(几乎,我们永远不会太确定)每个 WM 下顺利运行。而且您不需要任何大多数 linux 用户在他们的安装中还没有的依赖项。如果你使用 Qt、GTK 等......在任何 WM 下工作都会更容易,但它们可能没有安装库。