Home > Software engineering >  MFC use OpenGL display images in the Picture control how to get control of the mouse wheel rotation
MFC use OpenGL display images in the Picture control how to get control of the mouse wheel rotation

Time:09-18

MFC use OpenGL display images in the Picture control how to get control of the mouse wheel rotation message
In building a class Picture control can response to the mouse buttons also can response in the key and the mouse messages is not response to the mouse wheel rotation

H # OpenGL.
The class COpenGL: public CWnd
{
DECLARE_DYNAMIC (COpenGL)

Public:
COpenGL ();
Virtual ~ COpenGL ();
Protected:
DECLARE_MESSAGE_MAP ()

Public:
Afx_msg int OnCreate (LPCREATESTRUCT LPCREATESTRUCT);
Afx_msg void OnPaint ();
Afx_msg void OnLButtonDown (UINT nFlags, CPoint point);
Afx_msg void OnLButtonUp (UINT nFlags, CPoint point);
Afx_msg void OnMButtonDown (UINT nFlags, CPoint point);
Afx_msg void OnRButtonDown (UINT nFlags, CPoint point);
Afx_msg void onm ouseMove (UINT nFlags, CPoint point);
Afx_msg BOOL onm ouseWheel (UINT nFlags, short zDelta, CPoint pt);//add the mouse wheel response
Afx_msg void OnMButtonUp (UINT nFlags, CPoint point);
Cx, afx_msg void OnSize (UINT nType, int int cy);
Public:
Int MySetPixelFormat (HDC HDC);
Void RefreshDisplay1 ();
Public:
HDC HDC.
HGLRC HGLRC;
GLfloat step, s;
};


OpenGL. CPP
IMPLEMENT_DYNAMIC (COpenGL, CWnd)

COpenGL: : COpenGL ()
{
M_xPos=0.0 f;
M_yPos=0.0 f;
M_zPos=0.0 f;
M_xAngle=0.0 f;
M_yAngle=0.0 f;
M_zAngle=0.0 f;
M_Scale=1.0 f;
M_Capture=false;
M_MouseDown=false;
M_LButtonDown=false;
}

COpenGL: : ~ COpenGL ()
{
WglMakeCurrent (NULL, NULL);
WglDeleteContext (HGLRC);//delete rendering describe table
: : ReleaseDC (m_hWnd, HDC);//release device context
}


BEGIN_MESSAGE_MAP (COpenGL, CWnd)
ON_WM_CREATE ()
ON_WM_PAINT ()

ON_WM_LBUTTONDOWN ()
ON_WM_LBUTTONUP ()
ON_WM_MBUTTONDOWN ()
ON_WM_MBUTTONUP ()
ON_WM_RBUTTONDOWN ()
ON_WM_MOUSEMOVE ()
ON_WM_MOUSEWHEEL ()//have been added the mouse wheel response
ON_WM_SIZE ()
END_MESSAGE_MAP ()



//COpenGL message handler


Int COpenGL: : OnCreate (LPCREATESTRUCT LPCREATESTRUCT)//create the form
{
If (CWnd: : OnCreate (lpCreateStruct)==1)
return -1;

//TODO: here to add your special creation code
MySetPixelFormat (: : GetDC (m_hWnd));//create graphics context, and the associated rendering context
CPaintDC dc (this);

HDC=: : GetDC (m_hWnd);
HGLRC=wglCreateContext (HDC);//the drawing description table to the calling thread's current drawing description table
WglMakeCurrent (HDC, HGLRC);

GlClearColor (0.0 f, f, 0.0 0.0 f, 0.0 f);//clear color cache
GlClearDepth (10.0 f);
//clear the depth bufferGlEnable (GL_DEPTH_TEST);//enabled depth

return 0;
}

Void COpenGL: : OnPaint ()//draw the form
{
CPaintDC dc (this);//there is no this instruction is unable to use the timer
//painting device context
//TODO: add message handler code here

GlClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear color and depth buffer
GlLoadIdentity ();//load status

GlTranslatef (m_xPos m_yPos, m_zPos);//translation
GlRotatef (m_xAngle, 1.0 f, f, 0.0 0.0 f);//by the Angle of rotation
GlRotatef (m_yAngle, 0.0 f, f, 1.0 0.0 f);
GlScalef (m_Scale m_Scale, m_Scale);//zoom
DrawBox ();
SwapBuffers (HDC);
//no message calls for drawing COpenGL: : OnPaint ();
}

Void COpenGL: : OnSize (UINT nType, cx, int int cy)
{
Cx, CWnd: : OnSize (nType, cy);

//TODO: add message handler code here
If (0 & gt; Cx |=| 0 & gt;=cy)
{
return;
}
M_cx=cx (float); Cy m_cy=(float);
GlViewport (0, 0, cx, cy);//viewport parameters X and Y specifies the position of the lower left corner of the view area in the window, under normal circumstances is (0, 0), Width and Height specified Width and Height of the view area
GlMatrixMode (GL_PROJECTION);//matrix model
GlLoadIdentity ();//load
//gluPerspective (60.0, cx/(GLfloat) cy (GLfloat), 1.0, 10.0);
//gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
If (cx & lt; Cy)
{
//glOrtho (100.0, 100.0, 100.0 * (GLfloat) cy/cx, (GLfloat) 100.0 * (GLfloat) cy/cx (GLfloat), 100.0, 100.0);//container//left/right/down/on/near/far
Cx/glOrtho (- (GLfloat) (2), (GLfloat) cx/(2), - (GLfloat) (cy/2) * (GLfloat) cy/cx, (GLfloat) (GLfloat) (cy/2) * (GLfloat) cy/cx, (GLfloat) - (GLfloat) cx/(2), (GLfloat) cx/(2));
}
The else
{
//glOrtho (cx/- 100.0 * (GLfloat) (GLfloat) cy, cx/100.0 * (GLfloat) (GLfloat) cy, 100.0, 100.0, 100.0, 100.0);
Cx/glOrtho (- (GLfloat) (2) * (GLfloat) cx/cy GLfloat, (GLfloat) (cx/2) * (GLfloat) cx/cy GLfloat, - (GLfloat) (cy/2), (GLfloat) (cy/2), - (GLfloat) cx/(2), (GLfloat) cx/(2));
}

GlMatrixMode (GL_MODELVIEW);//matrix model
GlLoadIdentity ();//load
}

Int COpenGL: : MySetPixelFormat (HDC HDC)
{
PIXELFORMATDESCRIPTOR PFD={
Sizeof (PIXELFORMATDESCRIPTOR),//the size of the structure of PFD
No. 1,//version
PFD_DRAW_TO_WINDOW |//support in drawing window
PFD_SUPPORT_OPENGL |//support OPENGL
PFD_DOUBLEBUFFER,//double buffer
PFD_TYPE_RGBA,//RGBA color mode
24,//a 24-bit color depth
0,0,0,0,0,0,//ignore color a
0,//not a transparent cache
0,//ignore shift a
0,//no accumulative cache
0,0,0,0,//ignore accumulate a
32,//32 bit depth buffer
0,//no template cache
0,//no secondary cache
PFD_MAIN_PLANE,//main floor
0,//keep
0 0,//ignore layers, visibility and damaged mask
};
Int iPixelFormat;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related