VS2013 using MFC to make OpenGL programs containing menus

Source: Internet
Author: User

Open VS2013 New Project MFC program: named: Mfcopengldemo


Click OK next to build a dialog-based program


Click Finish:


Add a resource to a resource View menu



Then add a variable of type protected in MFCOpenGLDemo.h:

CMenu M_menu;

Add in the MFCOpenGLDemo.cpp file bool Cmfcopengldemodlg::oninitdialog () function

M_menu.loadmenu (IDR_MENU1);
SetMenu (&m_menu);

Then add a new dialog box:

Name the rectangle, change the ID to Idd_rect, and add a class to it rect.h

Change the contents of a rect.h

Class Rect:public CDialogEx
{
Declare_dynamic (Rect)


Public
Rect (cwnd* pparent = NULL); Standard constructors
Virtual ~rect ();


dialog box data
enum {IDD = Idd_rect};


Protected

Hicon M_hicon;
HGLRC HRC;
HDC hdc;


virtual void DoDataExchange (cdataexchange* pDX); DDX/DDV Support
void Drawfunc (); //Drawing
void Changedrawwinsize (); //Change window for large hours


Declare_message_map ()
Public
afx_msg void OnSize (UINT nType, int cx, int cy);
afx_msg void OnDestroy ();
afx_msg void OnPaint ();
Virtual BOOL OnInitDialog ();
afx_msg void OnTimer (Uint_ptr nidevent);
afx_msg void OnClose ();
afx_msg hcursor Onquerydragicon ();
};

Can be added with the Class wizard using the Class Wizard;

Include header files in the Rect.cpp file # include "Gl\glut.h"

Change the following code

Line::line (cwnd* pparent/*=null*/)
: CDialogEx (Line::idd, pparent)
{
M_hicon = AfxGetApp ()->loadicon (IDR_MAINFRAME);
}


Line::~line ()
{
}


void line::D odataexchange (cdataexchange* PDX)
{
CDialogEx::D odataexchange (PDX);
}




Begin_message_map (line, CDialogEx)
On_wm_close ()
On_wm_size ()
On_wm_paint ()
On_wm_querydragicon ()
On_wm_timer ()
End_message_map ()




Line Message Processing program




void Line::onclose ()
{
TODO: Add Message Handler code and/or call default values here
KillTimer (1);
Cdialogex::onclose ();
}




void Line::onsize (UINT nType, int cx, int cy)
{
Cdialogex::onsize (NType, CX, CY);
Changedrawwinsize ();
TODO: Add Message Handler code here
}


Hcursor Line::onquerydragicon ()
{
TODO: Add Message Handler code and/or call default values here


return Cdialogex::onquerydragicon ();
}




void Line::ontimer (Uint_ptr nidevent)
{
TODO: Add Message Handler code and/or call default values here
Drawfunc ();
Cdialogex::ontimer (nidevent);
}


BOOL Line::oninitdialog ()
{
Cdialogex::oninitdialog ();


TODO: Add additional initialization here
SetIcon (M_hicon, TRUE); Set Large Icons
SetIcon (M_hicon, FALSE); Set Small Icons


/****************************************************************/
Set the pixel format in OnInitDialog and select this format in the DC
Static Pixelformatdescriptor PFD = {sizeof (pixelformatdescriptor),
1,
Pfd_draw_to_window |
Pfd_support_opengl |
Pfd_doublebuffer,
Pfd_type_rgba,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
Pfd_main_plane,
0,
0, 0, 0
};
int PixelFormat;
HDC =:: GetDC (This->getsafehwnd ());
if (! ( PixelFormat = Choosepixelformat (HDC, &PFD)))
{
MessageBox (L "Choosepixelformat failed!");
return false;
}
if (! Setpixelformat (HDC, PixelFormat, &PFD))
{
MessageBox (L "Setpixelformat failed!");
return false;
}
if (! ( HRC = Wglcreatecontext (HDC)))
{
MessageBox (L "Createcontext failed!");
return false;
}
if (!wglmakecurrent (HDC, HRC))
{
MessageBox (L "Makecurrent failed!");
return false;
}


Changedrawwinsize ();


SetTimer (1, +, NULL); Use a timer (the simplest way)
/****************************************************************/




return TRUE; Return TRUE unless you set the focus to a control
Exception: OCX property page should return FALSE
}


void Line::changedrawwinsize ()
{
CRect rect; Draw in this rectangle
GetClientRect (rect);
Draw area occupies entire window size
Glviewport (0, 0, rect. Width (), Rect. Height ());
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (45.0f, rect. Width ()/rect. Height (), 0.1f, 100.0f);//calculate the appearance ratio of the window */
Gluperspective (45.0f, 640/480, 0.1f, 100.0f);//calculate the appearance ratio of the window
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Glclearcolor (0.0f, 0.0f, 0.0f, 1.0f);
Glcleardepth (1.0f);
Glenable (gl_depth_test);
}




void Line::onpaint ()
{
CPAINTDC DC (this); Device context for painting
TODO: Add Message Handler code here
Do not call Cdialogex::onpaint () for a drawing message
if (Isiconic ())
{
CPAINTDC DC (this); Device context for drawing


SendMessage (Wm_iconerasebkgnd, Reinterpret_cast<wparam> (DC). GETSAFEHDC ()), 0);


Center the icon in the workspace rectangle
int cxicon = GetSystemMetrics (Sm_cxicon);
int cyicon = GetSystemMetrics (Sm_cyicon);
CRect rect;
GetClientRect (&rect);
int x = (rect. Width ()-Cxicon + 1)/2;
int y = (rect. Height ()-Cyicon + 1)/2;


Draw icon
dc. DrawIcon (x, y, M_hicon);
}
Else
{
Cdialogex::onpaint ();
}
}




void line::D rawfunc ()//function specific implementation
{
Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
Glloadidentity ();
Gltranslatef (0.0f, 0.0f, -3.0f);


Glbegin (gl_quads); Draw a square
GLTEXCOORD2F (0.0, 1.0); Texture on top left
glvertex3f ( -1.0f, 1.0f, 0.0f); Quadrilateral left Upper


GLTEXCOORD2F (1.0, 1.0); Texture on top Right
glvertex3f (1.0f, 1.0f, 0.0f); Top Right


GLTEXCOORD2F (1.0, 0.0); Texture bottom Right
glvertex3f (1.0f, -1.0f, 0.0f); Lower right


GLTEXCOORD2F (0.0, 0.0); Texture bottom Left
glvertex3f ( -1.0f, -1.0f, 0.0f); Lower left


Glend ();


Swapbuffers (HDC); With double buffering
}

Then add message handling for the Rectangle menu in the menu in Resource View

void Cmfcopengldemodlg::onrect ()
{
TODO: Add Command handler code here
Rect mrect;
Mrect.domodal ();
}

Then run the click Paint, and the rectangle will show the result.








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

VS2013 using MFC to make OpenGL programs containing menus

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.