在工程资源管理器窗口中添加一个模块,命名为OGL。在OGL模块中添加代码:
Public Sub EnableOpenGL(WorkPic As PictureBox)激活OGL Dim ghDC As Long ghDC = WorkPic.hDC Dim PixFormat As Long Dim pfd As PIXELFORMATDESCRIPTOR ZeroMemory pfd, Len(pfd) pfd.nSize = Len(pfd) pfd.nVersion = 1 pfd.dwFlags = PFD_DRAW_TO_WINDOW Or PFD_SUPPORT_OPENGL Or PFD_DOUBLEBUFFER pfd.iPixelType = PFD_TYPE_RGBA pfd.cColorBits = 24 pfd.cDepthBits = 32 pfd.iLayerType = PFD_MAIN_PLANE PixFormat = ChoosePixelFormat(ghDC, pfd) SetPixelFormat ghDC, PixFormat, pfd hRC = wglCreateContext(ghDC) wglMakeCurrent ghDC, hRC End Sub Public Sub DisableOpenGL()'禁用OGL wglMakeCurrent 0, 0 wglDeleteContext hRC End Sub
以上代码用于启动OpenGL,直接挪用就可以。在操作过程中,OGL的激活和禁用没有任何修改和变动。
接下来,在窗体代码中添加启动和注销OpenGL的操作代码。在窗体加载的时候,需要增加启动OpenGL的操作:
Private Sub Form_Load() Call OGL.EnableOpenGL(Me.WorkPic) End Sub
在窗体注销的时候,注销OpenGL:
Private Sub Form_Unload(Cancel As Integer) Call DisableOpenGL End Sub