VTK+MFC:建立自定义控件显示

这里我们用MFC框架来做VTK的界面
VTK比较蛋疼的是不能像图片一样直接在MFC的picture控件中显示,需要自己添加自定义控件来显示画出的模型,
1.首先新建一个类如MyCustomControl
这里写图片描述
Ctrl+W打开建立类向导界面,选择Add Class->new
名字填写如MyCustomControl
Base class选择generic CWnd
2.在MyCustomControl.cpp文件中添加如下代码:
注册代码:

BOOL MyCustomControl::RegisterWndClass()
{
    WNDCLASS windowclass;
    HINSTANCE hInst = AfxGetInstanceHandle();

    //Check weather the class is registerd already
    if (!(::GetClassInfo(hInst, MYWNDCLASS, &windowclass)))
    {
        //If not then we have to register the new class
        windowclass.style = CS_DBLCLKS;// | CS_HREDRAW | CS_VREDRAW;
        windowclass.lpfnWndProc = ::DefWindowProc;
        windowclass.cbClsExtra = windowclass.cbWndExtra = 0;
        windowclass.hInstance = hInst;
        windowclass.hIcon = NULL;
        windowclass.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
        windowclass.hbrBackground = ::GetSysColorBrush(COLOR_WINDOW);
        windowclass.lpszMenuName = NULL;
        windowclass.lpszClassName = MYWNDCLASS;

        if (!AfxRegisterClass(&windowclass))
        {
            AfxThrowResourceException();
            return FALSE;
        }
    }

    return TRUE;
}

在构造函数中添加:

RegisterWndClass();

添加响应函数:

void MyCustomControl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    if(flag==FALSE)
    {
        oldpt=point;
        flag=TRUE;
    }

    CWnd::OnLButtonDown(nFlags, point);
}

void MyCustomControl::OnLButtonUp(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    flag=FALSE;

    CWnd::OnLButtonUp(nFlags, point);
}

void MyCustomControl::OnMouseMove(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default

    if(flag==TRUE)
    {

        CDC *d=GetDC();

        d->MoveTo(oldpt);
        d->LineTo(point);


        oldpt=point;

        ReleaseDC(d);
    }

    CWnd::OnMouseMove(nFlags, point);
}

这几个分别是控制右键按下时vtk画出来的模型的放大缩小,左键按下时vtk模型的旋转,鼠标在自定义控件窗口的移动
再在BEGIN_MESSAGE_MAP(MyCustomControl, CWnd)添加
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
2.右键点击添加自定义控件在种类中命名如MyDrawPad
这里写图片描述
3.在MyCustomControl.hpp文件中添加声明等
在文件开头添加:

#define MYWNDCLASS "MyDrawPad"

在public中添加:

MyCustomControl();
BOOL SetBitmap(UINT nIDResource);

在protect中修改(添加一些消息相应):

    //{{AFX_MSG(MyCustomControl)
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
        // NOTE - the ClassWizard will add and remove member functions here.
    //}}AFX_MSG

在private中添加:

private:
    CDC cDC;
    BOOL RegisterWndClass();
    CPoint oldpt;
    BOOL flag;

4.在Dlg.cpp中为自定义空键添加变量:
在::DoDataExchange(CDataExchange* pDX)中添加
DDX_Control(pDX,IDC_moxing,m_drawpad1);
5.在Dlh.h中添加头文件:
#include “MyCustomControl.h”
在protected中添加变量:
MyCustomControl m_drawpad1;
6.在vtk要显示的时候加上获取窗口的代码:

 vtkRenderer *ren = vtkRenderer::New();
  ren->SetBackground(1.0, 1.0, 1.0);
  vtkRenderWindow *renWindow = vtkRenderWindow::New();
  renWindow->AddRenderer(ren);
  //renWindow->SetSize( 200, 200 );
  CRect rect;
  GetClientRect(&rect);
  m_drawpad1.GetClientRect(&rect);
  renWindow->SetParentId(m_drawpad1.m_hWnd);
  renWindow->SetSize(rect.Width(),rect.Height());

之后可以在mfc框架下显示vtk画出的图像
这里写图片描述
这里写图片描述
我用了十五年,帮那时候我最喜欢的女生追回了她的丈夫……

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值