简体   繁体   English

糊涂:如何知道窗口何时移动?

[英]Glut: how to know when the window is moving?

I have a problem with glut, I would like to have a callback to know when the user is moving the window of my application, and I didn't found anything in glut. 我有一个过剩的问题,我想有一个回调来知道用户何时移动我的应用程序的窗口,而我在过剩的东西中没有发现任何东西。

My goal is to make an application only on windows, is it possible to do this with MFC keeping my glut code? 我的目标是仅在Windows上创建应用程序,使用MFC保留我的glut代码是否可以做到这一点?

Thanks in advance 提前致谢

I couldn't find a call back function but you could do it by hand with glutGet : 我找不到回调函数,但是您可以使用glutGet手动glutGet

int glutGet(GLenum state);

An example of what you might do is: 您可能会做的一个例子是:

bool window_change(void)
{
  int init_x = glutGet(GLUT_INIT_WINDOW_X);
  int init_y = glutGet(GLUT_INIT_WINDOW_Y);
  int pos_x = glutGet(GLUT_WINDOW_X);
  int pos_y = glutGet(GLUT_WINDOW_Y);

  return (init_x != pos_x || init_y != pos_y);
}

This will return true if it's moved from it's initial spot. 如果将其从初始位置移开,则将返回true。 If you want to see if it's moved since the last check, try: 如果要查看自上次检查后是否已移动,请尝试:

bool window_change(void)
{
  static int init_x = glutGet(GLUT_INIT_WINDOW_X);
  static int init_y = glutGet(GLUT_INIT_WINDOW_Y);
  int pos_x = glutGet(GLUT_WINDOW_X);
  int pos_y = glutGet(GLUT_WINDOW_Y);

  bool result = init_x != pos_x || init_y != pos_y;

  init_x = pos_x; 
  init_y = pos_y;

  return result;
}

You can set the window position by using the function: glutPositionWindow 您可以使用以下功能来设置窗口位置: glutPositionWindow

void glutPositionWindow(int x, int y);

the idea from Bruce is very good. 布鲁斯的想法非常好。 i think there is not another option while using GLUT. 我认为使用GLUT时没有其他选择。 i think this scenario is something that GLUT was not developed for. 我认为这种情况不是为GLUT开发的。 GLUT is a toolkit for OpenGL, which wraps window- and input-management across platforms. GLUT是OpenGL的工具包,它跨平台包装了窗口和输入管理。 it has a lot of uses, but why should it care, when its window is dragged? 它有很多用途,但是当拖动窗口时为什么要关心它呢? i think if you (or your program) care, then you should implement your own window- and input-management anyway. 我认为,如果您(或您的程序)在乎,那么无论如何您应该实现自己的窗口和输入管理。

which leads me to your second question. 这使我想到了第二个问题。 you can use OpenGL with MFC (although my recommendation strongly depends on what you are planing). 您可以将OpenGL与MFC一起使用(尽管我的建议强烈取决于您的计划)。 you should not use GLUT then, because MFC has its own way of handling input, windows, event management, rendering/drawing ... 那么,您不应该使用GLUT,因为MFC有其自身的处理输入,窗口,事件管理,渲染/绘图的方式...

if you really want to do it: http://www.codeproject.com/KB/openGL/OpenGL_MFC_AppWizard.aspx 如果您确实要这样做: http : //www.codeproject.com/KB/openGL/OpenGL_MFC_AppWizard.aspx

You might try using the glutTimerFunction to periodically call Bruce's example function. 您可以尝试使用glutTimerFunction定期调用Bruce的示例函数。 It will have to take an integer as a parameter used to identify the source of the callback request. 它必须使用整数作为用于标识回调请求源的参数。 The glutTimerFunction is also designed to only be called once, then it deletes itself. glutTimerFunction还设计为仅被调用一次,然后将其删除。 However, it can schedule a call on itself. 但是,它可以安排自己的呼叫。 I ran into this problem using PyOpenGL so I'm not going to go through the C agony of getting this to compile. 我使用PyOpenGL遇到了这个问题,所以我不会经历将其编译的C痛苦。 From Bruce's generous example altered. 从布鲁斯的慷慨例子改写而来。 Obviously a hack, but better than scrapping. 显然是hack,但比报废更好。


int main(void)
{
   ...
   glutTimerFunc(0, window_change, 0);

}

void window_change(int id) { int init_x = glutGet(GLUT_INIT_WINDOW_X); int init_y = glutGet(GLUT_INIT_WINDOW_Y); int pos_x = glutGet(GLUT_WINDOW_X); int pos_y = glutGet(GLUT_WINDOW_Y); ...

/* call back again after 1 second has passed */ glutTimerFunc (1000, window_change, id); }

http://www.opengl.org/resources/faq/technical/glut.htm http://www.opengl.org/resources/faq/technical/glut.htm

GLUT is actually fairly limited, and does not expose all window events to the client. GLUT实际上是相当有限的,并且不会向客户端公开所有窗口事件。 However it does provide a lot of boiler code plate for WGL (on Windows) and GLX (on X11) that is probably not worth rewriting. 但是,它确实为WGL(在Windows上)和GLX(在X11上)提供了许多锅炉代码板,可能不值得重写。

When I had a hobby project that was using GL, and there were certain events that I couldn't handle via GLUT, what I did is fork the open source freeglut . 当我有一个使用GL的业余项目时,并且某些事件我无法通过GLUT处理,我所做的就是派生开源freeglut That is, download the tarball, and edit the library's WindowProc (Windows) or XGetEvent() loop (on X) and simply add the missing callbacks to the library. 也就是说,下载压缩包,并编辑库的WindowProc(Windows)或XGetEvent()循环(在X上),然后只需将缺少的回调添加到库中即可。

You'll have a dependency on your own GLUT modifications, but it'll work, and you won't have to write as much boiler-plate code as if you were targeting WGL or GLX directly. 您将依赖于自己的GLUT修改,但是它将起作用,并且不必像直接针对WGL或GLX那样编写太多的样板代码。

You'll probably want to link it statically so it doesn't conflict with any other freeglut. 您可能需要静态链接,这样它就不会与任何其他freeglut冲突。

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

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