How to use ATL to make an OpenGL (COM) control under VC6 or VC7?

zhaozj2021-02-08  457

How to use ATL to make an OpenGL (COM) control under VC6 or VC7?

Objective: These things must be written every time when writing OpenGL programs, and it is especially cumbersome! In order to enter the OpenGL person to provide an interface control, let them see the effect of the OpenGL program written earlier! Let them feel the magic of OpenGL!

Author: Wang Weixing (wangweixing2000)

1. Create a new ATL null project (project name OpenGL_ATL)

2, add an ATL object (MyControl) (under the VC6 is Full Control, the ATL control under VC7) must select Support Connection Points In order to add an event.

3, add: H header files in the object:

#include

#include

#pragma comment (Lib, "OpenGL32.LIB")

#pragma comment (Lib, "Glu32.lib")

4. Add an OpenGL RC (Rendering Context) member variable to implement the class:

Hglrc m_hrc;

5, add a member function that sets the OpenGL pixel format (interface implementation class):

// set OpenGL Pixel Format for Given DC

Bool MyControl :: setuppixelformat (HDC HDC)

{

Static Pixelformatdescriptor PFD =

{

Sizeof (Pixelformatdescriptor), // size of this pfd

1, // Version Number

PFD_DRAW_TO_WINDOW | // Support Window

Pfd_support_opengl | // support OpenGL

PFD_DOUBLEBUFFER, // Double Buffered

PFD_TYPE_RGBA, // RGBA Type

24, // 24-bit color depth

0, 0, 0, 0, 0, 0, // color bits ignored

0, // no alpha buffer

0, // shift bit ignored

0, // no Accumulation Buffer

0, 0, 0, 0, // Accum bits ignored

32, // 32-bit z-buffer

0, // no steencil buffer

0, // no auxiliary buffer

Pfd_main_plane, // main layer

0, // reserved

0, 0, 0 // Layer Masks ignored

}

INT Pixelformat;

IF ((Pixelformat = Choosepixelformat (HDC, & PFD)) == 0)

{

Atlassert (false);

Return False;

}

IF (setpixelformat (hdc, pixelformat, & pfd) == false)

{

Atlassert (false);

Return False;

}

Return True;

}

6, add a Windows message WM_CREATE

LResult CMYControl :: oncreate (uint / * umsg * /, wparam / * wparam * /,

LPARAM / * LPARAM * /, BOOL & / * BHANDLED * /

{

HDC HDC = Getdc ();

Rect RC;

GetClientRect (& RC);

CreateContext (HDC, RC); // Initialization

Return 0;

}

7, add a Windows message WM_DESTROY

Lresult CMYCONTROL :: Ondestroy (uint / * umsg * /, wparam / * wparam * /, lparam / * lparam * /, bool & / * bhandled * /)

{

WGLmakecurrent (null, null);

IF (m_hrc)

{

WGLDeleteContext (m_hrc);

m_hrc = null;

}

Return 0;

}

7, add an event onrender, click on the iMyControlents in ClassView to add the onRender Parameters Int Right, int LEFT, INTTOM, INT TOP, then compile your IDL file, click CMYControl to implement the connection point to select the iMyControlvents Click OK.

8, add code on the onDraw:

HRESULT OnDRAW (ATL_DRAWINFO & DI)

{

HDC HDC = DI.HDCDRAW;

Rect & rc = * (Rect *) di.prcbounds;

WGLmakecurrent (HDC, M_HRC);

GlclearColor (1.0F, 0.0F, 0.0F, 10.0F);

GLCLEAR (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/ / Excitation OnRender event

FireonRender (rc.right, rc.Left, rc.bottom, rc.top)

/ / Do not add prefix Fire in VC7

//Onrender(Rc.right, rc.left, rc.bottom, rc.top)

GLFINISH ();

SwapBuffers (WGLgetCurrentDC ());

Return S_OK;

}

9, compile.

To use this Control very simple to sign up to your form, then write the object you want to draw in the OnRender event!

转载请注明原文地址:https://www.9cbs.com/read-741.html

New Post(0)