本文将介绍一种不依赖任何第三方库,搭建自己Win32应用程序窗口的方法,程序总共长度为77行,源代码下载请看附件。如果不清楚怎么一步一步实现的,可以移步去看OpenGL入门级课程的视频教程。

#include<windows.h>#include<gl/GL.h>#include<gl/GLU.h>#pragmacomment(lib,"opengl32.lib")LRESULTCALLBACKRenderWindowProc(HWNDhwnd,UINTmsg,WPARAMwParam,LPARAMlParam){switch(msg){caseWM_CLOSE:PostQuitMessage(0);break;}returnDefWindowProc(hwnd,msg,wParam,lParam);}INTWINAPIWinMain(_In_HINSTANCEhInstance,_In_opt_HINSTANCEhPrevInstance,_In_LPSTRlpCmdLine,_In_intnShowCmd){WNDCLASSEXwndClass;wndClass.cbClsExtra=0;wndClass.cbSize=sizeof(WNDCLASSEX);wndClass.cbWndExtra=0;wndClass.hbrBackground=NULL;wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);wndClass.hIcon=NULL;wndClass.hIconSm=NULL;wndClass.hInstance=hInstance;wndClass.lpfnWndProc=RenderWindowProc;wndClass.lpszClassName=L"OpenGLWindow";wndClass.lpszMenuName=NULL;wndClass.style=CS_HREDRAW|CS_VREDRAW;ATOMatom=RegisterClassEx(&wndClass);HWNDhwnd=CreateWindowEx(NULL,L"OpenGLWindow",L"OpenGLRenderWindow",WS_OVERLAPPEDWINDOW,100,100,800,600,NULL,NULL,hInstance,NULL);HDCdc=GetDC(hwnd);PIXELFORMATDESCRIPTORpfd;memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));pfd.nVersion=1;pfd.dwFlags=PFD_DOUBLEBUFFER|PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_TYPE_RGBA;pfd.iLayerType=PFD_MAIN_PLANE;pfd.iPixelType=PFD_TYPE_RGBA;pfd.cColorBits=32;pfd.cDepthBits=24;pfd.cStencilBits=8;intpixelFormatID=ChoosePixelFormat(dc,&pfd);SetPixelFormat(dc,pixelFormatID,&pfd);HGLRCrc=wglCreateContext(dc);wglMakeCurrent(dc,rc);glClearColor(41.0/255.0f,71.0f/255.0f,121.0f/255.0f,1.0f);ShowWindow(hwnd,SW_SHOW);UpdateWindow(hwnd);MSGmsg;while(true){if(PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE)){if(msg.message==WM_QUIT){break;}TranslateMessage(&msg);DispatchMessage(&msg);}glClear(GL_COLOR_BUFFER_BIT);SwapBuffers(dc);}return0;}


附件:http://down.51cto.com/data/2368079