【问题标题】:dynamic data exchange between two instances of the class COpenGLControlCOpenGLControl 类的两个实例之间的动态数据交换
【发布时间】:2013-08-28 17:34:41
【问题描述】:

按照我的another question 假设我的班级中有两个公共成员数据用作模式:

bool WantToSetRectangle;
bool WantToDrawRectangle;  

我还有两个公共成员数据,它们是具有 4 个成员的向量,用于设置矩形和绘制矩形。

vector<int>ViewRectangle;
vector<int>RectangleToDraw; 

这是类的OnDraw函数的实现,在每个导航任务(如缩放、平移等)之后运行。

void COpenGLControl::OnDraw(CDC *pDC)
{
// TODO: Camera controls
wglMakeCurrent(hdc,hrc);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,1,0);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glScalef(m_fZoom,m_fZoom,1.0);
if (WantToSetRectangle)
    setViewRectangle();
if (WantToDrawRectangle)
    DrawRectangleOnTopOfTexture();
wglMakeCurrent(NULL, NULL);
}  

现在我在 CDialogEx 中创建了 COpenGLControl 类的两个实例:

COpenGLControl m_oglWindow1;
COpenGLControl m_oglWindow2;  

正如您在my another question 的第一张图片中看到的,m_oglWindow1 是较大的窗口,m_oglWindow2 是较小的窗口。我设置两个窗口的模式如下:(这些模式在构造函数中设置false

m_oglWindow1.WantToSetRectangle = true;
m_oglWindow2.WantToDrawRectangle = true;  

每次调用m_oglWindow1onDraw 函数时,都会设置ViewRectangle。这些数据应动态传递给m_oglWindow2RectangleToDraw,然后立即调用m-oglWindow2OnDraw 函数以在始终处于Full Extent 模式的较小窗口上绘制范围矩形。
请记住,对于像Fixed Zoom in 这样的任务,我可以很容易地在我的CDialogEx 的按钮单击处理程序中编写此代码:

void CMyOpenGLTestDlg::OnBnClickedButton4()
{
    // TODO: Add your control notification handler code here
    m_oglWindow1.FixedZoomOut();
    m_oglWindow2.RectangleToDraw = m_oglWindow1.ViewRectangle;
    m_oglWindow2.OnDraw(NULL);
}  

但是在使用mouse-event handlersmouse-event handlers 实现的其他任务中,例如panzoom in to the pointzoom out of the point,我需要在类的两个实例之间进行某种实时数据交换:

void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (WantToPan)
{
    if (m_fLastX < 0.0f && m_fLastY < 0.0f)
    {
        m_fLastX = (float)point.x;
        m_fLastY = (float)point.y;
    }
    int diffX = (int)(point.x - m_fLastX);
    int diffY = (int)(point.y - m_fLastY);
    m_fLastX = (float)point.x;
    m_fLastY = (float)point.y;
    if (nFlags & MK_MBUTTON)
    {
        m_fPosX += (float)0.2f*m_fZoomInverse*diffX;
        m_fPosY -= (float)0.2f*m_fZoomInverse*diffY;
    }
    OnDraw(NULL);
}
CWnd::OnMouseMove(nFlags, point);
}

void COpenGLControl::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (WantToUseZoomTool)
{
    if (nFlags & MK_LBUTTON)
    {
        m_fZoom = 1.05f*m_fZoom;
        m_fZoomInverse = 1/m_fZoom;
        int diffX = (int)(point.x - oglWindowWidth/2);
        int diffY = (int)(point.y - oglWindowHeight/2);
        m_fPosX -= (float)diffX;
        m_fPosY += (float)diffY;
    }
    OnDraw(NULL);
}
CWnd::OnLButtonDown(nFlags, point);
}  

void COpenGLControl::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (WantToUseZoomTool)
{
    if (nFlags & MK_RBUTTON)
    {
        m_fZoom = 0.95f*m_fZoom;
        m_fZoomInverse = 1/m_fZoom;
        int diffX = (int)(point.x - oglWindowWidth/2);
        int diffY = (int)(point.y - oglWindowHeight/2);
        m_fPosX -= (float)diffX;
        m_fPosY += (float)diffY;
    }
    OnDraw(NULL);
}
CWnd::OnRButtonDown(nFlags, point);
}

【问题讨论】:

    标签: c++ opengl real-time dynamic-data multiple-instances


    【解决方案1】:

    每个控件都可以通知父对话框需要更新另一个控件。

    GetParent()->PostMessage(UWM_UPDATE2, 0, 0);
    

    用户定义的消息在哪里:

    #define UWM_UPDATE1 (WM_APP + 1)
    #define UWM_UPDATE2 (WM_APP + 2)
    

    如果您将 ON_MESSAGE 放入其消息映射中,该对话框可以处理这些消息。

    ON_MESSAGE(UWM_UPDATE2, OnUpdate2)
    
    LRESULT CMyOpenGLTestDlg::OnUpdate2(WPARAM, LPARAM)
    {
    
    
    }
    

    【讨论】:

    • 谢谢@ScottMcP-MVP 告诉我这是正确的方法吗?事实上,我只是考虑了用户定义的消息,但由于我是 MFC 的新手,因此需要一段时间来实现它。这个项目应该尽快准备好,不想走错路,从零开始?那么告诉我用户定义的消息是否是我应该研究的正确概念?
    • 是的,用户定义的消息是通知父窗口的正确概念。实施不需要一段时间。
    猜你喜欢
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2018-12-06
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多