3

Do you know how can I create a maximized Window in GLUT ? (maximized, not full-screen)

I have searched in Google for the solution but I couldn't find it, so I started trying to do it with the Windows API, even tough I later will need to solve it for Linux as well :(

This is what I tried:

wchar_t* wString = new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, "GLUT", -1, wString, 4096);
HWND vWnd = FindWindow(wString, NULL);
long currentStyle = GetWindowLong(vWnd, GWL_STYLE);
SetWindowLong(vWnd, GWL_STYLE, currentStyle | WS_MAXIMIZE);

But it is not changing the Window's state. I also tried this:

ShowWindow(vWnd, WS_MAXIMIZE);

Because the documentation (here) states that this function: "Sets the specified window's show state". But I guess it is only for Windows that are not yet visible, and mine was created and shown using GLUT (glutCreateWindow).

Currently there is an answer stating that this cannot be done, but I'd like a confirmation on that from a credible source.

Thanks,

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • Check out `ShowWindowAsynch` for Windows. For Linux you will need luck. The X11 standard is written to discourage applications from fiddling with host windows. There is a supplementary attribute `_NET_WM_STATE_MAXIMIZED_VERT` and also `_HORZ` that window managers can choose to honor or not. See http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html. See code at http://graphin.googlecode.com/svn/trunk/platform/linux/xwnd.cpp to observe a window manager setting the atrributes. GLUT also supports a full screen mode on some systems. You could look at that. – Gene Jan 03 '15 at 05:27

2 Answers2

2

I spent a few hours testing and experimenting different solutions, but at the end what worked was:

/* Maximize window using Windows API after glutCreateWindow() has been called */

HWND win_handle = FindWindow(0, L"Stackoverflow");
if (!win_handle)
{
    printf("!!! Failed FindWindow\n");
    return -1;
}

SetWindowLong(win_handle, GWL_STYLE, (GetWindowLong(win_handle, GWL_STYLE) | WS_MAXIMIZE));
ShowWindowAsync(win_handle, SW_SHOWMAXIMIZED);

/* Activate GLUT main loop */

glutMainLoop();

It's pretty simple, and I just want to empathize that you can only call glutMainLoop() at the end.

Now, since you are looking for a cross-platform solution I must say that this approach is a terrible idea. Let me share a few other approaches:

  • Consider using Qt (a cross-platform C++ framework to build applications with GUI). Here's an example;

  • Consider writing individual platform code to retrieve the current monitor resolution. When glutInitWindowSize() is called, you can pass the right values instead of trying to hack the window to maximize it;

Here's the source code to a minimal, complete and verifiable example (for Windows):

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/glut.h>

int WIDTH = 480;
int HEIGHT = 480;

void initializeGL()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    GLfloat aspect = (GLfloat) WIDTH / HEIGHT;
    gluPerspective(45, aspect, 1.0f, 500.0f);
    glViewport(0, 0, WIDTH, HEIGHT);

    glMatrixMode(GL_MODELVIEW);
    glShadeModel( GL_SMOOTH );
    glClearDepth( 1.0f );
    glEnable( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL );
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    glClearColor(0.0, 0.0, 0.0, 1.0);
}

void displayGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-3.0f);

    glBegin(GL_TRIANGLES);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-1.0f,-1.0f, 0.0f);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f);
    glEnd();

  glutSwapBuffers();
}

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowSize(640, 480);
  glutCreateWindow("Stackoverflow");

  glutDisplayFunc(displayGL);
  initializeGL();

  /* Maximize window using Windows API */

  HWND win_handle = FindWindow(0, L"Stackoverflow");
  if (!win_handle)
  {
      printf("!!! Failed FindWindow\n");
      return -1;
  }

  SetWindowLong(win_handle, GWL_STYLE, (GetWindowLong(win_handle, GWL_STYLE) | WS_MAXIMIZE));
  ShowWindowAsync(win_handle, SW_SHOWMAXIMIZED);

  /* Activate GLUT main loop */

  glutMainLoop();

  return 0;
}
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • Thanks. I will check that in a moment. As for your recommendations, 1) It is a program for a course where GLUT needs to be used, but if not, Qt sounds like the way to go; and 2) I'm currently doing it like that, setting the window size to 90% of the screen, but if I tried to use the 100% to make it look maximized I'd have the problem of the size of the taskbar (or the dock) – Daniel Jan 03 '15 at 20:55
  • Thanks! I verified it and indeed your code succeeds at maximizing the window. I also tried removing the line containing `SetWindowLong` and even without it the code is able to maximize the window, so only `ShowWindowAsync` seems to be needed (is there some scenario I'm missing here where both are needed?) – Daniel Jan 03 '15 at 21:07
  • In this case, `SetWindowLong()` is not needed because the window was already created by GLUT with permission to be maximized by the user. On other situations, one might have to enable this feature before trying to maximize the window. I'll leave this tip here for completeness sake. – karlphillip Jan 03 '15 at 21:08
  • This example doesn't work for me. I compile it with VS 2019 on Win 11 with CMake and FreeGLUT 3.2.2. It shows the window at 640x480 with a title bar, as always. However, the maximize window icon in the title bar appears as though it were maximized. This appears to be the only effect this code is having. – All The Rage Mar 18 '22 at 20:23
  • @AlltheRage Did you notice how old this thread is? – karlphillip Mar 19 '22 at 05:04
  • @karlphillip, yes. Why? Do you think it's not valid for me to inform the world that this solution doesn't work on my platform? Do you think I'm blaming you for your solution not working for me? I'm not, but a little help would be nice. What do you think I should have done instead? Ask the same question again? People tend to delete duplicate questions or flippantly point the questioner back to the prior answer. I think commenting is the right thing to do here, but if you have a problem with it, I'd love to hear something helpful from you. – All The Rage Mar 19 '22 at 23:49
  • Feel free to ask a new question on the site pointing out that none of the previous questions on this subject (and then share the links to each of them, including this one) has a valid answer that is up-to-date with current APIs. People can't close it as a duplicate if you let them know in advanced that previous threads are not helpful anymore. – karlphillip Mar 19 '22 at 23:57
1

As far as I know, there is no platform independent way to maximize windows from glut. One possible work around would be to forget maximizing the window and instead set its size to that of the display. That is, use glutGet(GLUT_SCREEN_WIDTH) and glutGet(GLUT_SCREEN_HEIGHT) to get the size of the display and then use those values in glutInitWindowSize.

idfah
  • 1,328
  • 10
  • 16
  • Thanks for the response. Currently I'm doing it like that but then there is the problem of the OS taskbar occluding part of my window. I have temporarily set my window's size to 90% of the screen to avoid the occlusion, but that is a number taken out of the blue. – Daniel Dec 17 '14 at 20:50