CHtmlView:如何获取点击数据?

发布于 2024-11-13 07:32:59 字数 155 浏览 0 评论 0原文

我有一个使用 CHtmlView 的 MFC 应用程序。它显示一些临时 html 文件中的 html 格式的文本。是否可以处理鼠标单击段落以将一些数据发送到程序?我知道javascript可以用来处理点击,但是如何将数据从javascript函数传递到应用程序? 谢谢。

I have an MFC application that uses CHtmlView. It displays some text in html format from some temp html file. Is it possible to handle mouse click on a paragraph to send some data to the program? I understand that javascript can be used to handle click, but how to pass the data from javascript function to the application??
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生寂 2024-11-20 07:32:59

可以从 HTML 页面的 Javascript 中干净地调用包含的应用程序。在 Javascript 级别,执行 CHtmlView 实际工作的 MSHTML 接口提供了一个“外部”对象,该对象充当返回调用应用程序的方式。

假设我们要添加一个可以从 Javascript 调用的方法“someCall()”,并且该方法采用字符串作为参数。在 JavaScript 中,我们会使用类似的方式来调用它。

external.someCall("An example string");

在 MFC 应用程序中,我们需要编写一个 CCmdTarget 派生对象来充当“外部”对象的实现(作为基于调度的 COM 对象),例如:

class TestExternal : public CCmdTarget
{
public:
  TestExternal()
  {
    EnableAutomation();
  }

  void SomeCall(LPCWSTR str)
  {
    // This is where we get called when the Javascript runs...
  }

private:
  DECLARE_DISPATCH_MAP()
};

BEGIN_DISPATCH_MAP(TestExternal,CCmdTarget)
  DISP_FUNCTION(TestExternal,"someCall",SomeCall,VT_EMPTY,VTS_WBSTR)
END_DISPATCH_MAP()

要绑定“的此实现” external”与 HTML 视图,在从 CHtmlView 派生的类中,您需要重写 OnGetExternal() 并将其指向至少与 CHtmlView 一样长的 TestExternal 实例:

class TestHtmlView : public CHtmlView
{
  // Usual implementation stuff goes here...

public:
  HRESULT OnGetExternal(LPDISPATCH *lppDispatch)
  {
    *lppDispatch = m_external.GetIDispatch(TRUE);
    return S_OK;
  }

private:
  TestExternal m_external;
};

请注意,我实际上并没有对此进行了测试,但它似乎是凭记忆而来的......

It is possible to cleanly call the containing application from within the Javascript of the HTML page. At the Javascript level the MSHTML interface that is doing the actual work of the CHtmlView provides an "external" object that acts as a way back to the calling application.

Suppose we want to add a method "someCall()" that can be called from Javascript, and that the method takes a string as an argument. In JavaScript we would call it with something like

external.someCall("An example string");

In the MFC application, we need to write a CCmdTarget derived object to act as the implementation of the "external" object as a dispatch-based COM object, something like:

class TestExternal : public CCmdTarget
{
public:
  TestExternal()
  {
    EnableAutomation();
  }

  void SomeCall(LPCWSTR str)
  {
    // This is where we get called when the Javascript runs...
  }

private:
  DECLARE_DISPATCH_MAP()
};

BEGIN_DISPATCH_MAP(TestExternal,CCmdTarget)
  DISP_FUNCTION(TestExternal,"someCall",SomeCall,VT_EMPTY,VTS_WBSTR)
END_DISPATCH_MAP()

To tie this implementation of "external" with the HTML view, in a class derived from CHtmlView you need to over-ride OnGetExternal() and to point it to an instance of TestExternal that lives at least as long as the CHtmlView:

class TestHtmlView : public CHtmlView
{
  // Usual implementation stuff goes here...

public:
  HRESULT OnGetExternal(LPDISPATCH *lppDispatch)
  {
    *lppDispatch = m_external.GetIDispatch(TRUE);
    return S_OK;
  }

private:
  TestExternal m_external;
};

Note that I haven't actually tested this, but it seems about right from memory ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文