目录
以VTK8.2版本中的vtkWin32RenderWindowInteractor为例,说明使用Windows窗口消息实现自定义的消息框架所需要的结构。
1、消息循环
void vtkWin32RenderWindowInteractor::StartEventLoop()
{
// No need to do anything if this is a 'mapped' interactor
if (!this->Enabled || !this->InstallMessageProc)
{
return;
}
this->StartedMessageLoop = 1;
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
2、窗口过程
void vtkWin32RenderWindowInteractor::Enable()
{
vtkWin32OpenGLRenderWindow *ren;
vtkWin32OpenGLRenderWindow *tmp;
if (this->Enabled)
{
return;
}
if (this->InstallMessageProc)
{
// add our callback
ren = (vtkWin32OpenGLRenderWindow *)(this->RenderWindow);
this->OldProc = (WNDPROC)vtkGetWindowLong(this->WindowId,vtkGWL_WNDPROC);
tmp=(vtkWin32OpenGLRenderWindow *)vtkGetWindowLong(this->WindowId,sizeof(vtkLONG));
// watch for odd conditions
if (tmp != ren)
{
// OK someone else has a hold on our event handler
// so lets have them handle this stuff
// well send a USER message to the other
// event handler so that it can properly
// call this event handler if required
CallWindowProc(this->OldProc,this->WindowId,WM_USER+12,24,(intptr_t)vtkHandleMessage);
}
else
{
vtkSetWindowLong(this->WindowId,vtkGWL_WNDPROC,(intptr_t)vtkHandleMessage);
}
// Check for windows multitouch support at runtime
RegisterTouchWindowType RTW =
(RegisterTouchWindowType)GetProcAddress(GetModuleHandle(TEXT("user32")), "RegisterTouchWindow");
if(RTW != nullptr)
{
RTW(this->WindowId, 0);
}
#ifdef VTK_USE_TDX
if(this->UseTDx)
{
this->Device->SetInteractor(this);
this->Device->Initialize();
this->Device->StartListening();
}
#endif
// in case the size of the window has changed while we were away
int *size;
size = ren->GetSize();
this->Size[0] = size[0];
this->Size[1] = size[1];
}
this->Enabled = 1;
this->Modified();
}
3、Windows消息循环