本示例基于Win32创建多窗口的VTK渲染程序
VTK的类封装在myVTKApp.h文件中,如下:
#ifndef MYVTKAPP_H_INCLUDE
#define MYVTKAPP_H_INCLUDE
#include"vtkConeSource.h"
#include"vtkPolyDataMapper.h"
#include"vtkRenderWindow.h"
#include"vtkRenderWindowInteractor.h"
#include"vtkRenderer.h"
//定义一个简单的myVTKApp类
class myVTKApp{
public:
myVTKApp(HWND parent);
~myVTKApp();
private:
vtkRenderWindow *renWin;
vtkRenderer *renderer;
vtkRenderWindowInteractor *iren;
vtkConeSource *cone;
vtkPolyDataMapper *coneMapper;
vtkActor *coneActor;
};
//构造函数
myVTKApp::myVTKApp(HWND hwnd)
{
// 创建流水线
this->renderer = vtkRenderer::New();
this->renWin = vtkRenderWindow::New();
this->renWin->AddRenderer(this->renderer);
// 启动渲染窗口的父窗口
this->renWin->SetParentId(hwnd);
this->iren = vtkRenderWindowInteractor::New();
this->iren->SetRenderWindow(this->renWin);
this->cone = vtkConeSource::New();
this->cone->SetHeight( 3.0 );
this->cone->SetRadius( 1.0 );
this->cone->SetResolution( 10 );
this->coneMapper = vtkPolyDataMapper::New();
this->coneMapper->SetInputConnection(this->cone->GetOutputPort());
this->coneActor = vtkActor::New();
this->coneActor->SetMapper(this->coneMapper);
this->renderer->AddActor(this->coneActor);
this->renderer->SetBackground(0.2,0.4,0.3);
this->renWin->SetSize(1024,500);
// 启动交互
this->renWin->Render();
}
//析构函数
myVTKApp::~myVTKApp()
{
renWin->Delete();
renderer->Delete();
iren->Delete();
cone->Delete();
coneMapper->Delete();
coneActor->Delete();
}
#endif
入口函数:
/*********************************************************************************/
/**主程序,包含入口函数、2个窗口处理函数,消息回调函数**************/
/**创建时间:2015-5-16 **/
/**建立者:张伟 **/
/**功能描述:实现图像在不同窗口中显示 **/
/*******************************************************************************/
#include "stdafx.h"
#include "myVTKApp.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]={_TEXT("主窗口")}; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
TCHAR szTitle2[MAX_LOADSTRING] = {_TEXT("第二窗口")};
TCHAR szWindowClass2[MAX_LOADSTRING] = {_TEXT("Submarine")}; // 第二个窗口类的类名
HWND hWnd, hWnd2;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc2(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32LEVELRAIL15, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15));
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex,wcex2;
//第一个窗口类
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32LEVELRAIL15));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
// 第二个窗口类的定义
wcex2.cbSize = sizeof(WNDCLASSEX);
wcex2.style = CS_HREDRAW | CS_VREDRAW;
wcex2.lpfnWndProc = (WNDPROC)WndProc2;
wcex2.cbClsExtra = 0;
wcex2.cbWndExtra = 0;
wcex2.hInstance = hInstance;
wcex2.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32LEVELRAIL15);
wcex2.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex2.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex2.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32LEVELRAIL15);
wcex2.lpszClassName = szWindowClass2;
wcex2.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return (RegisterClassEx(&wcex)&&RegisterClassEx(&wcex2));
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
//创建第一个窗口
hWnd = CreateWindow(szWindowClass, /*szTitle*/_TEXT("主窗口"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,1024, 600, NULL, NULL, hInstance, NULL);
//创建第二个窗口
hWnd2 = CreateWindow(szWindowClass2, /*szTitle2*/_TEXT("第二窗口"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT, 1024,600, NULL, NULL, hInstance, NULL);
if (!hWnd || !hWnd2)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
ShowWindow(hWnd2, nCmdShow);
UpdateWindow(hWnd2);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static myVTKApp *theVTKApp;
static HWND ewin;
switch (message)
{
case WM_CREATE:
ewin = CreateWindow("button","Exit",
WS_CHILD | WS_VISIBLE | SS_CENTER,
0,500,1024,50,
hWnd,(HMENU)2,
(HINSTANCE)vtkGetWindowLong(hWnd,vtkGWL_HINSTANCE),
NULL); //创建button按钮,用以退出程序
theVTKApp=new myVTKApp(hWnd);
return 0;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 2:
PostQuitMessage(0);
delete theVTKApp;
theVTKApp=NULL;
break;
case IDM_HELP_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_FILE_EXIT:
if(MessageBox(hWnd, _TEXT("确认离开程序"), _TEXT("警告"), MB_OKCANCEL)==IDOK)
{
DestroyWindow(hWnd);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
if(theVTKApp)
{
delete theVTKApp;
theVTKApp=NULL;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK WndProc2(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static myVTKApp *theVTKApp1;
static HWND ewin2;
switch (message)
{
case WM_CREATE:
ewin2 = CreateWindow("button","Exit",
WS_CHILD | WS_VISIBLE | SS_CENTER,
0,500,1024,50,
hWnd,(HMENU)2,
(HINSTANCE)vtkGetWindowLong(hWnd,vtkGWL_HINSTANCE),
NULL); //创建button按钮,用以退出程序
theVTKApp1=new myVTKApp(hWnd);
return 0;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case 2:
PostQuitMessage(0);
delete theVTKApp1;
theVTKApp1=NULL;
break;
case IDM_HELP_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_FILE_EXIT:
if(MessageBox(hWnd, _TEXT("确认离开窗口"), _TEXT("警告"), MB_OKCANCEL)==IDOK)
{
DestroyWindow(hWnd);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
if(theVTKApp1)
{
delete theVTKApp1;
theVTKApp1=NULL;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
运行效果如下: