(八)OpenGL获取电脑屏幕大小和窗口大小来设置窗口居中

获取电脑屏幕大小和窗口大小

	//获取窗口大小,宽度和高度
	wwidth = glutGet(GLUT_WINDOW_WIDTH);
    wheight = glutGet(GLUT_WINDOW_HEIGHT);
    //获取电脑屏幕大小,宽度和高度
    swidth = glutGet(GLUT_SCREEN_WIDTH);
    sheight = glutGet(GLUT_SCREEN_HEIGHT);

这样就能设置窗口相对于屏幕的大小了,比如居中,到左上角,右下角之类的。
示例代码如下。

#include 
#include 
#include 
#include 

void draw(void){
    glClearColor(0,0,1,0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glOrtho(-1,1,-1,1,-1,1);
    glBegin(GL_POLYGON);
        glVertex2f(-0.5,-0.5);
        glVertex2f(-0.5,0.5);
        glVertex2f(0.5,0.5);
        glVertex2f(0.5,-0.5);
    glEnd();
    glFlush();
}

void menufunc(int data){
    int wwidth,wheight;
    int swidth,sheight;
    int x,y;
    wwidth = glutGet(GLUT_WINDOW_WIDTH);
    wheight = glutGet(GLUT_WINDOW_HEIGHT);
    swidth = glutGet(GLUT_SCREEN_WIDTH);
    sheight = glutGet(GLUT_SCREEN_HEIGHT);
    switch(data)
    {
        case 1:
            x = (swidth-wwidth)/2;
            y = (sheight-wheight)/2;
            glutPositionWindow(x,y);
            glutReshapeWindow(wwidth,wheight);
            break;
        case 2:
            glutPositionWindow(0,0);
            glutReshapeWindow(wwidth,wheight);
            break;
        case 3:
            x = swidth-wwidth;
            y = 0;
            glutPositionWindow(x,y);
            glutReshapeWindow(wwidth,wheight);
            break;
        case 4:
            x = 0;
            y = sheight-wheight;
            glutPositionWindow(x,y);
            glutReshapeWindow(wwidth,wheight);
            break;
        case 5:
            x = swidth-wwidth;
            y = sheight-wheight;
            glutPositionWindow(x,y);
            glutReshapeWindow(wwidth,wheight);
            break;
        case 6:
            glutFullScreen();
            break;
        case 7:
            glutReshapeWindow(500,500);
            break;
        case 8:
            glutReshapeWindow(400,400);
            break;
        default:
            break;
    }
}

int main(int argc,char *argv[]){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_SINGLE);
    glutInitWindowPosition(0,0);
    glutInitWindowSize(512,512);
    glutCreateWindow("");
    glutDisplayFunc(draw);

    glutCreateMenu(menufunc);
    glutAddMenuEntry("ju zhong",1);
    glutAddMenuEntry("zuo shang",2);
    glutAddMenuEntry("you shang",3);
    glutAddMenuEntry("zuo xia",4);
    glutAddMenuEntry("you xia",5);
    glutAddMenuEntry("quan ping",6);
    glutAddMenuEntry("500",7);
    glutAddMenuEntry("400",8);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glutMainLoop();
    return 0;


}


运行结果:
居中模式:
(八)OpenGL获取电脑屏幕大小和窗口大小来设置窗口居中_第1张图片
右下角模式:
(八)OpenGL获取电脑屏幕大小和窗口大小来设置窗口居中_第2张图片

你可能感兴趣的:((八)OpenGL获取电脑屏幕大小和窗口大小来设置窗口居中)