Use OpenGL in the VC multi-document application

zhaozj2021-02-11  193

About the use of OpenGL's use of OpenGL in the VC multi-document application: Many introduces OpenGL and MFC in combination with MFC, OpenGL can only be used in a single document application. ---- However, in the application, sometimes it is desired to express a variety of expressions in a variety of expressions, which cannot be implemented with an SDI application; and use MDI applications, you can open multiple views at the same time, each view Different properties of data are displayed. ---- There are two points in MDI to use OpenGL: ---- 1, properly set and manage global variables. ---- 2, the correct device context (DC: Device Context) is associated with drawing context (rc: rendering context), each view manages a device context, and draw only one throughout the program, so there must be Correctly associated, you can correctly display the drawing scenes in each view. ---- The following is a simple example (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 the message of the MFC to press the message 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. ---- Take the following changes in the view object CTestmdiView: ---- 1) Add a member variable hglrc m_hrc; draw the 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 modified as follows: void ctestmdiView :: Ondraw ()

{

.....

// 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 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-3976.html

New Post(0)