VC multi-document application OpenGL's use

zhaozj2021-02-16  80

About the use of OpenGL in the VC multi-document application, the use of the National Defense University Computer, Zhou Wei 01-9-13 04:32:21

Many of the articles describing OpenGL and MFC are pointed out that OpenGL can only be used in a single document application.

However, in the application, sometimes it is desirable to simultaneously expressed a variety of expressions in a variety of expressions, and use the SDI application to implement this need; use the MDI application, you can open multiple views at the same time, display data in each view. Different attributes.

Two points in the MDI using OpenGL:

1, correctly set and manage global variables.

2, the correct device context (DC: device context) is associated with the plot context (rc: rendering context), each view manages a device context, and draw only one throughout the program, so it must be correctly associated The drawing scene is correctly displayed in each view.

Below below, this is the two points in a simple instance (TestMDi). The function of this example is to count the open view, and a square is displayed in the view of the odd-numbered view. Counter m_icount defines in the application class ctestmdiapp, initialized to 0. Because the application class is managed the entire program, including the initialization of the program, the global data should be defined here. CTestMDiapp also needs to add a message response function onfilenew ():

Void ctestmdiapp :: OnfileNew ()

{

m_icount ;

CWndapp :: onfilenew ();

}

This is a message processing mechanism of the MFC to press CView-> cdoc-> cmainframe-> CWndApp to each object, and is processed by the first object to the message response. Onfilenew () Implementation in CWndApp is generating a new document, which has no own variables in the Chinese file class, usually there is some parameters for controlling view properties in practical applications.

The view object CTestMDIVIEW is made as follows:

1) Increase member variables

Hglrc m_hrc; draw a context handle;

CDC * m_pcdc; device context pointer;

INT M_IID; View ID, the first aight view created in this app;

2) Modify On OnCreate (LPCReatestruct LPCreatestruct:

INT CTESTMDIVIEW :: oncreate

(Lpcreatestruct lpcreatestruct)

{

.........

// Todo: Add Your Specialized Creation Code Here

// OpenGL Rendering Context Crext

Pixelformatdescriptor PFD;

Int n;

// Initialize the Private Member

M_PCDC = New CclientDC (this);

// bsetuppixelformat () Establish the pixel format required to apply,

And associated with the current device context

IF (! bsetuppixelformat ()) Return 0;

n = :: getpixelformat (m_pcdc-> getsafehdc ());

:: Describepixelformat (m_pcdc->

GetsafeHDC (), N, SIZEOF (PFD), & PFD);

// link the win device context

With the ogl rendering context

m_hrc = wglcreateContext

(m_pcdc-> getsafehdc ());

// Specify The Target DeviceContext (Window) of The Subsequent Ogl Calls

WGLmakecurrent (m_pcdc-> getsafehdc (), m_hrc);

// Note: This name is not required in SDI applications, and MDI is essential

WGLmakecurrent (null, null);

// Get global variables: counters

Ctestmdiapp * PAPP = (ctestmdiapp *) AFXGetApp ();

M_IID = PAPP-> m_icount;

Return 0;

}

Return 0;

3) Modify the OnDestroy function:

Void ctestmdiView :: ONDESTROY ()

{

/ / Make sure to delete the correct view and draw a context

WGLmakecurrent (m_pcdc-> getsafehdc (), m_hrc);

IF (m_hrc! = null)

:: WGLDeleteContext (m_hrc);

// Destroy Win Device Context

IF (m_pcdc)

Delete m_pcdc;

.....

// Todo: .....

}

4) The onDraw () function is made as follows:

Void ctestmdiView :: overdraw ()

{

.....

// Todo: ....

WGLmakecurrent (m_pcdc-> getsafehdc (), m_hrc);

IF (PAPP-> M_ICOUNT% 2 == 0)

{

// Impherical number view, painting

Drawsquare ();

}

Else

{

// Situ numbers, painting

Drawcircle ();

}

// After paying attention to the painting, disconnect the DC and RC's association,

Make other views can be connected to RC when activated

WGLmakecurrent (null, null);

}

5) PRECREATEWINDOW (CREATESTRUCT & CS) function:

Bool CtestmdiView :: PrecreateWindow (CreateStruct & Cs)

{

// Todo: modify the window

Class or Styles Here by Modifying

// The createStruct CS

// OpenGL requirements:

CS.Style | = WS_CLIPSIBLINGS | WS_CLIPCHILDREN

// MDI application requirements:

cs.lpszclass = afxregisterWndClass (cs_owndc |

CS_HREDRAW | CS_VREDRAW);

Return CView :: PrecreateWindow (CS);

}

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

New Post(0)