C++ 类CopinglControl的两个实例之间的动态数据交换
下面假设我的类中有两个公共成员数据用作模式:C++ 类CopinglControl的两个实例之间的动态数据交换,c++,opengl,real-time,dynamic-data,multiple-instances,C++,Opengl,Real Time,Dynamic Data,Multiple Instances,下面假设我的类中有两个公共成员数据用作模式: bool WantToSetRectangle; bool WantToDrawRectangle; 我还有两个公共成员数据,它们是有4个成员的向量,用于设置一个矩形和绘制一个矩形 vector<int>ViewRectangle; vector<int>RectangleToDraw; 现在,我在我的CDialogEx中创建了类COpenGLControl的两个实例: COpenGLControl m_oglWin
bool WantToSetRectangle;
bool WantToDrawRectangle;
我还有两个公共成员数据,它们是有4个成员的向量,用于设置一个矩形和绘制一个矩形
vector<int>ViewRectangle;
vector<int>RectangleToDraw;
现在,我在我的CDialogEx
中创建了类COpenGLControl
的两个实例:
COpenGLControl m_oglWindow1;
COpenGLControl m_oglWindow2;
void CMyOpenGLTestDlg::OnBnClickedButton4()
{
// TODO: Add your control notification handler code here
m_oglWindow1.FixedZoomOut();
m_oglWindow2.RectangleToDraw = m_oglWindow1.ViewRectangle;
m_oglWindow2.OnDraw(NULL);
}
正如您在m_oglWindow1
的第一张图片中看到的,是较大的窗口,m_oglWindow2
是较小的窗口。我将两个窗口的模式设置如下:(这些模式在构造函数中设置为false
)
每次调用m_oglWindow1
的onDraw
函数时,都会设置ViewRectangle
。这些数据应动态地传递到m_oglWindow2
的RectangleToDraw
,之后应立即调用m-oglWindow2
的OnDraw
函数,在始终处于全范围
模式的较小窗口上绘制范围矩形。请记住,对于像
固定放大
这样的任务,我可以很容易地在我的CDialogEx
的按钮点击处理程序中编写:
COpenGLControl m_oglWindow1;
COpenGLControl m_oglWindow2;
void CMyOpenGLTestDlg::OnBnClickedButton4()
{
// TODO: Add your control notification handler code here
m_oglWindow1.FixedZoomOut();
m_oglWindow2.RectangleToDraw = m_oglWindow1.ViewRectangle;
m_oglWindow2.OnDraw(NULL);
}
但是在使用类COpenGLControl
的鼠标事件处理程序实现的其他任务(如pan
、放大点
和缩小点
)中,我需要在类的两个实例之间进行某种实时数据交换:
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);
}
void COpenGLControl::OnMouseMove(UINT nFlags,CPoint point)
{
//TODO:在此处添加消息处理程序代码和/或调用默认值
如果(WANTOPAN)
{
if(mu fLastX<0.0f&&mu fLastY<0.0f)
{
m_fLastX=(浮点)点x;
m_fLastY=(浮点)点y;
}
int diffX=(int)(点x-m_fLastX);
int diffY=(int)(点y-m_fLastY);
m_fLastX=(浮点)点x;
m_fLastY=(浮点)点y;
if(nFlags和MK_MBUTTON)
{
m_fPosX+=(浮点)0.2f*m_fZoomInverse*diffX;
m_fPosY-=(浮点)0.2f*m_fZoomInverse*diffY;
}
OnDraw(空);
}
CWnd::OnMouseMove(nFlags,point);
}
void COpenGLControl::OnLButtonDown(UINT nFlags,CPoint point)
{
//TODO:在此处添加消息处理程序代码和/或调用默认值
如果(要使用ZoomTool)
{
if(nFlags和MK按钮)
{
m_fZoom=1.05f*m_fZoom;
m_fZoomInverse=1/m_fZoom;
int diffX=(int)(点x-oglWindowWidth/2);
int-diffY=(int)(点y-oglWindowHeight/2);
m_fPosX-=(浮点)diffX;
m_fPosY+=(浮点)diffY;
}
OnDraw(空);
}
CWnd::ONLBUTDOWN(nFlags,点);
}
void COpenGLControl::OnRButtonDown(UINT nFlags,CPoint point)
{
//TODO:在此处添加消息处理程序代码和/或调用默认值
如果(要使用ZoomTool)
{
if(nFlags和MK按钮)
{
m_fZoom=0.95f*m_fZoom;
m_fZoomInverse=1/m_fZoom;
int diffX=(int)(点x-oglWindowWidth/2);
int-diffY=(int)(点y-oglWindowHeight/2);
m_fPosX-=(浮点)diffX;
m_fPosY+=(浮点)diffY;
}
OnDraw(空);
}
CWnd::OnRButtonDown(nFlags,点);
}
每个控件都可以通知父对话框需要更新另一个控件
GetParent()->PostMessage(UWM_UPDATE2, 0, 0);
其中,用户定义的消息是:
#define UWM_UPDATE1 (WM_APP + 1)
#define UWM_UPDATE2 (WM_APP + 2)
如果将_消息放入其消息映射中,该对话框可以处理这些消息
ON_MESSAGE(UWM_UPDATE2, OnUpdate2)
LRESULT CMyOpenGLTestDlg::OnUpdate2(WPARAM, LPARAM)
{
}
谢谢@ScottMcP MVP告诉我这样做对吗?事实上,我只是想到了用户定义的消息,但由于我是MFC的新手,实现它需要一段时间。这个项目应该尽快准备好,所以我不想走错方向,从零开始?那么,请告诉我用户定义的消息是否是我应该学习的正确概念?是的,用户定义的消息是通知父窗口的正确概念。它不需要一段时间就能实现。