一个Viewer管理多个view对象,一个view就是一个视图。它不与具体的窗口关联。
构造一个view需要两个对象: 一个是Aspect_WindowDriver的子类对象,一个是Viewer。
Aspect_WindowDriver的父类是Aspect_Driver,它有画直线段,多边形等纯虚函数。
可以把它看成是抽象的GDI,即用来画图。在windows平台下,可使用Aspect_WindowDriver
的子类WNT_WDriver。构造这个对象只需要一个WNT_Window对象。它是Aspect_Window的子类。
WNT_Window是一个被包装的窗口。传入父窗体句柄,然后调用CreateWindow的API,创建一个Windows OS
的窗口。New 一个WNT_Window需要父窗口句柄和WNT_GraphicDevice对象。它的父类是
Aspect_GraphicDevice,是一个颜色管理类。在Windows,X-Window ,及各种显卡,它们显
示颜色方式不统一,OCC为是可移植到其它系统上,创建了这个颜色管理类。
WNT_GraphicDevice是它的子类,针对Windows NT平台。只要new 一个
WNT_GraphicDevice就可以用了。
好了,我们用代码描述创建一个OCC窗口的过程。
device = new WNT_GraphicDevice;
win = new WNT_Window(device, 父窗口句柄);
Driver = new WNT_WDriver(win);
View = newV2d_View(driver,已先创建的Viewer对象);
创建Viewer对象,需要一个WNT_GraphicDevice对象。
下面的例子,显示了一个金黄色的窗口。
附加包含目录 $(CASROOT)\inc
附加依赖项 TKernel.lib TKService.lib TKV2d.lib
代码如下
//mfc.h
#include <Handle_V2d_View.hxx>
class CMFCApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class CMainWindow : public CFrameWnd
{
public:
CMainWindow();
protected:
Handle_V2d_View pV2dView;
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP ()
};
//MFC.cpp
#include <afxwin.h>
#include "resource.h"
#include "MFC.H"
#include "OCCMgr.h"
#include <Quantity_Color.hxx>
#include <V2d_View.hxx>
CMFCApp theApp;
BOOL CMFCApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
//ON_WM_PAINT ()
ON_WM_CREATE()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow()
{
Create(NULL,_T("A Simple MFC Program!"));
}
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
pV2dView = OCCMgr::Get()->CreatView(this->m_hWnd);
pV2dView->SetBackground(Quantity_Color(Quantity_NOC_GOLD));
return 0;
}
//OCCMgr.h
#pragma once
#include <v2d_viewer.hxx>
#include <Handle_WNT_GraphicDevice.hxx>
#include <Handle_V2d_View.hxx>
#include <Handle_WNT_WDriver.hxx>
#include <Handle_WNT_Window.hxx>
class OCCMgr
{
public:
static OCCMgr * Get();
Handle_V2d_View CreatView(HWND parentHwnd);
protected:
OCCMgr();
protected:
Handle_V2d_Viewer m_pViewer;
Handle_WNT_GraphicDevice m_pGraphicDevice;
Handle_V2d_View m_pView;
};
//OCCMgr.cpp
#include "afxwin.h"
#include "OCCMgr.h"
#include <WNT_WClass.hxx>
#include <WNT_Window.hxx>
#include <WNT_WDriver.hxx>
#include <V2d_View.hxx>
#include <WNT_GraphicDevice.hxx>
OCCMgr * OCCMgr::Get()
{
static OCCMgr mgr;
return & mgr;
}
OCCMgr::OCCMgr()
{
try
{m_pGraphicDevice = new WNT_GraphicDevice();}
catch(Standard_Failure)
{
AfxMessageBox("Fatal error during graphic initialization",MB_ICONSTOP);
ExitProcess(1);
}
TCollection_ExtendedString Name("Viewer 2D");
TCollection_AsciiString Domain("My Domain");
m_pViewer = new V2d_Viewer(m_pGraphicDevice,
Name.ToExtString(),
Domain.ToCString());
}
Handle_V2d_View OCCMgr::CreatView(HWND parentHwnd)
{
Handle_WNT_Window pWin = new WNT_Window(m_pGraphicDevice, parentHwnd);
Handle_WNT_WDriver pDriver = new WNT_WDriver(pWin);
m_pView = new V2d_View(pDriver, m_pViewer);
return m_pView;
}