用C++ vtk写一段代码,在制定位置生成vtkborderwidget
时间: 2023-10-06 17:12:36 浏览: 76
用vc++编写的界面实例代码
好的,这是一道编程问题。下面是参考答案:
```c++
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkActor.h>
#include <vtkBorderWidget.h>
#include <vtkCallbackCommand.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkWidgetEvent.h>
#include <vtkWidgetEventTranslator.h>
int main(int, char *[]) {
vtkNew<vtkPlaneSource> planeSource;
planeSource->Update(); // forces update of output and its data
vtkNew<vtkPolyDataMapper> planeMapper;
planeMapper->SetInputConnection(planeSource->GetOutputPort());
vtkNew<vtkActor> planeActor;
planeActor->SetMapper(planeMapper);
vtkNew<vtkRenderer> renderer;
renderer->AddActor(planeActor);
renderer->SetBackground(1, 1, 1); // white
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(300, 300);
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
vtkNew<vtkBorderWidget> borderWidget;
borderWidget->SetInteractor(interactor);
borderWidget->CreateDefaultRepresentation();
borderWidget->ResizableOn();
vtkSmartPointer<vtkCallbackCommand> callback =
vtkSmartPointer<vtkCallbackCommand>::New();
callback->SetCallback([](vtkObject *caller, long unsigned int eid,
void *clientData, void *callData) {
std::cout << "widget event: " << vtkCommand::GetStringFromEventId(eid)
<< std::endl;
});
borderWidget->AddObserver(vtkCommand::InteractionEvent, callback);
vtkNew<vtkInteractorStyleTrackballCamera> style;
interactor->SetInteractorStyle(style);
// vtkWidgetEventTranslator is necessary to capture events generated by the
// widget vtkBorderWidget, e.g., LeftButtonPressEvent
vtkNew<vtkWidgetEventTranslator> translator;
translator->SetTranslation(vtkCommand::LeftButtonPressEvent,
vtkWidgetEvent::Select);
translator->SetTranslation(vtkCommand::LeftButtonReleaseEvent,
vtkWidgetEvent::EndSelect);
translator->SetTranslation(vtkCommand::MiddleButtonPressEvent,
vtkWidgetEvent::Translate);
translator->SetTranslation(vtkCommand::MiddleButtonReleaseEvent,
vtkWidgetEvent::EndTranslate);
translator->SetTranslation(vtkCommand::RightButtonPressEvent,
vtkWidgetEvent::Scale);
translator->SetTranslation(vtkCommand::RightButtonReleaseEvent,
vtkWidgetEvent::EndScale);
translator->SetTranslation(vtkCommand::MouseWheelForwardEvent,
vtkWidgetEvent::Zoom);
translator->SetTranslation(vtkCommand::MouseWheelBackwardEvent,
vtkWidgetEvent::Zoom);
translator->SetTranslation(vtkCommand::MouseMoveEvent,
vtkWidgetEvent::MouseMove);
translator->SetTranslation(vtkCommand::KeyPressEvent,
vtkWidgetEvent::KeyDown);
translator->SetTranslation(vtkCommand::KeyReleaseEvent,
vtkWidgetEvent::KeyUp);
translator->AddEventObserver(borderWidget);
interactor->Initialize();
borderWidget->On();
interactor->Start();
return 0;
}
```
希望这个答案能够帮助您!如果您有问题,请随时问我!
阅读全文