如何将wxWebview对象的自定义事件发布到其父级wxWidgets中?

huangapple go评论79阅读模式
英文:

How to post a custom event of a wxWebview object to its parent in wxWidgets?

问题

我正在尝试将一个拖放事件发送给wxWebview对象的父对象。我知道wxWebView会消耗所有接收到的事件,所以我必须为其父对象创建一个自定义事件。我尝试了以下代码,但它不起作用:

void onNewWindow( wxWebViewEvent& evt )
{
  wxString* files = new wxString[ 1 ];
  files[ 0 ] = evt.GetURL();
  wxDropFilesEvent dropEvent( wxEVT_DROP_FILES, 1, files );
  dropEvent.SetEventObject( ctrl_->GetParent() );
  dropEvent.m_pos.x = 0;
  dropEvent.m_pos.y = 0;
  ctrl_->GetParent()->GetEventHandler()->ProcessEvent( dropEvent );
  // 我们不想打开新窗口,所以否决事件
  evt.Veto();
}

当我将文件拖放到wxWebview中时,它不起作用,但如果我将其拖放到其父对象上,程序就按预期工作。我不确定发生了什么以及是否已为父对象设置了正确的拖放事件。

我还尝试在Windows的wxWebView子对象中为接收到的消息设置钩子,方法是重写MSWWindowProc函数和SetWindowsHookEx,但到目前为止我还没有成功。

我正在使用wxWidgets 3.2.2.1,使用Windows 10上的Edge后端,其他平台在这个时刻不重要。

英文:

I am trying to send a drop event to the parent of wxWebview object. I know that wxWebView consumes all received events and I have to create a custom event for its parent. I tried the following code but it does not work:

void onNewWindow( wxWebViewEvent& evt )
    {
      wxString* files = new wxString[ 1 ];
      files[ 0 ] = evt.GetURL();
      wxDropFilesEvent dropEvent( wxEVT_DROP_FILES, 1, files );
      dropEvent.SetEventObject( ctrl_->GetParent() );
      dropEvent.m_pos.x = 0;
      dropEvent.m_pos.y = 0;
      ctrl_->GetParent()->GetEventHandler()->ProcessEvent( dropEvent );
      // we don't want to open a new window so veto the event
      evt.Veto();
    }

When I drop a file into the wxWebview, it does not work but if I drop it on its parent, program works as expected. I am not sure what's going on and whether I have set the correct drop event for the parent or not.

I also tried to set a hook for the message received in the child of wxWebView in Windows by overriding the MSWWindowProc function and SetWindowsHookEx but so far I have not succeeded.

I am using wxWidgets 3.2.2.1 with edge backend in Windows 10 and other platform are not important at this moment.

答案1

得分: 1

This depends on what do you mean by "work". If this just means "execute the handler bound to wxEVT_DROP_FILES in the parent", then this should work, but I'm not sure how is it really useful considering that you could just bind the same handler to the same event on ctrl_ directly.

If you mean "do exactly the same thing as if the file were dropped on the parent", then you can't do this at wx level because part of this "thing" is what is done by Windows itself when it gets WM_DROPFILES in DefWindowProc() and, of course, you can't trigger execution of this code by sending wx events as Windows doesn't know anything about them. So to do this you would indeed need to forward the Windows message instead.

But it's still not clear at all why exactly do you think you need to do all this.

英文:

This depends on what do you mean by "work". If this just means "execute the handler bound to wxEVT_DROP_FILES in the parent", then this should work, but I'm not sure how is it really useful considering that you could just bind the same handler to the same event on ctrl_ directly.

If you mean "do exactly the same thing as if the file were dropped on the parent", then you can't do this at wx level because part of this "thing" is what is done by Windows itself when it gets WM_DROPFILES in DefWindowProc() and, of course, you can't trigger execution of this code by sending wx events as Windows doesn't know anything about them. So to do this you would indeed need to forward the Windows message instead.

But it's still not clear at all why exactly do you think you need to do all this.

huangapple
  • 本文由 发表于 2023年6月8日 16:22:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76429933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定