简体   繁体   English

在事件的主循环中获取X11窗口

[英]Get X11 Window inside the main loop for an event

I come from Windows, where, inside WndProc you can find out what window handler is related to a specific message. 我来自Windows,在WndProc您可以找到与特定消息相关的窗口处理程序。 I want to know if this is also possible with X11 我想知道X11是否也可以

while (!done) {
    XNextEvent(dis, &xev);

    if(xev.type == Expose) {
        // I want to know what window is being exposed here
    }

    if (xev.type == KeyPress) {
        // I want to know what window has received a key press here
    }
}

How could I achieve it? 我该如何实现? Really couldn't find anything so far 到目前为止真的找不到任何东西

Also, in Win32, you can store an object pointer for a class you create to represent your window, using SetWindowLong , which you can later get in the WndProc callback. 另外,在Win32中,可以使用SetWindowLong存储用于创建的代表窗口的类的对象指针,稍后可以在WndProc回调中获得该指针。 Is there a way to store an object pointer in the X11 case, so that it can be later retrieved in the same way, when processing the events? 有没有一种方法可以在X11情况下存储对象指针,以便以后在处理事件时可以以相同的方式检索它?

For those events that are related to X windows, their 'overloaded' event structure has a Window parameter. 对于与X窗口相关的那些事件,其“重载”事件结构具有Window参数。

XEvent is a union, a collection of message specific structures mapped into one structure. XEvent是一个并集,它是映射到一个结构的特定于消息的结构的集合。 So, to get to the proper event structure, you use this: 因此,要使用适当的事件结构,请使用以下代码:

   if (xev.type == KeyPress)
   {
      Window w = xev.xkey.window;
   }
   if (xev.type == Expose)
   {
      Window w = xev.xexpose.window;
   }

Et cetera. 等等。 Each event structure has only the parameters it needs. 每个事件结构只有其所需的参数。

I don't know about an object pointer for an X window; 我不知道X窗口的对象指针。 however, you could use a std::map to keep a list from Window ID that maps to a pointer, struct or class and keep track of it globally. 但是,您可以使用std :: map保留Window ID中的列表,该列表映射到指针,结构或类,并在全局范围内对其进行跟踪。

You don't need to retrieve the Window from each event type, you can use 您不需要从每种事件类型中检索窗口,可以使用

Window w = event.xany.window;

at the top of your event loop, before you even detect what kind of event it is. 在事件循环的顶部,甚至没有检测到它是什么类型的事件。 You can use 您可以使用

XContext ClassID = XUniqueContext();

as a global variable to use with the XSaveContext function. 作为与XSaveContext函数一起使用的全局变量。 Then you can use 那你可以用

XSaveContext( display, w, ClassID, (XPointer)myclass );

to store the Class pointer on the X Window itself. 将类指针存储在X窗口本身上。 So once you have the Window from the event, you can retrieve the Class from the Window using 因此,一旦有了事件中的窗口,就可以使用以下方法从窗口中检索类

XPointer return_class;
XFindContext( display, w, &return_class );
MyClass myclass = (MyClass *)return_class;

and so on... 等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM