1

我正在尝试创建一个可以从 JavaScript 调用的自定义 C++ 函数。该功能是一个简单的调整窗口大小的功能。

我在以下地方有以下位:

在 appshell_extensions_platform.h 中:

#if defined(OS_WIN)
void ResizeWindow(CefRefPtr<CefBrowser> browser, int width, int height);
#endif

在 appshell_extensions_win.cpp 中:

void ResizeWindow(CefRefPtr<CefBrowser> browser, int width, int height) {
    OutputDebugString(L"ResizeWindow");
    CefWindowHandle hWnd = browser->GetHost()->GetWindowHandle();
    SetWindowPos(hWnd, 0, 0, 0, width, height, SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
}

在 appshell_extensions.js 中:

/**
  * Resize the window to the given size.
  *
  * @param {number} width
  * @param {number} height
  *
  * @return None. This is an asynchronous call that sends all return information to the callback.
 */
 native function ResizeWindow();
 appshell.app.resizeWindow = function (width, height) {
     ResizeWindow(width, height);
 };

在 appshell_extensions.cpp 中:

} else if (message_name == "ResizeWindow") {
    // Parameters:
    //  0: int32 - width
    //  1: int32 - height
    int width = argList->GetInt(0);
    int height = argList->GetInt(1);
    ResizeWindow(browser, width, height);
}

然后,我使用 Visual Studio 2012 在 Debug Win32 版本上构建和调试。当我打开控制台时,appshell.app.resizeWindow是否如预期的那样。我可以调用它,它工作得很好。如果我在函数中添加额外的 JavaScript 代码,它也可以工作。

在 中的函数中appshell_extensions.cpp,我添加了OutputDebugString(std::wstring(message_name.begin(), message_name.end()).c_str());. 对于我写的功能以外的功能,它会正确输出消息名称。对于我写的那一篇,我什么也得不到。

我也没有从函数本身获得输出。

看来消息实际上并没有到达处理它的函数,但我不知道。我只是使用括号壳(转换为2012)附带的 sln 来编译。是否有我可能缺少的构建步骤或其他什么?

谢谢。

4

2 回答 2

0

您尝试调试消息名称的 ProcessMessageDelegate::OnProcessMessageReceived() 方法 (appshell_extension.cpp) 正在 Renderer 进程中运行。您必须将 VS 调试器附加到该子进程才能对其进行调试。现在您正在调试浏览器进程(主进程)。

对于在控制台窗口中调试子进程的简单方法,请尝试为每个子进程创建一个控制台窗口:

if (show_console) {
    AllocConsole();
    FILE* freopen_file;
    freopen_s(&freopen_file, "CONIN$", "rb", stdin);
    freopen_s(&freopen_file, "CONOUT$", "wb", stdout);
    freopen_s(&freopen_file, "CONOUT$", "wb", stderr);
}

将此代码放在启动子进程的 CefExecuteProcess() 之前。

看看我是如何在 PHP 桌面项目中完成的:

  1. 请参阅 subprocess_show_console 变量。 https://code.google.com/p/phpdesktop/source/browse/phpdesktop-chrome/main.cpp?r=6de71bd0217a#126

  2. 对运行上述代码的 InitializeLogging() 的调用。

  3. 然后调用 CefExecuteProcess。


在 Linux 上,在控制台窗口中调试子进程更容易,因为子进程的所有输出都会自动从主进程转发到控制台。Windows 缺少这个简洁的功能。

于 2014-02-13T12:19:22.577 回答
0

我解决了这个问题。

显然,在 JavaScript 函数中,第一个参数必须是回调,即使您不使用它。

将 JavaScript 部分更改为此解决了问题:

/**
  * Resize the window to the given size.
  *
  * @param {number} width
  * @param {number} height
  *
  * @return None. This is an asynchronous call that sends all return information to the callback.
 */
 native function ResizeWindow();
 appshell.app.resizeWindow = function (width, height) {
     ResizeWindow(_dummyCallback, width, height);
 };

我对实际的调整大小逻辑也有一点问题。您需要使用browser->getHost()->getWindowHandle()父窗口句柄:

`CefWindowHandle hWnd = getParent(browser->GetHost()->GetWindowHandle());
于 2014-02-18T20:48:27.860 回答