How to enable or disable an event hander in MFC?

2019-08-31 05:31发布

问题:

I have searched the web a lot but couldn't find exactly what I want!
suppose that I have a class derived from CWnd. In fact, it is the class COpenGLControl here in codeguru customized by me for my own purposes.
the event handler for the WM_MOUSEMOVE button is written as follows:

void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
    int diffX = (int)(point.x - m_fLastX);
    int diffY = (int)(point.y - m_fLastY);
    m_fLastX = (float)point.x;
    m_fLastY = (float)point.y;

    // Left mouse button
    if (nFlags & MK_LBUTTON)
    {
         m_fRotX += (float)0.5f * diffY;

         if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
         {
             m_fRotX = 0.0f;
         }

         m_fRotY += (float)0.5f * diffX;

         if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
         {
         m_fRotY = 0.0f;
         }
    }

    // Right mouse button
    else if (nFlags & MK_RBUTTON)
    {
        m_fZoom -= (float)0.1f * diffY;
    }

    // Middle mouse button
    else if (nFlags & MK_MBUTTON)
    {
    m_fPosX += (float)0.05f * diffX;
    m_fPosY -= (float)0.05f * diffY;
    }

    OnDraw(NULL);

    CWnd::OnMouseMove(nFlags, point);
}  

But I don't want this event handler to be active or enabled at all times. I want to put three buttons on my dialog named pan,rotate and zoom.
when I click pan I want OnMouseMove get active just for middle button
when I click rotate I want middle button get inactive and left button get active
when I click zoom I want left button get inactive and right one get active and so.
and finally when I click another button like Zoom extent, select and etc, I want the OnMouseMove event handler get inactive in such a way that even if I'm on the opengl window the Maya-style mouse won't be active?
How can I implement something like this whether in my customized COpenGLControl class or in My MFC dialog?
Please give some instructions to me to begin my search to find out more.


------------------------------------------------------------------------------------------ Edited part of my question
I also thought about adding an event-handler manually to my class just like the OnDraw function in the COpenGLControl class so did something like this:

OpenGLContro.h

afx_msg void Pan(UINT nFlags, CPoint point);  

OpenGLControl.cpp

void COpenGLControl::Pan(UINT nFlags, CPoint point)  
{
    int diffX = (int)(point.x - m_fLastX);
    int diffY = (int)(point.y - m_fLastY);
    if (nFlags & MK_MBUTTON)
    {
        m_fPosX += (float)0.05f * diffX;
        m_fPosY -= (float)0.05f * diffY;
    }
    OnDraw(NULL);
}  

and whenever the button pan is clicked I will call this function but there I'm not still on the OpenGL Window and don't know hat to pass as the parameters to the function Pan?

回答1:

Add a state/mode member variable to the class and for each "mode" a dedicated handler function. The event handlers you use the mode variable to decide, which of the mode dependent handlers you call from the event handler, passing all the parameters.