有时候需要在对话框上显示OpenCASCADE的模型,现基于CStatic封装了一个简单的控件:
COpenCascadeCtrl
代码如下:
#pragma once
// COpenCascadeCtrl
class COpenCascadeCtrl : public CStatic
{
DECLARE_DYNAMIC(COpenCascadeCtrl)
public:
COpenCascadeCtrl(Handle_Graphic3d_WNTGraphicDevice theGD);
virtual ~COpenCascadeCtrl();
Handle_AIS_InteractiveContext GetAIC() const {return mAISContext;}
Handle_V3d_View GetView() const { return mActiveView; }
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
private:
Handle_Graphic3d_WNTGraphicDevice theGraphicDevice;
Handle_V3d_View mActiveView;
Handle_V3d_Viewer mActiveViewer;
Handle_AIS_InteractiveContext mAISContext;
};
实现文件:
// OpenCascadeCtrl.cpp : implementation file
//
#include "stdafx.h"
#include "OpenCascadeCtrl.h"
// COpenCascadeCtrl
IMPLEMENT_DYNAMIC(COpenCascadeCtrl, CStatic)
COpenCascadeCtrl::COpenCascadeCtrl(Handle_Graphic3d_WNTGraphicDevice theGD)
: theGraphicDevice(theGD)
{
}
COpenCascadeCtrl::~COpenCascadeCtrl()
{
}
BEGIN_MESSAGE_MAP(COpenCascadeCtrl, CStatic)
ON_WM_CREATE()
ON_WM_PAINT()
END_MESSAGE_MAP()
// COpenCascadeCtrl message handlers
int COpenCascadeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CStatic::OnCreate(lpCreateStruct) == -1)
return -1;
mActiveViewer = new V3d_Viewer(theGraphicDevice, Standard_ExtString("OpenCascadeCtrl"));
mActiveViewer->SetDefaultLights();
mActiveViewer->SetLightOn();
mActiveView = mActiveViewer->CreateView();
Handle(WNT_Window) aWNTWindow = new WNT_Window(theGraphicDevice, GetSafeHwnd(), Quantity_NOC_GRAY);
aWNTWindow->SetDoubleBuffer(Standard_False);
mActiveView->SetWindow(aWNTWindow);
mAISContext = new AIS_InteractiveContext(mActiveViewer);
mAISContext->SetDisplayMode(AIS_Shaded, false);
return 0;
}
void COpenCascadeCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CStatic::OnPaint() for painting messages
mActiveView->Redraw();
}
用的时候,在对话框上加一个Picture控件(IDC_STATIC_LOC),初始化对话框的时候创建
COpenCascadeCtrl1 CRect rect;
2 GetDlgItem(IDC_STATIC_LOC)->GetWindowRect(&rect);
3 ScreenToClient(&rect);
4 m_OpenCascadeCtr->Create(_T(""), WS_CHILD|WS_VISIBLE, rect, this, IDC_STATIC_LOC);
代码下载