Chapter 7
������������� ��� ��� �������� ���������� � ����

7.4  MFC-���������� - MFC AppWizard

     ���� ��� ���������� �������� ����� ������������� � ������������ WinAPI-����������, �������������� ����, ��� ��� MFC - ��� ���������� ������� �++, �.�. ���������� ��� WinAPI. ���-������, �������, ������, ��� ���������� �� ������ �������� �������, �������� �������� � MFC ��� �������. � �����, ��� � ������� ���� ������. ������ ��-������ ����. ��� �� �����, � ������, ��� ��� ������ ������ ��������� ���� ����������. ���-�� ����� ������������ MFC, ���-�� WinAPI. �������, ��� ������ ��� ������ �������� ����������� ��������� �� ��� ������ ����� ���� �� �������. � MFC ���� ���� �����������, ������� ������� � ������������ ��� ����������� ���������� ������. � ����������� �� �������� ������ ��� ���� � ���� ��� �����. �����������,��� ����� �������� �������� ������ �������� ��� �� ����� �������� ��������.

������� ������:

  1. ��������� MSVisualC++6.0
  2. �������� ���� File->New->MFC AppWizard(exe).
  3. �������� ������� � ��� ������� ������� mfc, �������� OK.
  4. Step1: ��������� ������������� �� Single document, ����� OK.
  5. Step3: ������� ������ ActiveX Controls, ����� OK.
  6. �������� Finish.
  7. �������� Build->Set Active Configuration � ���������� ��� ������� MFC - Win32 Release
  8. ����� �������� Project->Settings->Link->Object/library modules: � �������� ���� opengl32.lib, glu32.lib � glaux.lib

� CMFCView �������� ��������(private) ���������� hGLRC ���� HGLRC. ��� �� �������� ������� int SetWindowPixelFormat(HDC) � ��������(public) ������� display. ��� ��� ������ ���������:

 
 class CMFCView : public CView
{
private:
CClientDC *pdc;
HGLRC hGLRC;
int SetWindowPixelFormat(HDC);

public:
        void display();
...

     �������� ��� ���� ������� � ���� MFCView.cpp. ��� �������� �� ����������� �������. �������������� ������� CMFCView::PreCretaeWindow ��������� �������:

BOOL CMFCView::PreCreateWindow(CREATESTRUCT& cs)
{
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs
    cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS); 
        return CView::PreCreateWindow(cs);
}
�������� ����� ������� display ����:
void CMFCView::display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

        glTranslated(0.01,0,0); 
        glColor3d(1,0,0);
        auxSolidSphere( 1 );


  glFinish();
  SwapBuffers(wglGetCurrentDC());
}
������ ��������� View->Class Wizard � �������� ���������� WM_CREATE,WM_DESTROY � WM_SIZE � ����� CMFCView. �������������� �� ��������� �������:
int CMFCView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        
        pdc = new CClientDC(this);

        if(SetWindowPixelFormat(pdc->m_hDC)==FALSE)
        return -1;


         hGLRC = wglCreateContext(pdc->m_hDC);
    if(hGLRC == NULL)
        return -1;

    if(wglMakeCurrent(pdc->m_hDC, hGLRC)==FALSE)
        return -1;

    glEnable(GL_ALPHA_TEST);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    float pos[4] = {3,3,3,1};
    float dir[3] = {-1,-1,-1};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);

        return 0;
}

void CMFCView::OnDestroy() 
{
 if(wglGetCurrentContext()!=NULL) 
   wglMakeCurrent(NULL, NULL) ;
   

    if(hGLRC!=NULL)
        {
      wglDeleteContext(hGLRC);
      hGLRC = NULL;
    }

        delete pdc;

        CView::OnDestroy();
}

void CMFCView::OnSize(UINT nType, int cx, int cy) 
{
        CView::OnSize(nType, cx, cy);
        
   glViewport(0,0,cx,cy);
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho(-5,5, -5,5, 2,12);   
   gluLookAt( 0,0,5, 0,0,0, 0,1,0 );
   glMatrixMode( GL_MODELVIEW );
        
}
��������� ������ � ���������� ��������. � StdAfx.h �������� ������������ ����� ����� ������� #include <afxext.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
��������� ��� ��� Class Wizard � �������� ������� OnIdle � ����� CMFCApp. ��� ������� ���������� ������ ���, ����� ������� ��������� ���� �����. �������������� ��:
BOOL CMFCApp::OnIdle(LONG lCount) 
{
 ( (CMFCView*) ((CMainFrame*)m_pMainWnd)->GetActiveView() )->display(); 
        return 1;//CWinApp::OnIdle(lCount);
}


�������� ���� �������� �����. ����������� ���� �����.