#include <gl/opengl.h>
HGLRC hRC = NULL;
HDC hDC = NULL;
HINSTANCE hInstance = NULL;
HWND hWnd = NULL;
BOOL keys[255];
BOOL active = TRUE;
BOOL fullscreen = TRUE;
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
GLvoid ResizeGLScene(GLsizei width, GLsizei height){
if (height == 0)
height = 1;
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid){
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE;
}
BOOL DrawwGLScene(GLvoid){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glEnd();
glTranslatef(3.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
return TRUE;
}
GLvoid KillGLWindow(GLvoid){
if (fullscreen){
ChangeDisplaySettings(NULL, NULL);
ShowCursor(TRUE);
}
if (hRC){
if (!wglMakeCurrent(NULL, NULL))
MessageBox(NULL, "释放DC 或 RC 失败", "关闭错误", MB_OK|MB_ICONEXCLAMATION);
if (!wglDeleteContext(hRC))
MessageBox(NULL, "释放RC 失败", "关闭错误", MB_OK|MB_ICONEXCLAMATION);
hRC = NULL;
}
if (hDC && !ReleaseDC(hWnd, hDC)){
MessageBox(NULL,"释放DC失败","关闭错误", MB_OK|MB_ICONEXCLAMATION);
hDC = NULL;
}
if (hWnd && !DestroyWindow(hWnd)){
MessageBox(NULL, "释放窗口失败", "关闭错误", MB_OK|MB_ICONEXCLAMATION);
hWnd = NULL;
}
if (!UnregisterClass("opengl", hInstance)){
MessageBox(NULL, "反注册窗口类失败","关闭错误", MB_OK|MB_ICONEXCLAMATION);
hInstance = NULL;
}
}
BOOL CreateGLWindow(char *title, int width, int height, int bits, BOOL fullscreenflag){
GLuint PixelFormat;
WNDCLASS wc;
DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)width;
WindowRect.top = (long)0;
WindowRect.bottom = (long)height;
fullscreen = fullscreenflag;
hInstance = GetModuleHandle(NULL);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszClassName = "opengl";
wc.lpszMenuName = NULL;
if (!RegisterClass(&wc)){
MessageBox(NULL, "窗口注册失败", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (fullscreen){
DEVMODE dmScreenSetting;
memset(&dmScreenSetting, 0, sizeof(dmScreenSetting));
dmScreenSetting.dmSize = sizeof(dmScreenSetting);
dmScreenSetting.dmPelsHeight = height;
dmScreenSetting.dmPelsWidth = width;
dmScreenSetting.dmBitsPerPel = bits;
dmScreenSetting.dmFields = DM_BITSPERPEL|DM_PELSHEIGHT|DM_PELSWIDTH;
if (ChangeDisplaySettings(&dmScreenSetting, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL){
if (MessageBox(NULL, "全屏模式在当前显卡设置失败.\n使用小窗口模式?", "错误", MB_YESNO|MB_ICONEXCLAMATION) == IDYES){
fullscreen = FALSE;
}
else{
MessageBox(NULL, "程序将被关闭","错误",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
}
}
if (fullscreen){
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_POPUP;
ShowCursor(FALSE);
}
else{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;
}
AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
if(!(hWnd = CreateWindowEx(dwExStyle,
"opengl",
title,
dwStyle|
WS_CLIPSIBLINGS|
WS_CLIPCHILDREN,
0,0,
WindowRect.right-WindowRect.left,
WindowRect.bottom-WindowRect.top,
NULL,
NULL,
hInstance,
NULL))){
KillGLWindow();
MessageBox(NULL, "创建窗口错误", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
static PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DOUBLEBUFFER|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
bits,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
if (!(hDC = GetDC(hWnd))){
KillGLWindow();
MessageBox(NULL, "不能创建一个相匹配的像素格式","错误",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))){
KillGLWindow();
MessageBox(NULL, "选择像素格式错误", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!SetPixelFormat(hDC, PixelFormat, &pfd)){
KillGLWindow();
MessageBox(NULL, "设置像素格式错误", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!(hRC = wglCreateContext(hDC))){
KillGLWindow();
MessageBox(NULL,"创建hRc误误", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
if (!wglMakeCurrent(hDC, hRC)){
KillGLWindow();
MessageBox(NULL, "不能激活当前的opengl设备描述表", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
ShowWindow(hWnd, SW_SHOW);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
ResizeGLScene(width, height);
if (!InitGL()){
KillGLWindow();
MessageBox(NULL, "opengl 初始化失败", "错误", MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
switch(uMsg){
case WM_ACTIVATE:
{
if (!HIWORD(wParam))
active = TRUE;
else
active = FALSE;
return 0;
}
case WM_SYSCOMMAND:
{
switch(wParam){
case SC_SCREENSAVE:
case SC_MONITORPOWER:
return 0;
}
}
break;
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
case WM_KEYUP:
{
keys[wParam] = TRUE;
return 0;
}
case WM_KEYDOWN:
{
keys[wParam] = FALSE;
return 0;
}
case WM_SIZE:
{
ResizeGLScene(LOWORD(lParam), LOWORD(lParam));
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
MSG msg;
BOOL done = FALSE;
if (MessageBox(NULL, "你想在全屏模式下运行吗?", "设置全屏运行模式", MB_YESNO|MB_ICONEXCLAMATION) == IDYES)
fullscreen = TRUE;
else
fullscreen = FALSE;
if (!CreateGLWindow("Lesson 2", 640, 480, 16, fullscreen)){
MessageBox(NULL,"窗口创建失败", "错误", MB_OK|MB_ICONEXCLAMATION);
return 0;
}
while (!done){
if (PeekMessage(&msg, NULL, 0,0,PM_REMOVE)){
if (msg.message == WM_QUIT)
done = TRUE;
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else{
if (active){
if (keys[VK_ESCAPE]){
done = TRUE;
}
else{
DrawwGLScene();
SwapBuffers(hDC);
}
if (keys[VK_F1]){
keys[VK_F1] = FALSE;
KillGLWindow();
fullscreen = !fullscreen;
if (!CreateGLWindow("Lesson 2", 640, 480, 16,fullscreen))
return 0;
}
}
}
}
KillGLWindow();
return (msg.wParam);
}
![这里写图片描述](https://img-blog.csdn.net/20161025174330858)