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,
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;
}